-
Notifications
You must be signed in to change notification settings - Fork 0
/
epg_generator.py
244 lines (175 loc) · 8.22 KB
/
epg_generator.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
import itertools
import os
import xml.etree.ElementTree as ET
from datetime import datetime, timedelta
from multiprocessing import Pool, cpu_count
from diskcache import Cache
from conf import OUTPUT_FOLDER, CACHE_FOLDER, EPG_FILE, DOWNLOAD_EXTRA_INFO, DAYS_TO_DOWNLOAD, DELAYS, HD_CHANNELS
from date_time import DateTime
from movistar import Movistar
class EPGGenerator(object):
def __init__(self):
self.cache = Cache(CACHE_FOLDER)
def run(self):
movistar_data = self.download_movistar_data()
channels_data, programmes_data = self.merge_movistar_data(movistar_data)
channels, programmes = self.generate_epg_data(channels_data, programmes_data)
self.dump_epg_data(channels, programmes)
self.cache.expire()
def download_movistar_data(self):
print("Downloading movistar data...")
channels = Movistar.get_channels()
return [
Movistar.get_programation(
(datetime.now() + timedelta(days=days)).strftime("%Y-%m-%d"),
channels,
)
for days in range(-1, DAYS_TO_DOWNLOAD)
]
def merge_movistar_data(self, data):
data_channels = []
data_programmes = []
for dt in data:
data_channels.extend(dt["channels"])
for dt in data:
data_programmes.extend(dt["channelsProgram"][0])
data_programmes.extend(dt["channelsProgramDayBefore"][0])
channels = list({v["cod_cadena_tv"]: v for v in data_channels}.values())
programmes = list({v["cod_evento_rejilla"]: v for v in data_programmes}.values())
return channels, programmes
def create_channels(self, channel_data):
channels = [self.create_channel(channel_data)]
if channel_data["cod_cadena_tv"] in HD_CHANNELS:
channel = dict(channels[0]) # We clone the original channel
channel["name"] = f"{channel['name']} HD"
channel["code"] = f"{channel['code']} HD"
channels.append(channel)
delays = DELAYS.get(channel_data["cod_cadena_tv"], 0)
for delay in range(1, delays + 1):
channel = dict(channels[0]) # We clone the original channel
channel["name"] = f"{channel['name']}-{delay}h"
channel["code"] = f"{channel['code']}-{delay}h"
channels.append(channel)
return channels
def create_channel(self, channel):
return {
"name": channel["des_cadena_tv"],
"code": channel["cod_cadena_tv"],
"logo": Movistar.get_channel_logo(channel["cod_cadena_tv"])
}
def create_programmes(self, programme_data):
programmes = [self.create_programme(programme_data)]
if programme_data["cod_cadena_tv"] in HD_CHANNELS:
programme = dict(programmes[0]) # We clone the original programme
programme["channel"] = f"{programme['channel']} HD"
programmes.append(programme)
delays = DELAYS.get(programme_data["cod_cadena_tv"], 0)
for delay in range(1, delays + 1):
programme = dict(programmes[0]) # We clone the original programme
programme["channel"] = f"{programme['channel']}-{delay}h"
programme["start"] += timedelta(hours=delay)
programme["stop"] += timedelta(hours=delay)
programmes.append(programme)
return programmes
def create_programme(self, programme):
cer = programme["cod_evento_rejilla"]
if cer in self.cache:
return self.cache.get(cer)
info = {
"channel": programme["cod_cadena_tv"],
"title": programme["des_evento_rejilla"],
"category": programme["des_genero"],
"start": DateTime().parse(programme["f_evento_rejilla"]),
"stop": DateTime().parse(programme["f_fin_evento_rejilla"])
}
cee = programme["cod_elemento_emision"]
if cee and DOWNLOAD_EXTRA_INFO:
info.update(Movistar.get_extra_info(cee))
# DAYS_TO_DOWNLOAD + 1 we add the day before data | 86400 seconds in a day
expire_time = (DAYS_TO_DOWNLOAD + 1) * 86400
self.cache.set(cer, info, expire=expire_time)
return info
def generate_epg_data(self, channels, programmes):
print("Generating epg data...")
p = Pool(cpu_count())
epg_channels = p.map(self.create_channels, channels)
p.terminate()
p.join()
p = Pool(cpu_count())
epg_programmes = p.map(self.create_programmes, programmes)
p.terminate()
p.join()
return itertools.chain(*epg_channels), itertools.chain(*epg_programmes)
def dump_epg_data(self, channels, programmes):
print("Dumping epg data to xml...")
tv = ET.Element("tv")
tv.set("date", datetime.now().strftime("%Y-%m-%d"))
tv.set("source-info-url", "http://comunicacion.movistarplus.es/programacion/")
tv.set("source-info-name", "Movistar")
tv.set("generator-info-name", "Movistar EPG generator")
tv.set("generator-info-url", "https://github.com/oscarbc96/epg_generator")
for channel_data in channels:
channel = ET.SubElement(tv, "channel")
channel.set("id", channel_data["code"])
display_name = ET.SubElement(channel, "display-name")
display_name.set("lang", "es")
display_name.text = channel_data["name"]
icon = ET.SubElement(channel, "icon")
icon.set("src", channel_data["logo"])
for programme_data in programmes:
programme = ET.SubElement(tv, "programme")
programme.set("start", DateTime().format(programme_data["start"]))
programme.set("stop", DateTime().format(programme_data["stop"]))
programme.set("channel", programme_data["channel"])
title = ET.SubElement(programme, "title")
title.set("lang", "es")
title.text = programme_data["title"]
category = ET.SubElement(programme, "category")
category.set("lang", "es")
category.text = programme_data["category"]
if "desc" in programme_data:
desc = ET.SubElement(programme, "desc")
desc.set("lang", "es")
desc.text = programme_data["desc"]
if "image" in programme_data:
icon = ET.SubElement(programme, "icon")
icon.set("src", programme_data["image"])
if "age_rating" in programme_data:
rating = ET.SubElement(programme, "rating")
rating.set("system", "ES")
value = ET.SubElement(rating, "value")
value.text = programme_data["age_rating"]
if "details" in programme_data:
details = programme_data["details"]
if "temporada" in details and "capitulo" in details:
season = details["temporada"]
chapter = details["capitulo"]
episode_num = ET.SubElement(programme, "episode-num")
episode_num.set("system", "xmltv_ns")
episode_num.text = f"{season}.{chapter}.0/1"
credits = ET.SubElement(programme, "credits")
if "actor" in details:
for actor_data in details["actor"]:
actor = ET.SubElement(credits, "actor")
actor.text = actor_data
if "director" in details:
for director_data in details["director"]:
director = ET.SubElement(credits, "director")
director.text = director_data
xml = ET.tostring(tv, encoding="ISO-8859-1", method="xml")
if not os.path.exists(OUTPUT_FOLDER):
os.makedirs(OUTPUT_FOLDER)
output = open(EPG_FILE, "wb")
output.write(xml)
output.close()
if __name__ == "__main__":
print("EPG Generator")
print(f"N. cores: {cpu_count()}")
print(f"Download extra info: {DOWNLOAD_EXTRA_INFO}")
print(f"Days to download: {DAYS_TO_DOWNLOAD}")
start = datetime.now()
print(f"Start: {start}")
EPGGenerator().run()
stop = datetime.now()
print(f"Stop: {stop}")
print(f"Execution time: {stop - start}")