1 Replies - 15053 Views - Last Post: 28 January 2013 - 08:45 AM Rate Topic: -----

#1 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 124
  • Joined: 08-May 11

Using Tropo & Python to Have the Phone Tell Us the Weather

Posted 01 January 2013 - 03:38 AM

Alright well I decided to make a tutorial. In this tutorial we'll set up an automatic phone system, and, using Python, design it to answer the phone and tell you the weather in your area based on the zip code you give the program.

Step 1:

Set up an account on tropo. I'm assuming if you're reading this tutorial you have the computer savvy to set up an account on a website so I won't waste your time with that.

Step 2: Writing the Script

Now that you have your account all ready to use, log in and go to 'Your Hosted Files' and click 'Create New File.' (Note: It may be [much] easier to write out these files in your favorite Python writing program than to enter them directly onto tropo) Name the file what you want to and move down to the area named 'File Text'. First step is import the modules we need. After which we'll create a dictionary that we'll use later to convert the abbreviated days of the week to full names of the week and then a function that will do that work. Enter this. . .
import re
import urllib2
from xml.dom import minidom, Node
day_of_week_dic = {
"Mon": "Monday",
"Tue": "Tuesday",
"Wed": "Wednesday",
"Thu": "Thursday",
"Fri": "Friday",
"Sat": "Saturday",
"Sun ": "Sunday ",
"mph":"Milesperhour "
}

def replace_words(text, word_dic):
	rc = re.compile('|'.join(map(re.escape, word_dic)))

	def translate(match):
		return word_dic[match.group(0)]
	return rc.sub(translate, text)



This is pretty self explanatory. The function has a built in function which substitutes and returns the value that we want in the dictionary. Now we'll move on to my favorite part, which is scraping the web page.
def weather():

	result = ask("Enter or say your five digit zip code",{"choices":"[5 DIGITS]", 
        	"timeout":30,
        	"attempts":3,
        	"onBadChoice": lambda event : say("I'm sorry,  I didn't understand that.")})
	urlRead = urllib2.urlopen('http://xml.weather.yahoo.com/forecastrss/%s_f.xml'%result.value)
	xml = minidom.parse(urlRead)
	if xml:
		for channelNode in xml.documentElement.childNodes:
			if channelNode.nodeName == 'channel':
				for itemNode in channelNode.childNodes:
					if itemNode.nodeName == 'item':
						for yWeatherNode in itemNode.childNodes:
							if yWeatherNode.nodeName == 'yweather:forecast':
								day = replace_words(yWeatherNode.getAttribute('day'), day_of_week_dic)
								low = yWeatherNode.getAttribute('low')
								high = yWeatherNode.getAttribute('high')
								condition = yWeatherNode.getAttribute('text')
								say("For "+day+", there is a low of "+low+" degrees and a high of "+high+" degrees. The condition is "+condition+".")
			
						
weather()
hangup()



This may be confusing at first glance but I assure you it isn't hard to understand. The first part that may look unfamiliar is the 'ask' command. That command is a built-in tropo command. The first option is the question that we're going to ask, the next three are pretty self-explanatory. Next, what we're doing is taking the xml document (urlRead), looking for a node called 'channel', then a node called 'item', then a node called 'yweather:forecast'. When we get to yweather:forecast we grab the day, low, high, and condition from it then use tropo's 'say' command to have our program output whatever we want it to.

At the bottom we call weather and then tell our phone to hangup once weather is done executing.

Step 3: Getting the phone number and testing it!

After entering all that, please don't forget to click save file or update file at the bottom of the text box you entered the code in! Ok, now that you have it saved, click on 'Your Applications'. Click 'Create New Application', name your app whatever you'd like to and click 'Hosted File' link and then click the 'Map existing file to this application' link. From there just select map next to the script we created in the last step. After that, click 'Create Application'.

Now you can use the numbers given or add your own and call up the number. If all goes as planned, it should work (I had to wait about 5 minutes for mine to become ready).

Is This A Good Question/Topic? 1
  • +

Replies To: Using Tropo & Python to Have the Phone Tell Us the Weather

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Using Tropo & Python to Have the Phone Tell Us the Weather

Posted 28 January 2013 - 08:45 AM

That, sir, is one cool application. Nice work. Approved!

This post has been edited by Dogstopper: 28 January 2013 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1