-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
316 lines (251 loc) · 10.3 KB
/
main.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/python3
import requests
import re
import datetime
import html
import json
from epg_sources.teleboy import teleboy
from epg_sources.init7 import init7
import validators
class channel_item:
id: str
lang: str
display_name: str
icon: str
class programm_item:
start: datetime
stop: datetime
channel: str
icon: str
title: str
country: str
desc: str
sub_title: str
credits: dict
category: str
episode_num: str
date: int
length: int
def __main__():
print("[*] Getting/parsing Init7 tvchannels.m3u playlist")
channels = get_channel_list()
print("[*] Getting EPG data from Init7")
init7Obj = init7()
init7_epg = []
try:
init7_epg_raw = init7Obj.get_epg(7 * 24 * 60 * 60, 7 * 24 * 60 * 60)
init7_epg = match_init7_epg(channels, init7_epg_raw)
except:
print("[*] Failed. Continue processing other sources.")
print("[*] Getting past EPG data from teleboy.ch")
teleboyObj = teleboy()
teleboy_raw = ""
try:
teleboy_raw = teleboyObj.get_epg_from_past_by_duration(7 * 24 * 60)
except:
print("[*] Failed. Continue processing other sources.")
teleboy_epg_past = match_teleboy_epg(channels, teleboy_raw)
print("[*] Getting EPG data from teleboy.ch")
teleboy_raw = ""
try:
teleboy_raw = teleboyObj.get_epg_by_duration(7 * 24 * 60)
except:
print("[*] Failed. Continue processing other sources.")
teleboy_epg = match_teleboy_epg(channels, teleboy_raw)
# generate the xml for the channels
channels_xmltv = channels_to_xmltv(channels)
# generate tv7_teleboy_epg.xml
if len(teleboy_epg) > 0:
with open("tv7_teleboy_epg.xml", "w+") as w:
w.write(
'<?xml version="1.0" encoding="UTF-8" ?><tv>'
f"{channels_xmltv}{programms_to_xmltv(teleboy_epg)}</tv>"
)
if len(teleboy_epg_past) > 0:
with open("tv7_teleboy_epg_past.xml", "w+") as w:
w.write(
'<?xml version="1.0" encoding="UTF-8" ?><tv>'
f"{channels_xmltv}{programms_to_xmltv(teleboy_epg_past)}</tv>"
)
if len(init7_epg) > 0:
with open("tv7_init7_epg.xml", "w+") as w:
w.write(
'<?xml version="1.0" encoding="UTF-8" ?><tv>'
f"{channels_xmltv}{programms_to_xmltv(init7_epg)}</tv>"
)
# generate tv7_epg.xml
full_epg = []
full_epg.extend(teleboy_epg)
full_epg.extend(teleboy_epg_past)
full_epg.extend(init7_epg)
programms_xmltv = programms_to_xmltv(full_epg)
if len(full_epg) > 0:
with open("tv7_epg.xml", "w+") as w:
w.write(
'<?xml version="1.0" encoding="UTF-8" ?><tv>'
f"{channels_xmltv}{programms_xmltv}</tv>"
)
def get_channel_list():
tv7channel_list = requests.get("https://api.init7.net/tvchannels.m3u").text
tv7channel_list = re.sub(r"udp:\/\/.+", "", tv7channel_list)
tv7channel_list = tv7channel_list.replace("\n", "")
tv7channel_list = tv7channel_list.replace("#EXTM3U", "")
tv7channel_list = tv7channel_list.split("#EXTINF:0 ")
channel_list = []
for channel in tv7channel_list:
channel_obj = {}
if not channel == "":
for attribute in channel.split(" "):
if "=" in attribute:
name = attribute.split("=")[0]
value = attribute.split("=")[1].replace('"', "")
else:
value = attribute
if name == "group-title":
channel_obj["lang"] = value
elif name == "tvg-logo":
channel_obj["icon"] = value
# not all channels have tvg-name so do own stuff....
if "display_name" not in channel_obj:
channel_obj["display_name"] = channel.split(", ")[1]
channel_obj["id"] = gen_channel_id_from_name(
channel_obj["display_name"]
)
channel_list.append(channel_obj)
return channel_list
def gen_channel_id_from_name(channel_name):
return (
channel_name.lower()
.replace("hd", "")
.replace("schweiz", "")
.replace("(", "")
.replace(")", "")
.replace(" ", "")
)
def find_channel_by_id(id, channel_list):
for channel in channel_list:
if id == channel["id"]:
return True
return False
def match_init7_epg(channel_list, init7_epg):
print(
f"[*] Matching init7.ch EPG data ({str(len(init7_epg))}"
f" programms to {str(len(channel_list))} channels)"
)
programms = []
for programm in init7_epg:
channel_id = gen_channel_id_from_name(programm["channel"])
if find_channel_by_id(channel_id, channel_list):
programms.append(programm)
return programms
def match_teleboy_epg(channel_list, teleboy_epg):
print(
f"[*] Matching teleboy.ch EPG data ({str(len(teleboy_epg))}"
f" programms to {str(len(channel_list))} channels)"
)
mapping = json.loads(open("./mappings/teleboy.json", "r").read())
programms = []
matched_channels = set()
for programm in teleboy_epg:
channel_id = gen_channel_id_from_name(programm["station"])
if channel_id in mapping:
channel_id = mapping[channel_id]
if find_channel_by_id(channel_id, channel_list):
matched_channels.add(channel_id)
programm_matched = {
"start": programm["begin"],
"stop": programm["end"],
"channel": channel_id,
"icon": programm["image"],
"title": programm["title"],
}
if "subtitle" in programm and programm["subtitle"]:
programm_matched["sub_title"] = programm["subtitle"]
if "country" in programm and programm["country"]:
programm_matched["country"] = programm["country"]
if "desc" in programm and programm["desc"]:
programm_matched["desc"] = programm["desc"]
if (
"episode_num" in programm
and "season_num" in programm
and programm["episode_num"]
and programm["season_num"]
):
programm_matched["episode_num"] = (
f"S{str(programm['season_num'])} E{str(programm['episode_num'])}"
)
elif "episode_num" in programm and programm["episode_num"]:
programm_matched["episode_num"] = str(programm["episode_num"])
if "year" in programm and programm["year"]:
programm_matched["date"] = programm["year"]
programms.append(programm_matched)
print(f"[✓] Matched {str(len(matched_channels))} teleboy.ch channels")
return programms
def programms_to_xmltv(programms):
print(f"[*] Generating XML for {str(len(programms))} programms")
programms_xml = ""
for programm in programms:
programm_xml = ""
programm_xml = (
f'{programm_xml}<programme start="'
f"{programm['start'].strftime('%Y%m%d%H%M%S %z')}\" "
f"stop=\"{programm['stop'].strftime('%Y%m%d%H%M%S %z')}\" channel=\"{programm['channel']}\">"
)
if "title" in programm:
programm_xml = (
f"{programm_xml}<title>{html.escape(programm['title'] or '')}</title>"
)
if "sub_title" in programm:
programm_xml = f"{programm_xml}<sub-title>{html.escape(programm['sub_title'] or '')}</sub-title>"
if "desc" in programm:
programm_xml = f"{programm_xml}<desc lang=\"de\">{html.escape(programm['desc'] or '')}</desc>"
if "persons" in programm:
programm_xml = f"{programm_xml}<credits>"
for attrib in programm["persons"]:
if attrib == "actors":
for actor in programm["persons"]["actors"]:
programm_xml = f"{programm_xml}<actor>{actor}</actor>"
else:
programm_xml = f"{programm_xml}<{attrib}>{programm['persons'][attrib]}</{attrib}>"
programm_xml = f"{programm_xml}</credits>"
if "date" in programm:
programm_xml = f"{programm_xml}<date>{str(programm['date'])}</date>"
if "category" in programm:
programm_xml = f"{programm_xml}<category lang=\"de\">{html.escape(programm['category'] or '')}</category>"
if "categories" in programm:
for category in programm["categories"]:
programm_xml = f"{programm_xml}<category lang=\"de\">{html.escape(category or '')}</category>"
if "duration" in programm:
programm_xml = f"{programm_xml}<length units=\"seconds\">{str(int(programm['duration']))}</length>"
if (
"icon" in programm
and programm["icon"] != ""
and validators.url(programm["icon"])
):
programm_xml = f"{programm_xml}<icon src=\"{programm['icon']}\" />"
if "country" in programm:
programm_xml = f"{programm_xml}<country>{html.escape(programm['country'] or '')}</country>"
if "episode_num" in programm:
if "episode_num_system" in programm:
programm_xml = f"{programm_xml}<episode-num system=\"{programm['episode_num_system']}\">{programm['episode_num']}</episode-num>"
else:
programm_xml = f"{programm_xml}<episode-num system=\"onscreen\">{programm['episode_num']}</episode-num>"
programm_xml = f"{programm_xml}</programme>"
programms_xml = programms_xml + programm_xml
return programms_xml
def channels_to_xmltv(channel_list):
print(f"[*] Generating XML for {str(len(channel_list))} channels")
channels_xml = ""
for channel in channel_list:
channel_xml = (
f"<channel id=\"{channel['id']}\">"
f"<display-name lang=\"de\">{channel['display_name']}</display-name>"
f"<display-name lang=\"fr\">{channel['display_name']}</display-name>"
f"<display-name lang=\"it\">{channel['display_name']}</display-name>"
)
if "icon" in channel:
channel_xml = f"{channel_xml}<icon src=\"{channel['icon']}\" />"
channel_xml = f"{channel_xml}</channel>"
channels_xml = channels_xml + channel_xml
return channels_xml
__main__()