Recommended Posts

t1sq.png

 

I wrote what is mostly my own weather code for geektool, cause I wasn't satisfied with any of the existing ones. There was some interest when I posted my desktop, so I figure I'll post the code for anyone to be able to take advantage of. The main part of the weather code uses Yahoo's XML feed, and as such should work for everyone. The alert part only works for Canadians, as it uses Environment Canada's RSS to grab the alerts (not surprising, they don't care about other countries :p )

 

The way mine works is a single main file, master.py, downloads the the XML feeds from Yahoo and Environment Canada and saves them. It then checks these files to copy the correct weather conditions image, and if needed, an alert image to the temp images. It also calculates the lunar cycle, and copies that image.

 

The other files then, if needed, pull from these cached XML files to extract the information they need. This is to minimize the number of times I'm having to hit external servers, and pull data. I have limited bandwidth, sometimes caps on usage, every little bit helps.

 

MyWeather.zip

 

REQUIRED : Feedparser for Python - https://pypi.python.org/pypi/feedparser

You will need to install this as root in terminal (or other ways if you know what you are doing.. it's not hard, but not my topic today.)

 

REQUIRED : GeekTool http://projects.tynsoe.org/en/geektool/

Unless you use something else, in which case ignore this.

 

Included in this zip is all the scripts, the images that I used ( Weather icons are FlatWhite, from http://lavana.deviantart.com/art/Flat-Weather-Icons-32021664 ), and my current cached weather. Below I will go through some of the changes that need to be made.

 

I personally run each script individually in their own script boxes through the code:

python /path/to/python/file.py

Though pasting the contents of each file into their own script windows should work too.

 

 

Images link to w_temp.png, l_temp.png, and a_temp.png (Weather Conditions, Lunar Cycle, and Alerts) and are updated regularly.

 

 

Now, lets start on the fun parts.

 

master.py :

yahoo_url = 'http://xml.weather.yahoo.com/forecastrss?w=3826&u=c'
weather_cache = '/Users/Ryoken/Documents/GeekTool/MyWeather/w_temp.xml'

gcweather_url = 'https://weather.gc.ca/rss/warning/ns-19_e.xml'
gcweather_cache = '/Users/Ryoken/Documents/GeekTool/MyWeather/gcw_temp.xml'

theme_dir = '/Users/Ryoken/Documents/GeekTool/MyWeather/Icons/FlatWhite/'
lunar_theme_dir = '/Users/Ryoken/Documents/GeekTool/MyWeather/Icons/LunarWhite/'
alert_theme_dir = '/Users/Ryoken/Documents/GeekTool/MyWeather/Icons/Alerts/'

temp_image = '/Users/Ryoken/Documents/GeekTool/MyWeather/w_temp.png'
lunar_temp_image = '/Users/Ryoken/Documents/GeekTool/MyWeather/l_temp.png'
alert_temp_image = '/Users/Ryoken/Documents/GeekTool/MyWeather/a_temp.png'

night_images = [27, 29, 31, 33, 45, 46, 47]

Obviously, first thing you will need to do is update all the paths to your location. 

 

Second thing is update yahoo_url and gcweather_url to your locations. For Yahoo, refer to http://developer.yahoo.com/weather/ for how to find your specific WOEID. Also note the u=c, that means Yahoo sends me the information is Celcius/Metric. If you want it in Imperial, either change the C to an F, or omit it all together.

As for the Environment Canada address, go to https://weather.gc.ca/canada_e.html , select your nearest city, and then go to near the bottom of the page. It will have the RSS icon, and say ATOM: Weather | Alert, you want the alert.

 

Lastly, if you want to change the icons, then you can update the urls for the themes. The alerts one is a work in progress, atm it just uses a red icons for an alert, and transparent if there isn't one.

Weather icons wise, this script will with without edits with any if the icons in the pack I linked above (there are several different colours available in that pack). If you want to use other weather icon sets, then this script does use the standard Yahoo weather conditions code, HOWEVER, this script will also calculate the lunar cycle and apply that. Most icons sets do not have images for those different phases, so you will need to remove that section of code. The condition codes that have lunar cycle images are listed in the variable "night_images", simply removing the numbers there and putting a number that doesn't ever happen, like 100, will resolve it. Or you can actually remove the code itself.

 

If you are not Canadian, or otherwise don't want the alerts, you will need to remove a few sections of code, lines  60-64, 92-108 (you can remove the globals for it too if you wish).

 

With the master.py file setup, you add it as a scipt. Personally when I add it to geektool, I check the "Override text:" box, and put in my Degrees C. I have mine set on a refresh rate of every 600s but you can adjust this as you see fit for how often you want it to hit the external servers.

 

Now that that is over, you will need to edit most of the other py files. The lunar_phase.py doesn't have anything that needs to be edited, it doesn't read from either of the xml files, it just outputs the current phase name.

All of the other ones will need to be edited to have their global variable point to the correct locations for the cached XML files, alert.py for the Enciornment Canada one, and the rest for the Yahoo Weather xml.

 

The included scripts are:

alert.py -- The active weather alert from Enviornment Canada.

current_conditions.py -- Current weather conditions.

current_temp.py -- The current tempature.

feels_temp.py -- What the tempature feels like.

lunar_phase.py -- The current lunar cycle.

sunrise_cunset.py -- The Current Sunrise/Sunset times. (If it's day, it shows sunset, if it's night, it shows sunrise).

wind.py -- Current Windpseed and cumpass direction.

master.py -- The most imporant file.

 

 

If there is additional information you want, it isn't hard to make your own scripts to pull from the cache. I shall post one as an example.

#! /usr/bin/python

import feedparser

#	Globals
weather_cache = '/Users/Ryoken/Documents/GeekTool/MyWeather/w_temp.xml'

data = feedparser.parse(weather_cache)

#	Get current conditions from Cache
conditions = data['items'][0]['yweather_condition']['text']

#	Output the current conditions
print(conditions)

This is current_conditions.py, 3 guess as to what this outputs? :)

 

Now if you go to http://xml.weather.yahoo.com/forecastrss?w=3826&u=c , or open the included cached weather information in the download, you will see exactly what information Yahoo sends you. I ignored the forecast cause I really don't care. Maybe some day I'll write something for it, but not today.

 

Now I'm pulling the text from yweather:condition, ( : gets converted to _ for feedparser ). The full contests of that is:

<yweather:condition  text="Partly Cloudy"  code="29"  temp="-14"  date="Thu, 23 Jan 2014 7:59 pm AST" />

Simply changing the ['text'] part will change what it grabs, changing it to ["code"] will return the weather code, used for the images. ["temp"] will return the current temp.

 

Other important parts to look at are:

<yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/>
<yweather:wind chill="-22"   direction="220"   speed="14.48" />
<yweather:atmosphere humidity="82"  visibility=""  pressure="1011.7"  rising="0" />
<yweather:astronomy sunrise="7:41 am"   sunset="5:09 pm"/>

All the units used in these feed. Wind Chill, Direction, speed.

One that I haven't included that might be of use, depending on the time of year and your location is Humidity, As such I would change data['items'][0]['yweather_condition']['text'] to data['items'][0]['yweather_atmosphere']['humidity']

 

 

Well there it is. I think I've wrote enough on it for most people to be able to figure it out, and expand on it if needed. If you have any questions ask/message me. I may or may not notice and respond (being honest here, I often go away for weeks/months at a time from following forums lol)

 

 

** Also, don't forget to set refresh times for all the smaller parts. Personally I have all the individual scripts update every minute or two, with the master every 10 minutes, but that's personal preference. The more often you update, the mode system load geektool uses, but with that said, even on my old MacBook Pro, the usage for this never even registers as relevant.

 

 

Hope someone finds this of use :)

Link to comment
https://www.neowin.net/forum/topic/1197867-geektool-current-weather/
Share on other sites

This topic is now closed to further replies.