-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkekg.py
301 lines (233 loc) · 8.82 KB
/
kekg.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
import json
import math
import requests
from datetime import datetime, timedelta
import pytz
from imdb import imdb_info_by_search, imdb_printout
cwd = os.path.dirname(os.path.abspath(__file__))
with open(cwd + "/kekg_memes.json", "r") as stashjson:
kekg_config = json.load(stashjson)
sports_labels = kekg_config["sports_labels"]
movies_labels = kekg_config["movies_labels"]
shows_labels = kekg_config["shows_labels"]
church_labels = kekg_config["church_labels"]
reality_numbers = kekg_config["reality_numbers"]
number_mapping = kekg_config["number_mapping"]
KEKG_URL = os.environ.get("KEKG_URL")
def fetch_kekg():
if KEKG_URL:
page = requests.get(KEKG_URL)
return json.loads(page.content)
else:
raise ValueError("KEKG_URL is not set")
def filter_channels(numbers=[], labels=[], programs=[]):
kekg_json = fetch_kekg()
channels = kekg_json["result"]["channels"]
filtered = []
for ch in channels:
number_match = not numbers or str(ch["channelnumber"]) in numbers
label_match = not labels or ch["label"] in labels
program_match = not programs or (
ch.get("broadcastnow") and ch["broadcastnow"].get("title") in programs
)
if number_match and label_match and program_match:
filtered.append(ch)
return filtered
def channel_names():
channels = filter_channels()
lines = [
'"{}": "{}",'.format(
ch.get("channelnumber"),
ch.get("channel"),
)
for ch in channels
]
return "\n".join(lines)
def good_sports(broadcast) -> bool:
on_now = broadcast.get("title")
return (
on_now.startswith("*")
and "College" not in on_now
and "High School" not in on_now
)
def sports(spam=False):
started, coming_up = starting_now(
filter_channels(labels=sports_labels), good_sports, default_now=True
)
shows = started + coming_up
return "\n{}".format(
"\n".join(program_printout(ch, br, spam) for ch, br in shows),
)
def egg():
channels = filter_channels(programs=["*College Football"])
lines = []
for ch in channels:
now = ch.get("broadcastnow")
if now:
channel = number_mapping.get(str(ch.get("channelnumber")), ch.get("label"))
on_now = now.get("title", " ")
desc = now.get("plot")
if desc.startswith("All the action"):
continue
desc = desc.replace("No. ", "#")
desc = desc.replace("St.", "Saint")
desc = desc[: desc.find(".")] if "." in desc else desc
lines.append(f"<b>{on_now[1:]}</b> - {channel}\n{desc}")
return "\n" + "\n".join(lines)
def march():
channels = filter_channels(numbers=["503","683","551","552"])
lines = []
for ch in channels:
now = ch.get("broadcastnow")
if now:
channel = number_mapping.get(str(ch.get("channelnumber")), ch.get("label"))
on_now = now.get("title", " ")
desc = now.get("plot")
desc = desc.replace("No. ", "#")
desc = desc.replace("St.", "Saint")
desc = desc[: desc.find(".")] if "." in desc else desc
lines.append(f"<b>{on_now}</b> - {channel}\n{desc}")
lines.append("<b>CBS on /kek | truTV on /alt | TNT on /alt2 | TBS on /alt3</b>")
return "\n" + "\n".join(lines)
def is_show(broadcast) -> bool:
return broadcast.get("runtime", 0) <= 60
def is_not_show(broadcast) -> bool:
return broadcast.get("runtime", 0) > 60 and not broadcast.get(
"title", ""
).startswith("*")
def runs_over_jeop(broadcast) -> bool:
start = broadcast.get("starttime")
end = broadcast.get("endtime")
if not start or not end:
return False
eastern_timezone = pytz.timezone("US/Eastern")
startdate = (
datetime.strptime(start, "%Y-%m-%d %H:%M:%S")
.replace(tzinfo=pytz.UTC)
.astimezone(eastern_timezone)
)
enddate = (
datetime.strptime(end, "%Y-%m-%d %H:%M:%S")
.replace(tzinfo=pytz.UTC)
.astimezone(eastern_timezone)
)
jeop_start = startdate.replace(hour=19, minute=5, second=0, microsecond=0)
if startdate.time() > jeop_start.time():
jeop_start += timedelta(days=1)
if jeop_start.weekday() > 4:
return False
return startdate.time() < jeop_start.time() and enddate.time() > jeop_start.time()
def good_movie(broadcast) -> bool:
return is_not_show(broadcast) and not runs_over_jeop(broadcast)
def is_genre(broadcast, genre) -> bool:
try:
return broadcast.get("genre", False) and genre in broadcast.get("genre")
except TypeError:
return False
def bad_movie(broadcast) -> bool:
return is_genre(broadcast, "Movie") and not runs_over_jeop(broadcast)
def sports_genre(broadcast) -> bool:
return is_genre(broadcast, "Sports")
def always_true(_) -> bool:
return True
def shows(spam=False):
started, coming_up = starting_now(
filter_channels(labels=shows_labels), is_show, default_now=True
)
shows = started + coming_up
return "\n{}".format(
"\n".join(program_printout(ch, br, spam) for ch, br in shows),
)
def church(spam=False):
started, coming_up = starting_now(
filter_channels(labels=church_labels), always_true, default_now=True
)
shows = started + coming_up
return "\n{}".format(
"\n".join(program_printout(ch, br, spam) for ch, br in shows),
)
def reality(spam=False):
started, coming_up = starting_now(
filter_channels(numbers=reality_numbers), always_true, default_now=True
)
shows = started + coming_up
return "\n{}".format(
"\n".join(program_printout(ch, br, spam) for ch, br in shows),
)
def movies(spam=False, imdb=False):
started, coming_up = starting_now(filter_channels(labels=movies_labels), good_movie)
movies = started + coming_up
if imdb:
printer = imdb_extra_printout
else:
printer = program_printout
return "\n{}".format(
"\n".join(printer(ch, br, spam) for ch, br in starttime_sorted(movies)),
)
def movies_alt():
started, coming_up = starting_now(filter_channels(), bad_movie)
movies = started + coming_up
return "\n{}".format(
"\n".join(program_printout(ch, br, plot=False) for ch, br in movies),
)
def sports_alt():
started, coming_up = starting_now(filter_channels(), sports_genre, default_now=True)
shows = started + coming_up
return "\n{}".format(
"\n".join(program_printout(ch, br, plot=False) for ch, br in shows),
)
def starttime_sorted(channel_broadcasts):
return sorted(channel_broadcasts, key=lambda b: b[1].get("starttime", ""))
def program_printout(channel, broadcast, plot=False):
return "<b>{}</b> - {} - {}{}".format(
broadcast.get("title"),
number_mapping.get(str(channel.get("channelnumber")), channel.get("label")),
program_timing(broadcast),
f"\n{broadcast.get('plot','')[:275]}\n" if plot else "",
)
def imdb_extra_printout(channel, broadcast, plot=True):
try:
imdb_info = imdb_info_by_search(broadcast.get("title"))
channel_time = " - {} - {}".format(
number_mapping.get(str(channel.get("channelnumber")), channel.get("label")),
program_timing(broadcast),
)
return imdb_printout(imdb_info, show_poster=False, extra_info=channel_time)
except KeyError:
return ""
def program_timing(broadcast):
try:
current_time = datetime.now(pytz.UTC)
starttime = datetime.strptime(
broadcast.get("starttime", ""), "%Y-%m-%d %H:%M:%S"
).replace(tzinfo=pytz.UTC)
if starttime > current_time:
time_until = starttime - current_time
now_remaining = str(round(time_until.total_seconds() / 60))
return "In {} minutes".format(now_remaining)
else:
now_prog = broadcast.get("progresspercentage", 50)
return "{}% done".format(math.floor(now_prog))
except ValueError:
return ""
# Returns two lists of (channel, broadcast)
def starting_now(channels, test=always_true, default_now=False):
started = []
coming_up = []
for channel in channels:
now = channel.get("broadcastnow")
next = channel.get("broadcastnext")
if now:
now_prog = now.get("progresspercentage", 50)
now_starting = now_prog < 15
now_remaining = math.floor(
((now.get("runtime", 420) * 60) - now.get("progress", 210 * 60)) / 60
)
if now_starting and test(now):
started.append((channel, now))
elif next and now_remaining <= 15 and test(next):
coming_up.append((channel, next))
elif default_now and test(now):
started.append((channel, now))
return started, coming_up