Guest User

VinDsl Weather script

a guest
Dec 22nd, 2012
263
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # weather.sh
  4.  
  5. #######################################################
  6. # @Author: Hardik Mehta <hard.mehta@gmail.com> (2009-2010)
  7. # @Hackor: VinDSL <perfect.pecker@excite.co.uk> (2011)
  8. #
  9. # @version: 2.0 optimized/code cleanup (VinDSL)
  10. # removed line feed variables from most style sheets (VinDSL)
  11. # embedded line feeds allow .conkyrc to control spacing/alignment
  12. # added current forecast date in weather.xslt (VinDSL)
  13. # @version: 1.2 rewrite to accommodate metric stats (VinDSL)
  14. # added fcTemp.Metric.xslt (VinDSL)
  15. # added fcTemp.Imperial.xslt (VinDSL)
  16. # added conditionsTempBig.Metric.xslt (VinDSL)
  17. # added conditionsTempBig.Imperial.xslt (VinDSL)
  18. # @version: 1.1 code cleanup/added icon (VinDSL)
  19. # @version: 1.0 added fcTempToday.xslt (VinDSL)
  20. # @version: 0.9 added currentConditionBig.xslt (VinDSL)
  21. # @version: 0.8 bug fix (VinDSL)
  22. # @version: 0.7 code cleanup (VinDSL)
  23. # @version: 0.6 added currentTempBig (VinDSL)
  24. # @version: 0.5 added comments (VinDSL)
  25. # @version: 0.4 code cleanup (VinDSL)
  26. # @version: 0.3 initial hack (VinDSL)
  27. # @version: 0.2 optimized
  28. # @version: 0.1 basic script
  29. #
  30. #######################################################
  31. # .conkyrc format:
  32. # ${execi 1800 /path/to/weather/weather.sh "location" option } Note: do not use ~/weather/weather.sh shortcut
  33. #
  34. # .conkyrc examples:
  35. # ${execi 1800 /home/user/weather/weather.sh "Munich,Germany" } - prints current conditions
  36. # ${execi 1800 /home/user/weather/weather.sh "Munich,Germany" ccb} - prints current condition (big format)
  37. # ${execi 1800 /home/user/weather/weather.sh "Munich,Germany" ccti} - prints today's high/low temps (imperial)
  38. # ${execi 1800 /home/user/weather/weather.sh "Munich,Germany" cctm} - prints today's high/low temps (metric)
  39. # ${execi 1800 /home/user/weather/weather.sh "Munich,Germany" cp} - prints symbol for current condition
  40. # ${execi 1800 /home/user/weather/weather.sh "Munich,Germany" ctbi} - prints current temp (big format - imperial)
  41. # ${execi 1800 /home/user/weather/weather.sh "Munich,Germany" ctbm} - prints current temp (big format - metric)
  42. # ${execi 1800 /home/user/weather/weather.sh "Munich,Germany" dl} - prints list of days for forecast
  43. # ${execi 1800 /home/user/weather/weather.sh "Munich,Germany" fcp} - prints symbols for forecast conditions
  44. # ${execi 1800 /home/user/weather/weather.sh "Munich,Germany" fcti} - prints forecast high/low temps (imperial)
  45. # ${execi 1800 /home/user/weather/weather.sh "Munich,Germany" fctm} - prints forecast high/low temps (metric)
  46. #
  47. # NOTE: ++ Be sure to comment/uncomment the necessary CURLURL string
  48. # for Imperial or Metric weather stats (as noted below).
  49. #
  50. # ++ Always delete your old weatherInfo.xml cache file, when
  51. # changing between Imperial and Metric weather stats.
  52. #
  53. #######################################################
  54. ####
  55. ##
  56. # Don't get xml file if created within 30 minutes
  57. UPDATE=1800
  58.  
  59. # Local cURL location (cURL required)
  60. CURLCMD="/usr/bin/curl -s"
  61.  
  62. # Local xsltcmd location (xsltproc required)
  63. XSLTCMD=/usr/bin/xsltproc
  64.  
  65. # Where this script and the XSLT lives
  66. RUNDIR=`dirname $0`
  67.  
  68. # Where the weatherInfo.xml file lives
  69. weather_xml="${RUNDIR}/weatherInfo.xml"
  70.  
  71. # Location ID string
  72. # Parsed from .conkyrc
  73. LOCID=$1
  74.  
  75. # Conditions string
  76. # Parsed from weatherInfo.xml
  77. CONDITIONS=$2
  78.  
  79. # cURL the Google Weather API (Imperial - Fahrenheit)
  80. CURLURL="http://www.google.com/ig/api?weather=${LOCID}&hl=en"
  81.  
  82. # cURL the Google Weather API (Metric - Celsius)
  83. # CURLURL="http://www.google.com/ig/api?weather=${LOCID}&hl=en-gb"
  84.  
  85. # The XSLT to use when translating the response
  86. if [ "$CONDITIONS" = "ccb" ];
  87. then
  88. XSLT=$RUNDIR/currentConditionBig.xslt
  89. elif [ "$CONDITIONS" = "ctbi" ];
  90. then
  91. XSLT=$RUNDIR/currentTempBig.Imperial.xslt
  92. elif [ "$CONDITIONS" = "ctbm" ];
  93. then
  94. XSLT=$RUNDIR/currentTempBig.Metric.xslt
  95. elif [ "$CONDITIONS" = "cp" ];
  96. then
  97. XSLT=$RUNDIR/conditions.xslt
  98. elif [ "$CONDITIONS" = "dl" ];
  99. then
  100. XSLT=$RUNDIR/fcDayList.xslt
  101. elif [ "$CONDITIONS" = "fcp" ];
  102. then
  103. XSLT=$RUNDIR/fcConditions.xslt
  104. elif [ "$CONDITIONS" = "ctti" ];
  105. then
  106. XSLT=$RUNDIR/fcTempToday.Imperial.xslt
  107. elif [ "$CONDITIONS" = "cttm" ];
  108. then
  109. XSLT=$RUNDIR/fcTempToday.Metric.xslt
  110. elif [ "$CONDITIONS" = "fcti" ];
  111. then
  112. XSLT=$RUNDIR/fcTemp.Imperial.xslt
  113. elif [ "$CONDITIONS" = "fctm" ];
  114. then
  115. XSLT=$RUNDIR/fcTemp.Metric.xslt
  116. else
  117. XSLT=$RUNDIR/weather.xslt
  118. fi
  119.  
  120. ################################################################
  121. # You probably don't need to modify anything below this point. #
  122. ################################################################
  123. ####
  124. ## Optimization.
  125. # We cache the xml in a file and don't get the file
  126. # from internet if it is less than 30 minutes old
  127.  
  128. function get_file ()
  129. {
  130. # echo "get file called"
  131. # check if the file exists
  132. if [ -e $weather_xml ];
  133. then
  134. size=`stat -c %s $weather_xml`
  135. if [ $size -ge 1000 ];
  136. then
  137. now=`date -u +%s`
  138. created=`stat -c %Y $weather_xml`
  139. age=`expr $now - $created`
  140. else
  141. age=`expr $UPDATE + 1`
  142. fi
  143. else
  144. # if the file doesn't exist create it
  145. # and set the age older than update time
  146. touch $weather_xml
  147. age=`expr $UPDATE + 1`
  148. fi
  149. # echo $age
  150. # get the file if it is older than update time
  151. if [ $age -ge $UPDATE ];
  152. then
  153. $CURLCMD -o $weather_xml "$CURLURL"
  154. fi
  155. # echo "get file ended"
  156. }
  157.  
  158. get_file
  159.  
  160. eval "$XSLTCMD $XSLT $weather_xml"
RAW Paste Data