-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathwwenetwork.py
More file actions
129 lines (112 loc) · 5.44 KB
/
Copy pathwwenetwork.py
File metadata and controls
129 lines (112 loc) · 5.44 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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)