Skip to content

Instantly share code, notes, and snippets.

@relsqui
Last active October 11, 2015 04:28
Show Gist options
  • Save relsqui/3803446 to your computer and use it in GitHub Desktop.
Save relsqui/3803446 to your computer and use it in GitHub Desktop.
Morning readiness script.
#!/usr/bin/python
from urllib2 import urlopen
from json import loads
from xml.dom.minidom import parseString
from datetime import datetime, timedelta
from math import floor
TRIMET_API_KEY = "" # redacted for privacy
now = datetime.now()
print now.strftime("It is %I:%M %p on %A, %B %d.\n")
TRIMET_STOPS = [] # redacted for privacy; should be list of strings with stop IDs
f = urlopen("http://developer.trimet.org/ws/V1/arrivals?locIDs={}&appID={}".format(",".join(TRIMET_STOPS), TRIMET_API_KEY))
xml_string = f.read()
f.close()
westbound_arrivals = parseString(xml_string).getElementsByTagName('arrival')
TRIMET_STOPS = [] # redacted for privacy; should be list of strings with stop IDs
f = urlopen("http://developer.trimet.org/ws/V1/arrivals?locIDs={}&appID={}".format(",".join(TRIMET_STOPS), TRIMET_API_KEY))
xml_string = f.read()
f.close()
eastbound_arrivals = parseString(xml_string).getElementsByTagName('arrival')
def print_arrivals(arrivals):
for arrival in arrivals:
status = arrival.attributes["status"].value
if status in "estimated":
marker = " "
elif status in "scheduled":
marker = "*"
elif status in "delayed":
marker = "!"
elif status in "canceled":
continue
try:
estimated_time = arrival.attributes["estimated"].value
except KeyError:
estimated_time = arrival.attributes["scheduled"].value
estimated_time = float(estimated_time)/1000
estimated_time = datetime.fromtimestamp(estimated_time)
et_string = estimated_time.strftime("%I:%M %p")
interval = estimated_time - now
minutes = interval.seconds/60
route = arrival.attributes["route"].value
print "{}#{: >2} in {: >3} minutes (at {})".format(marker, route, minutes, et_string)
print_arrivals(westbound_arrivals)
print ""
# print_arrivals(eastbound_arrivals)
# print ""
C:\home\relsqui> ./morning.sh
October 2013
Su Mo Tu We Th Fr Sa
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
It is 12:34 PM on Sunday, October 13.
# 9 in 3 minutes (at 12:38 PM)
# 9 in 28 minutes (at 01:03 PM)
It's currently mostly cloudy, 53 degrees, with wind up to 0mph.
Sunrise is at 7:24. Sunset is at 18:27.
The forecast is partly cloudy until 2:00 PM, then clear until 11:00 PM, then fog.
High is 61 degrees at 2:00 PM. Low is 47 degrees at 12:00 AM.
#!/usr/bin/python
from urllib2 import urlopen
from json import loads
from xml.dom.minidom import parseString
from datetime import datetime, timedelta
from math import floor
WUNDERGROUND_API_KEY = "" #redacted
FORECAST_LENGTH = 12
ZIP_CODE = "" #redacted
f = urlopen("http://api.wunderground.com/api/{}/conditions/hourly/q/{}.json".format(WUNDERGROUND_API_KEY, ZIP_CODE))
json_string = f.read()
f.close
parsed_json = loads(json_string)
temperatures = {}
conditions = []
next_forecast = parsed_json['hourly_forecast'][0]
FORECAST_LENGTH = min(FORECAST_LENGTH, len(parsed_json['hourly_forecast']) - 2)
for hour in xrange(FORECAST_LENGTH):
forecast = next_forecast
next_forecast = parsed_json['hourly_forecast'][hour+1]
condition = forecast['condition'].lower()
next_condition = next_forecast['condition'].lower()
next_time = next_forecast['FCTTIME']['civil']
if condition != next_condition:
conditions.append("{} until {}".format(condition, next_time))
time = forecast['FCTTIME']['civil']
temperature = forecast['temp']['english']
if temperature not in temperatures:
temperatures[temperature] = time
# print "{} {}".format(time, condition)
if not len(conditions) or not conditions[-1].startswith(condition):
conditions.append(condition)
if len(conditions) < 4:
condition_string = "The forecast is {}.".format(", then ".join(conditions))
else:
condition_string = "The forecast is {}, then {}.".format(", ".join(conditions[:-1]), conditions[-1])
high = max(temperatures)
low = min(temperatures)
temperature_string = "High is {} degrees at {}. Low is {} degrees at {}.".format(high, temperatures[high], low, temperatures[low])
print condition_string
print temperature_string
print ""
#!/bin/bash
echo
cal
./buses.py
./weather.py
echo
./forecast.py
#!/usr/bin/python
"""
TODO:
- print what wunderground thinks the location was
- catch ambiguous locations and print the possibilities
"""
from urllib import quote_plus
from urllib2 import urlopen
from json import loads
from sys import argv
if len(argv) > 1:
location = quote_plus(" ".join(argv[1:]))
else:
location = "" # redacted for privacy; you could put "autoip" here if you run it locally
f = urlopen('http://api.wunderground.com/api/APIKEYREDACTED/conditions/astronomy/q/{}.json'.format(location))
json_string = f.read()
f.close()
parsed_json = loads(json_string)
weather = parsed_json['current_observation']
sunrise = parsed_json['moon_phase']['sunrise']
sunset = parsed_json['moon_phase']['sunset']
print("It's currently {}, {} degrees, with wind up to {}mph.".format(weather['weather'].lower(), int(weather['temp_f']), weather['wind_gust_mph']))
print("Sunrise is at {}:{}. Sunset is at {}:{}.".format(sunrise['hour'], sunrise['minute'], sunset['hour'], sunset['minute']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment