Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add weather api using Yahoo Weather

  • Loading branch information...
commit 984a0b9f226f0b8fd4abdae45585a5c162420303 1 parent 4c355fd
@kyleconroy kyleconroy authored
Showing with 39 additions and 13 deletions.
  1. +1 −1  app.yaml
  2. +19 −12 docs/dynamic_apps.rst
  3. +19 −0 util.py
View
2  app.yaml
@@ -6,7 +6,7 @@ threadsafe: true
handlers:
- url: /.*
- script: main.app
+ script: main_other.app
- url: /favicon\.ico
static_files: favicon.ico
View
31 docs/dynamic_apps.rst
@@ -162,28 +162,32 @@ verbs in the previous sections.
Here we turn our response object into a string using Python's built in string
function. We then write this string to the response object.
-Personalized Greetings
-----------------------
+The Weather Channel
+-------------------
-So far all our responses look the same. We're just returning static TwiML as
-we did that the last two sessions. Now we're about to show you why building a
-dynamic application is so powerful. First, a simple example.
+So far all our responses look the same. We're just returning static TwiML as we
+did that the last two sessions. Now we're about to show you why building a
+dynamic application is so powerful. Instead of saying read a message, we'll
+inform the caller of the current weather in his or her zipcode.
.. code-block:: python
- :emphasize-lines: 10
+ :emphasize-lines: 2,10,11,14,15
import webapp2
+ from util import current_weather
from twilio import twiml
- from util import weather_for_zip
class HelloWorld(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = "application/xml"
+ weather = current_weather(self.request.get("FromZip", "94117"))
+ city = self.request.get("FromCity", "San Francisco")
+
response = twiml.Response()
- weather = weather_for_zip(self.request.get("FromZip"))
- response.say("Hello " + weather)
+ response.say("Hello from " + city)
+ response.say("The current weather is " + weather)
self.response.write(str(response))
app = webapp2.WSGIApplication([
@@ -197,7 +201,8 @@ Now visit your page. You'll see the following message.
<?xml version="1.0" encoding="UTF-8"?>
<Response>
- <Say>Hello </Say>
+ <Say>Hello from San Francisco</Say>
+ <Say>The current weather is Partly Cloudy, 65 F</Say>
</Response>
@@ -286,7 +291,9 @@ Once it's deployed, take the URL for your application,
for your Twilio phone number. Configuring Twilio numbers is covered in more
detail in :ref:`configure-number`
-*Note: Since we have only implemented the GET endpoint, be sure to configure
-your number to use the GET method instead of the default POST*
+.. note::
+
+ Since we have only implemented the GET endpoint, be sure to configure your
+ number to use the GET method instead of the default POST*
Now give it a call. You should hear your custom message. Hooray!
View
19 util.py
@@ -1,6 +1,8 @@
import os
from twilio.util import TwilioCapability
+from google.appengine.api import urlfetch
from google.appengine.ext.webapp import template
+import xml.etree.ElementTree as ET
def render_template(rel_path, parameters=None, folder="templates"):
@@ -22,3 +24,20 @@ def generate_token(account_sid, auth_token, application_sid):
# Allow access to the Call-in ApplicationSid we created
capability.allow_client_outgoing(application_sid)
return capability.generate()
+
+
+def current_weather(zipcode):
+ """
+ Return the current weather for a zip code
+ """
+ url = "http://xml.weather.yahoo.com/forecastrss?p=" + str(zipcode)
+ resp = urlfetch.fetch(url)
+ feed = ET.fromstring(resp.content)
+
+ description = feed.find(".//{http://xml.weather.yahoo.com/ns/rss/1.0}condition")
+
+ if description is None:
+ return "unknown"
+
+ return "{}, {} degrees".format(description.attrib.get('text', 'Sunny'),
+ description.attrib.get('temp', '85'))
Please sign in to comment.
Something went wrong with that request. Please try again.