﻿#!/bin/sh
export PATH='/opt/usr/sbin:/opt/usr/bin:/opt/sbin:/opt/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin'
export LD_LIBRARY_PATH=/lib:/opt/lib

# 天气数据下载失败，停止获取
exit 0

if [ -z "$RANDOM" ] ; then
SEED=`tr -cd 0-9 </dev/urandom | head -c 8`
else
SEED=$RANDOM
fi
RND_NUM=`echo $SEED 15 20|awk '{srand($1);printf "%d",rand()*10000%($3-$2)+$2}'`
echo $RND_NUM
sleep $RND_NUM

#http://weather.yahoo.com/2146704
#The location parameter needs to be a WOEID. 
#To find your WOEID, browse or search for your city from the Weather home page. 
#The WOEID is in the URL for the forecast page for that city. You can also get 
#the WOEID by entering your zip code on the home page. For example, if you search 
#for Los Angeles on the Weather home page, the forecast page for that city is 
#http://weather.yahoo.com/united-states/california/los-angeles-2146704/. 
#The WOEID is 2146704.
#changping2151334
#beijing 2151330

display_weather=`nvram get display_weather`
display_aqidata=`nvram get display_aqidata`
if [ -z "$display_weather" ] ;then 
display_weather="2151330"

fi
if [ -z "$display_aqidata" ] ;then 
display_aqidata="beijing"
fi

#wget -O /tmp/weather "http://xml.weather.yahoo.com/forecastrss?w=$display_weather&u=c"
#获取天气信息
yahooweb='https://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20weather.forecast%20WHERE%20woeid%3D"'$display_weather'"%20and%20u%3D"c"%20%7C%20truncate(count%3D1)&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'
echo $yahooweb
rm -f /tmp/weatherweb
wget -O /tmp/weatherweb $yahooweb
if [ $? = 0 ]; then
	cat /tmp/weatherweb | grep '<yweather' | awk -F'<yweather' '{ \
		{L1="<yweather"$3; L2="<yweather"$2; L3="<yweather"$4; L4="<yweather"$5; L5="<yweather"$6; L6="<yweather"$7; L7="<yweather"$8; L8="<yweather"$9}} \
		END \
		{ printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", \
		L1,L2,L3,L4,L5,L6,L7,L8}' \
		> /tmp/weather
	cat /tmp/weather | grep '<yweather' | awk -F'"' '{C1++;}{ \
		{if (substr($1, 11, 8)=="location") L1=$4} \
		{if (substr($1, 11, 4)=="wind") {L81=$4; L82=$6; L83=$8; \
		{if($6=="0") L84="N"} {if($6=="90") L84="E"} {if($6=="180") L84="S"} {if($6=="270") L84="W"} {if(("01"<$6)&&($6<90)) L84="EN"} {if(("90"<$6)&&($6<180)) L84="ES"} {if(("180"<$6)&&($6<"270")) L84="WS"} {if(("270"<$6)&&($6<"360")) L84="WN"}}} \
		{if (substr($1, 11, 10)=="atmosphere") { L91=$4; L92=$10; L93=$6; \
		{if($8=="0") L94="S"} {if($8=="1") L94="R"} {if($8=="2") L94="F"}}} \
		{if (substr($1, 11, 9)=="astronomy") {LA1=$4; LA2=$6}} \
		{if (substr($1, 11, 9)=="condition") {L2=$4; L3=$8"c"}} \
		{if (substr($1, 11, 8)=="forecast") {if (C1==7) {L4=$4;L5=$12"cdu"$10"c"}}} \
		{if (substr($1, 11, 8)=="forecast") {if (C1==8) {L6=$4;L7=$12"cdu"$10"c"}}}} \
		END \
		{ while (length(L3) < 4) {L3="n"L3}; while (length(L5) < 10) {L5="n"L5}; while (length(L7) < 10) {L7="n"L7}; \
		i=index(LA1,":");Xss=substr(LA1,0,1) substr(LA1, i+1, 2); \
		i=index(LA2,":");Xse=substr(LA2,0,1) substr(LA2, i+1, 2); \
		printf "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\042\040%s\n%skm/h\n%s%%\n%skm\n%smb\n%s\n%s\n%s\n%s\n%s\n", \
		L1,L2,L3,L4,L5,L6,L7,L81,L82,L84,L83,L91,L92,L93,L94,LA1,LA2,Xss,Xse}' \
                > /opt/lcd4linux/data/weather
	cat /tmp/weather | grep '<lastBuildDate>' | awk -F'<lastBuildDate>' '{ \
		{L1=(substr($2, 6, 20))}} \
		END \
		{ printf "%s\n", \
		L1}' \
		>> /opt/lcd4linux/data/weather
else
         exit $?
fi

exit 0
