Skip to content

Adding WWE Network plugin #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions plugins/wwenetwork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# WWE Network Schedule plugin by craisins in 2014

import time
import calendar
import json
from util import hook, http, web, text

db_ready = False

schedule_url = "http://epg.media.net.wwe.com/epg_small.json"

def db_init(db):
"""Check that our db has the wwenetwork table, create it if not."""
global db_ready
if not db_ready:
# no reason to keep any old table because we don't do anything with the history
db.execute("drop table if exists wwenetwork")
db.execute("create table if not exists wwenetwork(datetime, title, description, alert_start default 0, primary key (datetime))")
db.commit()
db_ready = True

@hook.command("scrapewwe")
def get_schedule(inp, nick='', db=None, notice=None, message=None):
db_init(db)
print "wwenetwork: Scraping Network Schedule"
try:
page = http.get(schedule_url)
schedule = json.loads(page)
schedule = schedule["events"]
right_now = time.gmtime()
right_now_timestamp = calendar.timegm(right_now)

for show in schedule:
title = show["title"]
desc = show["big_blurb"]
time_gmt = show["dates_and_times"]["start_time_gmt"]

timestamp = calendar.timegm(time.strptime(time_gmt, "%Y-%m-%dT%H:%M:%S+0000"))

results = db.execute("select * from wwenetwork where datetime = ?", (timestamp,)).fetchall()
# don't load anything older than 5 hours because it's not worth it
if not results and (timestamp > right_now_timestamp - (60*60*5)):
db.execute("insert into wwenetwork(datetime, title, description) values(?, ?, ?)", (timestamp, title, desc))
db.commit()
# debug message
print "wwenetwork: Added {} at {}".format(title, timestamp)
# anything older than right now should be marked as alerted because we don't want to again
db.execute("update wwenetwork set alert_start = 1 where datetime < ?", (right_now_timestamp,))
db.commit()

except (http.HTTPError, http.URLError):
return "Couldn't scrape the WWE Network site."

@hook.command("search")
@hook.command
def wwesearch(inp, nick='', db=None, notice=None, message=None):
timestamp_gmt = calendar.timegm(time.gmtime())
original_input = inp
inp = inp.lower()
results = db.execute("select * from wwenetwork where datetime > ? and (title like ? or description like ?) order by datetime asc", (timestamp_gmt, '%'+inp+'%', '%'+inp+'%')).fetchall()
if results:
counter = 0
for result in results:
if counter > 2:
break
datetime, title, desc, alert_Start = result
datetime = time.strftime("%Y-%m-%d %I:%M%p %Z", time.localtime(datetime))
message(u"{}: \x02{}\x02: {}".format(datetime, title, desc))
counter += 1
else:
message("No results found for \"{}\"".format(original_input))

@hook.command("now")
@hook.command
def wwenow(inp, nick='', db=None, notice=None, message=None):
timestamp_gmt = calendar.timegm(time.gmtime())
# get the last event that started
result = db.execute("select * from wwenetwork where datetime < ? order by datetime desc limit 1", (timestamp_gmt,)).fetchone()
if result:
datetime, title, desc, alert_start = result
message(u"\x02{}\x02: {}".format(title, desc))
return


@hook.command("schedule")
def wweschedule(inp, nick='', db=None, notice=None, message=None):
time_gmt = time.gmtime()
timestamp_gmt = calendar.timegm(time_gmt)
# get the last event that started
result = db.execute("select * from wwenetwork where datetime < ? order by datetime desc limit 1", (timestamp_gmt,)).fetchone()
output = ""
if result:
datetime, title, desc, alert_start = result
message(u"Now: \x02{}\x02: {}".format(title, desc))
# get the next two events
results = db.execute("select * from wwenetwork where datetime > ? order by datetime asc limit 2", (timestamp_gmt,)).fetchall()
if results:
for result in results:
datetime, title, desc, alert_start = result
message(u"At {}: \x02{}\x02: {}".format(time.strftime("%I:%M%p %Z", time.localtime(datetime)), title, desc))
return

@hook.event("JOIN")
@hook.singlethread
def crond(inp, db=None, message=None):
# start a new thread
print "Starting wwenetwork:crond"
get_schedule("Scraping WWE Network for new shows.", '', db=db)
# keep track of when you're scraping the network site
last_scraped = calendar.timegm(time.gmtime())
while True:
time_gmt = time.gmtime()
timestamp_gmt = calendar.timegm(time_gmt)

# if it's been 12 hours since scraping the json file, do it again
if timestamp_gmt > (last_scraped + (60*60*12)):
get_schedule("Scraping WWE Network for new shows.")
last_scraped = timestamp_gmt

# if you're within one minute of a new event starting, let everyone know
results = db.execute("select * from wwenetwork where datetime <= ? and datetime >= ? and alert_start = 0", (timestamp_gmt + 60, timestamp_gmt - 60)).fetchall()
if results:
for result in results:
datetime, title, desc, alert_start = result
db.execute("update wwenetwork set alert_start = 1 where datetime = ?", (datetime,))
db.commit()
message(u"Starting Now: \x02{}\x02: {}".format(title, desc))

time.sleep(60)