-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutil.py
43 lines (34 loc) · 1.43 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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"):
"""
Takes a path relative to the templates/ folder and
an optional parameter for variables to render to the template.
"""
parameters = parameters if parameters is not None else {}
path = os.path.join(os.path.dirname(__file__), folder, rel_path)
return template.render(path, parameters)
def generate_token(account_sid, auth_token, application_sid):
"""
Create a capability token given Twilio account credentials
and an application sid.
"""
capability = TwilioCapability(account_sid, auth_token)
# 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'))