#@TecOEd
#Weather Parser reader from Yahoo

#!/usr/bin/env python

#http://developer.yahoo.com/weather/developers api overview

import time
import feedparser
import os

# Talking Raspberry Pi, based on code by Rollcode.com

import sys, subprocess, urllib, time
 
def getSpeech(phrase):
    googleAPIurl = "http://translate.google.com/translate_tts?tl=en&"
    param = {'q': phrase}
    data = urllib.urlencode(param)
    googleAPIurl += data # Combine the data from the Google API
    return googleAPIurl
 
def raspberryTalk(text): # This will call mplayer and will play the sound
    subprocess.call(["mplayer",getSpeech(text)], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

raspberryTalk("Welcome to the Weather Bot")
time.sleep(0.2)

while True:
#pulls all the data down
    rss_link ='http://weather.yahooapis.com/forecastrss?w=44418&u=c' #add your area code
    d = feedparser.parse(rss_link)


    #Parse data in various elements
    location = d.feed.yweather_location #returns the location data from yahoo
    city =location['city'] # looks for the city from the location data

    atmosphere = d.feed.yweather_atmosphere # returns the atmosphere data from yahoo 
    humidity = atmosphere['humidity']

    astronomy = d.feed.yweather_astronomy #returns astronomy data from yahoo
    sunrise = astronomy['sunrise'] #returns the sunrise time
    sunset = astronomy['sunset'] #returns the sunset time

    wind = d.feed.yweather_wind
    chill = wind['chill']

    #summary
    summary = d.entries[0].summary # returns the full weather summary for area code
    #but requires spliting 
    #print summary

    #prints weather elements
    #print""
    time.sleep(0.5)
    #print "Weather for "+city
    raspberryTalk("Weather for " +city)
    time.sleep(0.5)
    
    #print "Sunrise for "+city, sunrise
    raspberryTalk("Sunrise for "+city +sunrise)
    time.sleep(0.5)

    #print "Sunset for "+city, sunset
    raspberryTalk("Sunset for "+city +sunset)
    #print""
    time.sleep(0.5)

    #print "Humidity for "+city, humidity
    raspberryTalk("Humidity for "+city +humidity)              
    #print""
    time.sleep(0.5)
                  
    #print "Wind chill for "+city, chill, "degrees"
    raspberryTalk("Wind chill for "+city +chill +"degrees")
    time.sleep(0.5)
    #print""

    raspberryTalk("Weather provided by TeCoEd")

   
