Skip to content

Instantly share code, notes, and snippets.

@dittoslash
Last active August 9, 2019 14:36
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 dittoslash/e2411d51a9b60565b892371b8b73d6a7 to your computer and use it in GitHub Desktop.
Save dittoslash/e2411d51a9b60565b892371b8b73d6a7 to your computer and use it in GitHub Desktop.
horriblesubwatch, a replacement for your worn-down F5 key
from bs4 import BeautifulSoup
import requests
import datetime
from pytz import timezone, utc
from time import sleep
#not a little cheaty
parse_days = {"Monday": 0, "Tuesday": 1, "Wednesday": 2, "Thursday": 3, "Friday": 4, "Saturday": 5, "Sunday": 6, "Monday (Today)": 0, "Tuesday (Today)": 1, "Wednesday (Today)": 2, "Thursday (Today)": 3, "Friday (Today)": 4, "Saturday (Today)": 5, "Sunday (Today)": 6}
pacific = timezone('US/Pacific')
uk = timezone('GB')
webhook = "[WEBHOOK]"
watchlist = {}
with open("watchlist", "r") as f:
for line in f.readlines():
title, role = line.split("|")
watchlist[title] = role
req = requests.get("https://horriblesubs.info/release-schedule/")
soup = BeautifulSoup(req.text, "lxml")
schedule_root = soup.find_all("div", "entry-content")[0]
zipped_elems = zip(schedule_root.find_all("h2", "weekday"), schedule_root.find_all("table")) #link the day schedules with their days
parsed_sched = []
for elem_day, elem_sched in zipped_elems: #retrieve the text from the soup instances
day = elem_day.string
shows = []
for show in elem_sched.find_all("tr", "schedule-page-item"):
showtime = show.td
showtitle = showtime.next_sibling.next_sibling
shows.append((showtime.string, showtitle.string))
parsed_sched.append((day, shows))
schedule = []
now = uk.localize(datetime.datetime.now()).astimezone(utc)
for day in parsed_sched:
nextday = datetime.datetime.now()
while nextday.weekday() != parse_days[day[0]]: #find next day the show will air on
nextday += datetime.timedelta(days=1)
for show in day[1]:
time = show[1].split(":")
release_time = nextday.replace(hour=int(time[0]), minute=int(time[1]), second=0, microsecond=0) #get naive time on correct day
release_time = pacific.localize(release_time) #set timezone to pacific time
release_time = release_time.astimezone(utc) #move timezone to utc
if release_time < now and release_time.weekday() == now.weekday():
release_time += datetime.timedelta(days=7)
print(release_time, show[0])
schedule.append((release_time, show[0]))
while True:
now = uk.localize(datetime.datetime.now()).astimezone(utc) #get current time in utc
for show in schedule:
time = show[0]
title = show[1]
if title in watchlist.keys():
delta = time - now
print(f"{int(delta.days)}d {int(delta.seconds/60/60)%24}h {int(delta.seconds/60)%60}m {delta.seconds%60}s til {title}")
if delta.seconds == 0 and delta.days == 0:
requests.post(webhook, data={"content": f"{title} has released a new episode! <@&{watchlist[title]}> (Bear in mind, HorribleSubs takes a few minutes to upload - it should be up soon!)"})
time += datetime.timedelta(days=7)
sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment