Skip to content

Instantly share code, notes, and snippets.

@wridgers
Created October 15, 2012 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wridgers/3895688 to your computer and use it in GitHub Desktop.
Save wridgers/3895688 to your computer and use it in GitHub Desktop.
import android
import datetime
import json
import urllib
droid = android.Android()
# Configuration options
name = "YOUR_NAME"
weatherSearch = "A_CODE" # UK post code, US zip code, lat/lon
now = datetime.datetime.now()
def getGreeting(x):
if x.hour >= 0 and x.hour < 12:
return "morning"
if x.hour >= 12 and x.hour < 18:
return "afternoon"
if x.hour >= 18 and x.hour < 24:
return "evening"
def getSuffix(x):
if 4 <= x.day <= 20 or 24 <= x.day <= 30:
return "th"
else:
return ["st", "nd", "rd"][day % 10 - 1]
ones = ["", "one","two","three","four","five","six","seven","eight","nine"]
teen = ["", "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]
tens = ["","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"]
def numberToWords(x):
if x < 0:
return "minus %s" % numberToWords(x*-1)
elif x >= 0 and x < 10:
return ones[x]
elif 10 <= x <= 19:
return teen[x-9]
else:
u = x % 10
t = (x % 100)/10
output = tens[t]
if t > 0:
output = "%s %s" % (output, numberToWords(u))
return output
# say a greeting
greeting = "Good %s %s." % ( getGreeting(now), name )
print greeting
# say the time
if now.minute == 0:
minString = "o clock"
else:
minString = numberToWords(now.minute)
timeString = now.strftime("{H}, {M}, on %A the %d{S} of %B")
timeString = timeString.replace("{H}", numberToWords(now.hour))
timeString = timeString.replace("{M}", minString)
timeString = timeString.replace("{S}", getSuffix(now))
theTime = "The time is %s." % (timeString)
print theTime
# getting the weather
params = urllib.urlencode({'uac': 'Qg2GxYO2.J', 'output': 'json', 'query': weatherSearch, 'temp_unit': 'c'})
f = urllib.urlopen("http://www.myweather2.com/developer/forecast.ashx?%s" % params)
raw = f.read()
weather = json.loads(raw)
curTemp = weather['weather']['curren_weather'][0]['temp']
curText = weather['weather']['curren_weather'][0]['weather_text']
dayMaxTemp = weather['weather']['forecast'][0]['day_max_temp']
nightMinTemp = weather['weather']['forecast'][0]['night_min_temp']
dayText = weather['weather']['forecast'][0]['day'][0]['weather_text']
nightText = weather['weather']['forecast'][0]['night'][0]['weather_text']
currentWeather = "The current weather is %s, %s degrees with" % (curText, numberToWords(int(curTemp)))
print currentWeather
maxAndMinTemps = "expected highs of %s degrees and lows of %s degrees." % (numberToWords(int(dayMaxTemp)), numberToWords(int(nightMinTemp)))
print maxAndMinTemps
forecast = "The forecast weather is %s during the day and %s at night." % (dayText, nightText)
print forecast
# say everything!
droid.ttsSpeak("%s %s %s %s %s" % (greeting, theTime, currentWeather, maxAndMinTemps, forecast))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment