Page 2 of 12 FirstFirst 1234 ... LastLast
Results 11 to 20 of 111

Thread: Function to show weather forecast in the terminal

  1. #11
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Function to show weather forecast in the terminal

    Quote Originally Posted by ian-weisser View Post
    That service seems to be deprecated.
    Wunderground.com has moved to a purchased-key model for api access. See http://www.wunderground.com/weather/api/

    For US locations, I use data from the National Weather Service. No api key required. For how I do it, see http://cheesehead-techblog.blogspot....nfo-feeds.html

    The Ubuntu Phone weather app gets worldwide data from OpenWeatherMap.org. It also uses an api key for access, but the price is $0. See http://openweathermap.org/API#forecast
    Thanks for the update Ian.

    OK so I got a free key from the weather underground. Their developer level is free.
    I can now download an xml file of the current conditions in my area.
    The thing is I don't know how to go from that to showing in the terminal my current conditions and forecast.
    Ideally something like the function that started this thread, but also including the current conditions would be great.

    I really appreciate the help guys!

    Here is the xml file:

    Code:
    
    
    <response>
    	<version>0.1</version>
    	<termsofService>http://www.wunderground.com/weather/api/d/terms.html</termsofService>
    	<features>
    		<feature>conditions</feature>
    	</features>
    		<current_observation>
    		<image>
    		<url>http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png</url>
    		<title>Weather Underground</title>
    		<link>http://www.wunderground.com</link>
    		</image>
    		<display_location>
    		<full>Falun, Sweden</full>
    		<city>Falun</city>
    		<state></state>
    		<state_name>Sweden</state_name>
    		<country>SN</country>
    		<country_iso3166>SE</country_iso3166>
    		<zip>00000</zip>
    		<magic>6</magic>
    		<wmo>02435</wmo>
    		<latitude>60.59999847</latitude>
    		<longitude>15.63333321</longitude>
    		<elevation>116.00000000</elevation>
    		</display_location>
    		<observation_location>
    		<full>Kämparvet, Falun, DALARNA</full>
    		<city>Kämparvet, Falun</city>
    		<state>DALARNA</state>
    		<country>SWEDEN</country>
    		<country_iso3166>SE</country_iso3166>
    		<latitude>60.606194</latitude>
    		<longitude>15.639073</longitude>
    		<elevation>50 ft</elevation>
    		</observation_location>
    		<estimated>
    		</estimated>
    		<station_id>IDALARNA23</station_id>
    		<observation_time>Last Updated on October 6, 6:47 PM CEST</observation_time>
    		<observation_time_rfc822>Sun, 06 Oct 2013 18:47:04 +0200</observation_time_rfc822>
    		<observation_epoch>1381078024</observation_epoch>
    		<local_time_rfc822>Sun, 06 Oct 2013 18:47:05 +0200</local_time_rfc822>
    		<local_epoch>1381078025</local_epoch>
    		<local_tz_short>CEST</local_tz_short>
    		<local_tz_long>Europe/Stockholm</local_tz_long>
    		<local_tz_offset>+0200</local_tz_offset>
    		<weather>Clear</weather>
    		<temperature_string>54.3 F (12.4 C)</temperature_string>
    		<temp_f>54.3</temp_f>
    		<temp_c>12.4</temp_c>
    		<relative_humidity>74%</relative_humidity>
    		<wind_string>From the SE at 2.0 MPH</wind_string>
    		<wind_dir>SE</wind_dir>
    		<wind_degrees>135</wind_degrees>
    		<wind_mph>2.0</wind_mph>
    		<wind_gust_mph>0</wind_gust_mph>
    		<wind_kph>3.2</wind_kph>
    		<wind_gust_kph>0</wind_gust_kph>
    		<pressure_mb>1012</pressure_mb>
    		<pressure_in>29.89</pressure_in>
    		<pressure_trend>0</pressure_trend>
    		
    		<dewpoint_string>46 F (8 C)</dewpoint_string>
    		<dewpoint_f>46</dewpoint_f>
    		<dewpoint_c>8</dewpoint_c>
    		
    		
    		<heat_index_string>NA</heat_index_string>
    		<heat_index_f>NA</heat_index_f>
    		<heat_index_c>NA</heat_index_c>
    		
    		
    		<windchill_string>NA</windchill_string>
    		<windchill_f>NA</windchill_f>
    		<windchill_c>NA</windchill_c>
    		
            <feelslike_string>54.3 F (12.4 C)</feelslike_string>
            <feelslike_f>54.3</feelslike_f>
            <feelslike_c>12.4</feelslike_c>
    		<visibility_mi>6.2</visibility_mi>
    		<visibility_km>10.0</visibility_km>
    		<solarradiation></solarradiation>
    		<UV>-1</UV>
    		<precip_1hr_string>0.00 in ( 0 mm)</precip_1hr_string>
    		<precip_1hr_in>0.00</precip_1hr_in>
    		<precip_1hr_metric> 0</precip_1hr_metric>
    		<precip_today_string>0.00 in (0 mm)</precip_today_string>
    		<precip_today_in>0.00</precip_today_in>
    		<precip_today_metric>0</precip_today_metric>
    		
    		
    		
    		<icon>clear</icon>
    		<icon_url>http://icons-ak.wxug.com/i/c/k/nt_clear.gif</icon_url>
    		<forecast_url>http://www.wunderground.com/global/stations/02435.html</forecast_url>
            
            <history_url>http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IDALARNA23</history_url>
            
    		<ob_url>http://www.wunderground.com/cgi-bin/findweather/getForecast?query=60.606194,15.639073</ob_url>
    	</current_observation>
    
    
       
    </response>
    Thank you,
    GG -----------

  2. #12
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Function to show weather forecast in the terminal

    time for some python

    Code:
    #!/usr/bin/env python
    
    import sys
    import urllib2
    import xml.etree.ElementTree as ET
    
    data = ( (  "Location: {0}", [ "display_location/full" ] ),
             (  "Time: {0}", [ "observation_time_rfc822" ] ),
             (  "Conditions: {0}", [ "weather" ] ),
             ( u"Temperature: {0}\u00b0C ({1}\u00b0F), feels like {2}\u00b0C ({3}\u00b0F)", [ "temp_c", "temp_f", "feelslike_c", "feelslike_f" ] ),
             (  "Wind: {0} km/h, from the {1}", [ "wind_kph", "wind_dir" ] ),
             (  "Pressure: {0} hPa", [ "pressure_mb" ] ),
             ( u"Dew point: {0}\u00b0C ({1}\u00b0F)", [ "dewpoint_c", "dewpoint_f" ] ) )
    
    #url = "https://???"
    #req = urllib2.Request( url )
    #f = urllib2.urlopen( req )
    f = open( "test.xml", "r" )
    xmlroot = ET.fromstring( f.read() )
    
    for x in data:
      print x[0].format( *[ xmlroot.findall(".//"+i)[0].text for i in x[1] ] )
    Code:
    $ ./weather.py 
    Location: Falun, Sweden
    Time: Sun, 06 Oct 2013 18:47:04 +0200
    Conditions: Clear
    Temperature: 12.4°C (54.3°F), feels like 12.4°C (54.3°F)
    Wind: 3.2 km/h, from the SE
    Pressure: 1012 hPa
    Dew point: 8°C (46°F)

    it uses a local file with the data now, but in comments there is a code for gobbling the data directly from the server that should work.
    data describes lines of output: format string + [ fields/xml tags ] which are supposed to go into {integer} placeholders so it's trivial to come up with your own format.
    I don't see forecast there so...
    Last edited by Vaphell; October 6th, 2013 at 10:25 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #13
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Function to show weather forecast in the terminal

    I am speechless.
    I was mucking around with sed and you pull a really cool script out like it's nothing.
    By comparing the xml with your python script I think I can kind of see what you are doing.

    I tried with the forecast xml file and mucked it up a bit.

    Code:
    
    
    <response>
    	<version>0.1</version>
    	<termsofService>http://www.wunderground.com/weather/api/d/terms.html</termsofService>
    	<features>
    		<feature>forecast</feature>
    	</features>
    	<forecast>
    		<txt_forecast>
    		<date>2:00 PM CEST</date>
    		<forecastdays>
    		<forecastday>
    		<period>0</period>
    		<icon>partlycloudy</icon>
    		<icon_url>http://icons-ak.wxug.com/i/c/k/partlycloudy.gif</icon_url>
    		<title>Sunday</title>
    		<fcttext><![CDATA[Overcast in the morning, then partly cloudy. High of 57F. Winds from the SSW at 5 to 10 mph.]]></fcttext>
    		<fcttext_metric><![CDATA[Overcast in the morning, then partly cloudy. High of 14C. Winds from the SSW at 10 to 15 km/h.]]></fcttext_metric>
    		<pop>0</pop>
    		</forecastday>
    		<forecastday>
    		<period>1</period>
    		<icon>mostlycloudy</icon>
    		<icon_url>http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif</icon_url>
    		<title>Sunday Night</title>
    		<fcttext><![CDATA[Mostly cloudy in the evening, then overcast. Fog overnight. Low of 45F. Winds from the WSW at 5 to 15 mph.]]></fcttext>
    		<fcttext_metric><![CDATA[Mostly cloudy in the evening, then overcast. Fog overnight. Low of 7C. Breezy. Winds from the WSW at 10 to 20 km/h.]]></fcttext_metric>
    		<pop>20</pop>
    		</forecastday>
    		<forecastday>
    		<period>2</period>
    		<icon>mostlycloudy</icon>
    		<icon_url>http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif</icon_url>
    		<title>Monday</title>
    		<fcttext><![CDATA[Mostly cloudy. High of 61F. Winds from the WSW at 5 to 10 mph.]]></fcttext>
    		<fcttext_metric><![CDATA[Mostly cloudy. High of 16C. Breezy. Winds from the WSW at 10 to 20 km/h.]]></fcttext_metric>
    		<pop>0</pop>
    		</forecastday>
    		<forecastday>
    		<period>3</period>
    		<icon>cloudy</icon>
    		<icon_url>http://icons-ak.wxug.com/i/c/k/cloudy.gif</icon_url>
    		<title>Monday Night</title>
    		<fcttext><![CDATA[Overcast with  rain after midnight. Low of 50F. Winds from the WSW at 5 to 10 mph.]]></fcttext>
    		<fcttext_metric><![CDATA[Overcast with  rain after midnight. Low of 10C. Winds from the WSW at 5 to 15 km/h.]]></fcttext_metric>
    		<pop>60</pop>
    		</forecastday>
    		<forecastday>
    		<period>4</period>
    		<icon>rain</icon>
    		<icon_url>http://icons-ak.wxug.com/i/c/k/rain.gif</icon_url>
    		<title>Tuesday</title>
    		<fcttext><![CDATA[Overcast with  a chance of rain in the afternoon. Fog early. High of 59F. Winds from the SW at 5 to 15 mph. Chance of rain 40%.]]></fcttext>
    		<fcttext_metric><![CDATA[Overcast with  a chance of rain in the afternoon. Fog early. High of 15C. Breezy. Winds from the SW at 10 to 20 km/h. Chance of rain 40%.]]></fcttext_metric>
    		<pop>40</pop>
    		</forecastday>
    		<forecastday>
    		<period>5</period>
    		<icon>mostlycloudy</icon>
    		<icon_url>http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif</icon_url>
    		<title>Tuesday Night</title>
    		<fcttext><![CDATA[Overcast. Fog overnight. Low of 46F. Winds from the SW at 10 to 15 mph.]]></fcttext>
    		<fcttext_metric><![CDATA[Overcast. Fog overnight. Low of 8C. Breezy. Winds from the SW at 15 to 20 km/h.]]></fcttext_metric>
    		<pop>0</pop>
    		</forecastday>
    		<forecastday>
    		<period>6</period>
    		<icon>cloudy</icon>
    		<icon_url>http://icons-ak.wxug.com/i/c/k/cloudy.gif</icon_url>
    		<title>Wednesday</title>
    		<fcttext><![CDATA[Overcast. High of 57F. Winds from the SW at 10 to 15 mph.]]></fcttext>
    		<fcttext_metric><![CDATA[Overcast. High of 14C. Breezy. Winds from the SW at 20 to 25 km/h.]]></fcttext_metric>
    		<pop>0</pop>
    		</forecastday>
    		<forecastday>
    		<period>7</period>
    		<icon>cloudy</icon>
    		<icon_url>http://icons-ak.wxug.com/i/c/k/cloudy.gif</icon_url>
    		<title>Wednesday Night</title>
    		<fcttext><![CDATA[Overcast with  a chance of rain after midnight. Fog overnight. Low of 37F. Winds from the WSW at 5 to 10 mph shifting to the NE after midnight.]]></fcttext>
    		<fcttext_metric><![CDATA[Overcast with  a chance of rain after midnight. Fog overnight. Low of 3C. Winds from the WSW at 5 to 15 km/h shifting to the NE after midnight.]]></fcttext_metric>
    		<pop>20</pop>
    		</forecastday>
    		</forecastdays>
    		</txt_forecast>
    		<simpleforecast>
    		<forecastdays>
    		<forecastday>
    		<date>
      <epoch>1381093200</epoch>
      <pretty_short>11:00 PM CEST</pretty_short>
      <pretty>11:00 PM CEST on October 06, 2013</pretty>
      <day>6</day>
      <month>10</month>
      <year>2013</year>
      <yday>278</yday>
      <hour>23</hour>
      <min>00</min>
      <sec>0</sec>
      <isdst>1</isdst>
      <monthname>October</monthname>
      <weekday_short>Sun</weekday_short>
      <weekday>Sunday</weekday>
      <ampm>PM</ampm>
      <tz_short>CEST</tz_short>
      <tz_long>Europe/Stockholm</tz_long>
    </date>
    					<period>1</period>
    					<high>
    						<fahrenheit>57</fahrenheit>
    						<celsius>14</celsius>
    					</high>
    					<low>
    						<fahrenheit>45</fahrenheit>
    						<celsius>7</celsius>
    					</low>
    					<conditions>Partly Cloudy</conditions>
    					
    					<icon>partlycloudy</icon>
    					<icon_url>http://icons-ak.wxug.com/i/c/k/partlycloudy.gif</icon_url>
    					<skyicon>partlycloudy</skyicon>
    					<pop>0</pop>
    					<qpf_allday>
    						<in>0.00</in>
    						<mm>0.0</mm>
    					</qpf_allday>
    					<qpf_day>
    						<in>0.00</in>
    						<mm>0.0</mm>
    					</qpf_day>
    					<qpf_night>
    						<in>0.00</in>
    						<mm>0.0</mm>
    					</qpf_night>
    					<snow_allday>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_allday>
    					<snow_day>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_day>
    					<snow_night>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_night>
    					<maxwind>
    						<mph>9</mph>
    						<kph>14</kph>
    						<dir>SW</dir>
    						<degrees>220</degrees>
    					</maxwind>
    					<avewind>
    						<mph>8</mph>
    						<kph>13</kph>
    						<dir>SW</dir>
    						<degrees>225</degrees>
    					</avewind>
    					<avehumidity>78</avehumidity>
    					<maxhumidity>97</maxhumidity>
    					<minhumidity>66</minhumidity>
    				</forecastday>
    				
    				<forecastday>
    					<date>
      <epoch>1381179600</epoch>
      <pretty_short>11:00 PM CEST</pretty_short>
      <pretty>11:00 PM CEST on October 07, 2013</pretty>
      <day>7</day>
      <month>10</month>
      <year>2013</year>
      <yday>279</yday>
      <hour>23</hour>
      <min>00</min>
      <sec>0</sec>
      <isdst>1</isdst>
      <monthname>October</monthname>
      <weekday_short>Mon</weekday_short>
      <weekday>Monday</weekday>
      <ampm>PM</ampm>
      <tz_short>CEST</tz_short>
      <tz_long>Europe/Stockholm</tz_long>
    </date>
    					<period>2</period>
    					<high>
    						<fahrenheit>61</fahrenheit>
    						<celsius>16</celsius>
    					</high>
    					<low>
    						<fahrenheit>50</fahrenheit>
    						<celsius>10</celsius>
    					</low>
    					<conditions>Mostly Cloudy</conditions>
    					
    					<icon>mostlycloudy</icon>
    					<icon_url>http://icons-ak.wxug.com/i/c/k/mostlycloudy.gif</icon_url>
    					<skyicon>mostlycloudy</skyicon>
    					<pop>0</pop>
    					<qpf_allday>
    						<in>0.00</in>
    						<mm>0.0</mm>
    					</qpf_allday>
    					<qpf_day>
    						<in>0.00</in>
    						<mm>0.0</mm>
    					</qpf_day>
    					<qpf_night>
    						<in>0.05</in>
    						<mm>1.3</mm>
    					</qpf_night>
    					<snow_allday>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_allday>
    					<snow_day>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_day>
    					<snow_night>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_night>
    					<maxwind>
    						<mph>10</mph>
    						<kph>16</kph>
    						<dir>WSW</dir>
    						<degrees>252</degrees>
    					</maxwind>
    					<avewind>
    						<mph>7</mph>
    						<kph>11</kph>
    						<dir>WSW</dir>
    						<degrees>248</degrees>
    					</avewind>
    					<avehumidity>86</avehumidity>
    					<maxhumidity>100</maxhumidity>
    					<minhumidity>67</minhumidity>
    				</forecastday>
    				
    				<forecastday>
    					<date>
      <epoch>1381266000</epoch>
      <pretty_short>11:00 PM CEST</pretty_short>
      <pretty>11:00 PM CEST on October 08, 2013</pretty>
      <day>8</day>
      <month>10</month>
      <year>2013</year>
      <yday>280</yday>
      <hour>23</hour>
      <min>00</min>
      <sec>0</sec>
      <isdst>1</isdst>
      <monthname>October</monthname>
      <weekday_short>Tue</weekday_short>
      <weekday>Tuesday</weekday>
      <ampm>PM</ampm>
      <tz_short>CEST</tz_short>
      <tz_long>Europe/Stockholm</tz_long>
    </date>
    					<period>3</period>
    					<high>
    						<fahrenheit>59</fahrenheit>
    						<celsius>15</celsius>
    					</high>
    					<low>
    						<fahrenheit>46</fahrenheit>
    						<celsius>8</celsius>
    					</low>
    					<conditions>Rain</conditions>
    					
    					<icon>rain</icon>
    					<icon_url>http://icons-ak.wxug.com/i/c/k/rain.gif</icon_url>
    					<skyicon>cloudy</skyicon>
    					<pop>40</pop>
    					<qpf_allday>
    						<in>0.05</in>
    						<mm>1.3</mm>
    					</qpf_allday>
    					<qpf_day>
    						<in>0.00</in>
    						<mm>0.0</mm>
    					</qpf_day>
    					<qpf_night>
    						<in>0.00</in>
    						<mm>0.0</mm>
    					</qpf_night>
    					<snow_allday>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_allday>
    					<snow_day>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_day>
    					<snow_night>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_night>
    					<maxwind>
    						<mph>11</mph>
    						<kph>18</kph>
    						<dir>SW</dir>
    						<degrees>230</degrees>
    					</maxwind>
    					<avewind>
    						<mph>9</mph>
    						<kph>14</kph>
    						<dir>SW</dir>
    						<degrees>223</degrees>
    					</avewind>
    					<avehumidity>91</avehumidity>
    					<maxhumidity>100</maxhumidity>
    					<minhumidity>82</minhumidity>
    				</forecastday>
    				
    				<forecastday>
    					<date>
      <epoch>1381352400</epoch>
      <pretty_short>11:00 PM CEST</pretty_short>
      <pretty>11:00 PM CEST on October 09, 2013</pretty>
      <day>9</day>
      <month>10</month>
      <year>2013</year>
      <yday>281</yday>
      <hour>23</hour>
      <min>00</min>
      <sec>0</sec>
      <isdst>1</isdst>
      <monthname>October</monthname>
      <weekday_short>Wed</weekday_short>
      <weekday>Wednesday</weekday>
      <ampm>PM</ampm>
      <tz_short>CEST</tz_short>
      <tz_long>Europe/Stockholm</tz_long>
    </date>
    					<period>4</period>
    					<high>
    						<fahrenheit>57</fahrenheit>
    						<celsius>14</celsius>
    					</high>
    					<low>
    						<fahrenheit>37</fahrenheit>
    						<celsius>3</celsius>
    					</low>
    					<conditions>Overcast</conditions>
    					
    					<icon>cloudy</icon>
    					<icon_url>http://icons-ak.wxug.com/i/c/k/cloudy.gif</icon_url>
    					<skyicon>cloudy</skyicon>
    					<pop>0</pop>
    					<qpf_allday>
    						<in>0.00</in>
    						<mm>0.0</mm>
    					</qpf_allday>
    					<qpf_day>
    						<in>0.00</in>
    						<mm>0.0</mm>
    					</qpf_day>
    					<qpf_night>
    						<in>0.08</in>
    						<mm>2.0</mm>
    					</qpf_night>
    					<snow_allday>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_allday>
    					<snow_day>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_day>
    					<snow_night>
    						<in>0</in>
    						<cm>0</cm>
    					</snow_night>
    					<maxwind>
    						<mph>14</mph>
    						<kph>22</kph>
    						<dir>SW</dir>
    						<degrees>232</degrees>
    					</maxwind>
    					<avewind>
    						<mph>10</mph>
    						<kph>16</kph>
    						<dir>SW</dir>
    						<degrees>230</degrees>
    					</avewind>
    					<avehumidity>83</avehumidity>
    					<maxhumidity>100</maxhumidity>
    					<minhumidity>64</minhumidity>
    				</forecastday>
    				
    			</forecastdays>
    		</simpleforecast>
    	</forecast>   
        
    </response>
    Thank you,
    GG -----------

  4. #14
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Function to show weather forecast in the terminal

    weather.py
    Code:
    #!/usr/bin/env python
    
    import re
    import sys
    import urllib2
    import xml.etree.ElementTree as ET
    
    # http://misc.flogisoft.com/bash/tip_colors_and_formatting
    styles = { '[END]'    : '\033[0m',
               '[B]'      : '\033[1m',
               '[RED]'    : '\033[91m',           
               '[GREEN]'  : '\033[92m',
               '[YELLOW]' : '\033[93m',
               '[BLUE]'   : '\033[94m',
               '[MAGENTA]': '\033[95m',
               '[CYAN]'   : '\033[96m' }
    
    field_rgx = r'\{[^}]*[a-z]+[^}]*\}'
    
    # open xmls with data
    cxml = ET.fromstring( open( 'current.xml', 'r' ).read() )
    fxml = ET.fromstring( open( 'forecast.xml', 'r' ).read() )
    
    format = {}
    data = []
    
    # load formatting data           
    for ln in open( 'weather_format.txt', 'r' ):
      ln = ln.rstrip()
      if ln.startswith('!'):
        f_id = ln.lstrip('! ')
        format[f_id] = []
        data.append( ( f_id, cxml if 'current' in f_id else fxml ) )
      elif ln.startswith('#'):
        continue
      else:
        for s in styles:
          ln = ln.replace( s, styles[s] )
        fields = [ x.strip('{}') for x in re.findall( field_rgx, ln ) ]
        for i in range( 0, len(fields) ):
          ln = re.sub( field_rgx, '{{{0}}}'.format(i), ln, 1 )
        format[f_id].append( [ ln, fields[:] ] )
    
    
    # print info
    for (k,xml) in data:
      for x in xml.findall( k ):
        for ln in format[k]:
          print ln[0].format( *[ x.findtext( './/'+i ) for i in ln[1] ] )
      print
    sample weather_format.txt:
    Code:
    # current weather
    ! .//current_observation
       [CYAN]Location:[END] [B][GREEN]{display_location/full}[END]
           [CYAN]Time:[END] {observation_time_rfc822}
     [MAGENTA]Conditions:[END] [B][YELLOW]{weather}[END]
    [MAGENTA]Temperature:[END] [B][YELLOW]{temp_c}°C[END] ({temp_f}°F), feels like [B][YELLOW]{feelslike_c}°C[END] ({feelslike_f}°F)
           [RED]Wind:[END] [B][YELLOW]{wind_kph} km/h[END], from the {wind_dir}
       [MAGENTA]Pressure:[END] [B][YELLOW]{pressure_mb} hPa[END]
      [BLUE]Dew point:[END] [B][YELLOW]{dewpoint_c}°C[END] ({dewpoint_f}°F)
    # forecast in text format
    ! .//txt_forecast/forecastdays/forecastday
    [CYAN]{title}:[END]
      {fcttext_metric}
    # forecast in simple format
    ! .//simpleforecast/forecastdays/forecastday
    [MAGENTA]{weekday}, {date/monthname} {date/day}, {date/year}:[END]
      {conditions}, high {high/celsius}°C, low {low/celsius}°C
      Winds {avewind/kph} km/h from the {avewind/dir}, max {maxwind/kph} km/h

    so the forecast appears to be in 2 formats in parallel: text based (text_forecast) and field based (simpleforecast). Just for kicks i tapped into both of them
    Also i extracted the formatting data to external txt file so it's easier to manipulate than by tweaking tuples and lists by hand in the code.

    # lines are comments so ignored
    ! XXX are headers of templates and point to portions of xml on which their templates are based. {} markers indicate which xml fields will fill the blanks. The file format is pretty straightforward and for bonus points i added colors and bold
    Last edited by Vaphell; October 7th, 2013 at 04:08 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #15
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Function to show weather forecast in the terminal

    THANK YOU!!

    I was going to ask how to incorporate downloading the xml data into the python script, but I just put the curl -s <url> command in a bash script.
    Vaphell, you are amazing.
    You are in Poland right? If you ever come to Sweden, let me know. I owe you a beer!

    Here is a screen shot of my weather.
    http://screencloud.net/v/BUAf
    Last edited by GrouchyGaijin; October 7th, 2013 at 08:03 AM.
    Thank you,
    GG -----------

  6. #16
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Function to show weather forecast in the terminal

    you should change these random colors in the current conditions section to something more sane, that was a showcase/test You can reformat it, or plug another data, just look at xml fields and try to add some.

    where does that lame #None come from? I must have missed something :/

    I was going to ask how to incorporate downloading the xml data into the python script
    it would be something like this
    Code:
    current_url = 'https://???'
    forecast_url = 'https://???'
    # open xmls with data
    cxml = urllib2.urlopen( urllib2.Request( current_url ) ).read()
    fxml = urllib2.urlopen( urllib2.Request( forecast_url ) ).read()
    You are in Poland right? If you ever come to Sweden, let me know. I owe you a beer!
    me+Sweden is quite probable, my homie (3x the programmer i am btw ) works in Karlskrona and said i should visit him some time. That free beer would be nice, i wouldn't want to expose myself to your 1st world prices too much and bankrupt myself
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #17
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Function to show weather forecast in the terminal

    Anytime you come, let me know. The offer for a brew is real.
    The #none appeared when I commented out the longer text forecast and kept only the simple forecast.
    Thank you,
    GG -----------

  8. #18
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Function to show weather forecast in the terminal

    When I tried to add the name of the weather station to the output, it crashed because ascii could not display <city>Kämparvet, Falun</city>.
    Is it possible to use uni code? or maybe substitute with sed the ä for an a?
    Thank you,
    GG -----------

  9. #19
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Function to show weather forecast in the terminal

    in theory that part of cfg file should be totally ignored if commented out

    i changed this part a bit (removed print that separated sections and added strip() to deal with cases where the value is padded with spaces for whatever reason and ruins formatting
    Code:
    for (k,xml) in data:
      for x in xml.findall( k ):
        for ln in format[k]:
          print ln[0].format( *[ x.findtext( './/'+i ).strip() for i in ln[1] ] )
    with this:
    Code:
    # current weather
    ! .//current_observation
         [CYAN]Location:[END] [B][GREEN]{display_location/full}[END]
             [CYAN]Time:[END] [B][GREEN]{observation_time_rfc822}[END]
       [CYAN]Conditions:[END] [B][YELLOW]{weather}[END]
      [CYAN]Temperature:[END] [B][YELLOW]{temp_c}°C[END], feels like [B][YELLOW]{feelslike_c}°C[END]
             [CYAN]Wind:[END] [B][YELLOW]{wind_kph} km/h[END], from the {wind_dir}
    [CYAN]Precipitation:[END] [B][YELLOW]{precip_1hr_metric} mm[END] in 1hr, [B][YELLOW]{precip_today_metric} mm[END] today
         [CYAN]Pressure:[END] [B][YELLOW]{pressure_mb} hPa[END]
         [CYAN]Humidity:[END] [B][YELLOW]{relative_humidity}[END]
        [CYAN]Dew point:[END] [B][YELLOW]{dewpoint_c}°C[END]
       [CYAN]Visibility:[END] [B][YELLOW]{visibility_km} km[END]
    
    # forecast in text format
    #! .//txt_forecast/forecastdays/forecastday
    #[CYAN]{title}:[END]
    #  {fcttext_metric}
    # forecast in simple format
    ! .//simpleforecast/forecastdays/forecastday
    [YELLOW]{weekday}, {date/monthname} {date/day}, {date/year}:[END]
      [BLUE]{conditions}, high {high/celsius}°C, low {low/celsius}°C
      Winds {avewind/kph} km/h from the {avewind/dir}, max {maxwind/kph} km/h[END]
    i get this, obviously in bold/color:
    Code:
         Location: Falun, Sweden
             Time: Sun, 06 Oct 2013 18:47:04 +0200
       Conditions: Clear
      Temperature: 12.4°C, feels like 12.4°C
             Wind: 3.2 km/h, from the SE
    Precipitation: 0 mm in 1hr, 0 mm today
         Pressure: 1012 hPa
         Humidity: 74%
        Dew point: 8°C
       Visibility: 10.0 km
    
    Sunday, October 6, 2013:
      Partly Cloudy, high 14°C, low 7°C
      Winds 13 km/h from the SW, max 14 km/h
    Monday, October 7, 2013:
      Mostly Cloudy, high 16°C, low 10°C
      Winds 11 km/h from the WSW, max 16 km/h
    Tuesday, October 8, 2013:
      Rain, high 15°C, low 8°C
      Winds 14 km/h from the SW, max 18 km/h
    Wednesday, October 9, 2013:
      Overcast, high 14°C, low 3°C
      Winds 16 km/h from the SW, max 22 km/h
    Last edited by Vaphell; October 7th, 2013 at 02:39 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  10. #20
    Join Date
    Oct 2010
    Location
    The United States
    Beans
    843
    Distro
    Ubuntu

    Re: Function to show weather forecast in the terminal

    I keep getting the unicode error. It doesn't like the ä in the station name.
    Thank you,
    GG -----------

Page 2 of 12 FirstFirst 1234 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •