<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Juliana Peña &#187; code</title>
	<atom:link href="http://julianapena.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://julianapena.com</link>
	<description></description>
	<lastBuildDate>Sun, 18 Sep 2011 09:34:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Code snippet: Get a weather condition using Python and Google Weather API</title>
		<link>http://julianapena.com/2010/02/code-snippet-get-a-weather-condition-using-python-and-google-weather-api/</link>
		<comments>http://julianapena.com/2010/02/code-snippet-get-a-weather-condition-using-python-and-google-weather-api/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 17:09:17 +0000</pubDate>
		<dc:creator>Juliana Peña</dc:creator>
				<category><![CDATA[Code snippet]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://julianapena.com/2010/02/code-snippet-get-a-weather-condition-using-python-and-google-weather-api/</guid>
		<description><![CDATA[Here’s a simple Python code snippet for finding the weather condition of any given city using Google’s Weather API. It’s also published on GitHub if you want to clone it. import urllib2 def getWeather(city): #create google weather api url url = "http://www.google.com/ig/api?weather=" + urllib2.quote(city) try: # open google weather api url f = urllib2.urlopen(url) except: <a href="http://julianapena.com/2010/02/code-snippet-get-a-weather-condition-using-python-and-google-weather-api/"> read more <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here’s a simple Python code snippet for finding the weather condition of any given city using Google’s Weather API. It’s also <a href="http://gist.github.com/308912">published on GitHub</a> if you want to clone it.</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:8f6cfd24-50f5-4569-a63a-46d12fc1a212" class="wlWriterEditableSmartContent" style="margin: 0px; display: inline; float: none; padding: 0px;">
<pre class="brush: python;">import urllib2

def getWeather(city):

    #create google weather api url
    url = "http://www.google.com/ig/api?weather=" + urllib2.quote(city)

    try:
        # open google weather api url
        f = urllib2.urlopen(url)
    except:
        # if there was an error opening the url, return
        return "Error opening url"

    # read contents to a string
    s = f.read()

    # extract weather condition data from xml string
    weather = s.split("&lt;current_conditions&gt;&lt;condition data=\"")[-1].split("\"")[0]

    # if there was an error getting the condition, the city is invalid
    if weather == "&lt;?xml version=":
        return "Invalid city"

    #return the weather condition
    return weather

def main():
    while True:
        city = raw_input("Give me a city: ")
        weather = getWeather(city)
        print(weather)

if __name__ == "__main__":
    main()</pre>
</div>
<p><strong>Update: </strong>GitHub is awesome because it allows very easy forking. <a href="http://gist.github.com/beaumartinez">Beau Martínez</a> has made a <a href="http://gist.github.com/309222">fork</a> of my script that includes Python 3 support, XML parsing instead of RegEx searching, and temperature reporting.</p>
]]></content:encoded>
			<wfw:commentRss>http://julianapena.com/2010/02/code-snippet-get-a-weather-condition-using-python-and-google-weather-api/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
