Skip to content

Instantly share code, notes, and snippets.

@fz6
Created March 16, 2017 00:45
Show Gist options
  • Save fz6/6335229f9bb552befb0826707e0b0fab to your computer and use it in GitHub Desktop.
Save fz6/6335229f9bb552befb0826707e0b0fab to your computer and use it in GitHub Desktop.
closest_sunny_episode
import requests, datetime
DOW_DICT = {"monday": 0, "tuesday": 1, "wednesday": 2, "thursday": 3, "friday": 4, "saturday": 5, "sunday": 6}
NOW = datetime.datetime(day=10, month=3, year=2017, hour=6, minute=23) #now()
r = requests.get("https://gist.githubusercontent.com/billychasen/61442308c54b7f06c255d270912beb27/raw/169dd4c5c1be0ea9500877420bfa3c50e4aec0b1/alwaysunny.csv")
buff = r.text
episode_list = [] # each entry is dict in format of {"episode": dt}
def convert_to_dt(timestamp, day_word):
current_dow = NOW.weekday()
episode_dow = DOW_DICT[day_word]
# We want to figure out what day this episode took place, as if it occured this week
episode_day_this_week = NOW.day - current_dow + episode_dow
# parse timestamp
timestamp_dt = datetime.datetime.strptime(timestamp, "%I:%M%p")
episode_dt = datetime.datetime(day=episode_day_this_week,
month=NOW.month, year=NOW.year,
hour=timestamp_dt.hour,
minute=timestamp_dt.minute)
return episode_dt
shortest_diff = None
shortest_diff_ep = None
shortest_diff_text = None
for line in buff.splitlines():
ep = line.split(",")[0]
timestamp = line.split(",")[1]
day_word = line.split(",")[2]
if timestamp and day_word:
dt = convert_to_dt(timestamp, day_word)
diff = NOW - dt
diff_in_seconds = diff.total_seconds()
# We don't care if it's before or after, but make it a positive number for accurate comparisons
if diff_in_seconds < 0:
diff_in_seconds = 0 - diff_in_seconds
if diff_in_seconds < shortest_diff or not shortest_diff:
shortest_diff = diff_in_seconds
shortest_diff_ep = ep
shortest_diff_text = "%s on a %s" % (timestamp, day_word.title())
print "The closest episde to your current day and time is: %s" % shortest_diff_ep
print shortest_diff_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment