-
Notifications
You must be signed in to change notification settings - Fork 11
/
mall.py
329 lines (250 loc) · 11.8 KB
/
mall.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
317
318
319
320
321
322
323
324
325
326
327
328
329
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests
import re
from urlparse import urlparse, urlunparse
class MallApi():
def __init__(self, plugin):
self.plugin = plugin
self.is_cz = self.plugin.get_setting('country') == '0'
self.BASE = 'https://www.mall.tv' if self.is_cz else 'https://sk.mall.tv'
def get_img_for(self, img_version, url):
for pattern in ['/mobile/', '/mobile-a/', '/desktop/']:
if pattern in url:
url=url.replace(pattern,'/'+img_version+'/')
break
return url
def get_fanart_url(self, url):
return self.get_img_for('background', url)
def get_thumb_url(self, url):
return self.get_img_for('desktop', url)
def warn(self, *args, **kwargs):
self.plugin.log.warning(*args, **kwargs)
def url_for(self, *args, **kwargs):
return self.plugin.url_for(*args, **kwargs)
def get_page(self, url):
r = requests.get(self.BASE + url, cookies=dict(__selectedLanguage= 'cz' if self.is_cz else 'sk'), headers={'User-Agent': 'AndroidApp; LGE LG-850; 8.0.0'})
return BeautifulSoup(r.content, 'html.parser')
def get_categories(self, ):
result = []
page = self.get_page('/kategorie')
category = page.find('section', {'class': 'isCategory'})
cards = category.find_all('div', {'class': 'video-card'})
self.warn(self.url_for('category', link='3'))
for card in cards:
a = card.find('a')
result.append({
'path': self.url_for('category', link=a['href']),
'thumbnail': self.get_thumb_url(a['data-src']),
'label': card.find('h2').contents[0],
'fanart': self.get_fanart_url(a['data-src'])
})
badges = page.find_all('a', {'class': 'badge-kategory'})
for badge in badges:
result.append({
'path': self.url_for('category', link=badge['href']),
'label': badge.contents[0]
})
return result
def get_category(self, link):
page = self.get_page(link)
return self.extract_shows(page)
def get_shows(self):
index = 0
shows = []
while True:
self.warn(index)
page = self.get_page('/Serie/CategorySortedSeries?categoryId=0&sortType=1&page=' + str(index))
if not page:
return shows
slider_item = page.find('div', {'class': 'video-grid__body'})
if slider_item:
count = int(slider_item['slider-total'])
shows += self.extract_shows(page)
if len(shows) >= count:
break
index += 1
return shows
def get_show_videos(self, link, season_id):
if season_id == '0':
page = self.get_page(link)
seasons = self.get_seasons(page)
else:
page = self.get_page('/Serie/Season?seasonId={}&sortType=0&page=0'.format(season_id))
seasons = []
videos = self.extract_videos(page, search_section=True)
return seasons + videos
def get_seasons(self, page):
result = []
items = page.select('.mall_categories-list li')
for item in items[1:-1]:
for li in item.find_all('li'):
li.extract()
result.append({
'label': item.text,
'path': self.url_for('show', season=item['data-id'])
})
return result
def get_paged_videos(self, page, video_type):
if video_type == 'recent':
page = self.get_page(('/sekce/nejnovejsi' if self.is_cz else '/sekcia/najnovsie') +'?page={0}'.format(page))
elif video_type == 'popular':
page = self.get_page(('/sekce' if self.is_cz else '/sekcia') + '/trending?page={0}'.format(page))
videos = self.extract_videos(page, search_section=(page == 0))
for r in videos:
ctx_url = self.url_for('show', link=r['show_link'])
r['label'] = '[LIGHT]%s[/LIGHT] | %s' % (r['show_name'], r['label'])
r['context_menu'] = [(self.plugin.get_string(30014), 'XBMC.Container.Update({}, false)'.format(ctx_url))]
return videos
def get_live_categories(self):
page = self.get_page(('/zive' if self.is_cz else '/nazivo'))
video_grids = page.find_all('section', {'class': ['video-grid', 'isVideo']})
if not video_grids:
return []
# display current streams directly in the root as first items
result = self.get_live_category_videos(0, page)
# then the rest of the categories
for category_id, grid in enumerate(video_grids[1:], 1):
live_section_title = grid.find('h2', {'class': ['video-grid__title']}).text
result.append({
'label': live_section_title,
'path': self.url_for('live', category_id=str(category_id))
})
return result
def get_live_category_videos(self, category, page=None):
if not page:
page = self.get_page(('/zive' if self.is_cz else '/nazivo'))
videos = self.extract_live(page, category)
for r in videos:
r['context_menu'] = [(self.plugin.get_string(30014), 'XBMC.Container.Update({}, false)'.format(r['path']))]
return videos
def extract_shows(self, page):
result = []
for item in page.select('.video-card__series figure'):
img = item.find('a', attrs={'data-src': True})['data-src']
result.append({
'label': item.find('h4').text,
'path': self.url_for('show', link=item.find('a')['href']),
'thumbnail': self.get_thumb_url(img),
'fanart': self.get_fanart_url(img),
'info': {
'mediatype': 'tvshow',
'tvshowtitle': item.find('h4').text
}
})
return result
def extract_videos(self, page, search_section=True):
result = []
if search_section:
grid = page.find('section', {'class': ['video-grid', 'isVideo']})
else:
grid = page
for card in grid.find_all('div', {'class': 'video-card'}):
link = card.select('.video-card__details a.video-card__details-link')[0]
duration = card.find('span', {'class': 'badge__wrapper-video-duration'})
show = card.find('a', {'class': ['video-card__info', 'video-card__info-channel']})
show_title = show.text
show_link = show['href']
show_fanart = self.get_fanart_url(card.find('div', {'class': ['d-md-none', 'video-card__small-img']})['data-src'])
if not duration:
continue
self.plugin.log.debug(link['href'].encode('utf-8'))
result.append({
'label': link.text,
'thumbnail': self.get_thumb_url(card.find('div', {'class': ['video-card__thumbnail', 'lazy']})['data-src']),
'path': self.url_for('video', link=link['href'].encode('utf-8')),
'link': link['href'],
'info': {
'duration': self.get_duration(duration.text),
'mediatype': 'episode',
'tvshowtitle': show_title,
'title': link.text
},
'is_playable': True,
'show_name': show_title,
'show_link': show_link,
'fanart': show_fanart
})
return result
def extract_live(self, page, category):
result = []
video_grids = page.find_all('section', {'class': ['video-grid', 'isVideo']})
category_idx = int(category)
if category_idx >= len(video_grids):
return result
grid = video_grids[category_idx]
for card in grid.find_all('div', {'class': 'video-card'}):
link = card.select('.video-card__details a.video-card__details-link')[0]
show = card.find('a', {'class': ['video-card__info', 'video-card__info-channel']})
show_title = show.text
show_link = show['href']
show_fanart = self.get_fanart_url(card.find('div', {'class': ['d-md-none', 'video-card__small-img']})['data-src'])
self.plugin.log.debug(link['href'].encode('utf-8'))
result.append({
'label': link.text,
'thumbnail': self.get_thumb_url(card.find('div', {'class': ['video-card__thumbnail', 'lazy']})['data-src']),
'link': link['href'],
'path': self.url_for('livestream', link=link['href'].encode('utf-8')),
'info': {
'duration': '',
'mediatype': 'episode',
'tvshowtitle': show_title,
'title': link.text
},
'is_playable': True,
'show_name': show_title,
'show_link': show_link,
'fanart': show_fanart
})
return result
def get_duration(self, val):
count = 0
coef = [1, 60, 3600]
for index, part in enumerate(reversed(val.split(':'))):
count += int(part) * coef[index]
return count
def get_video_main_url(self, page):
# extracts a video url from a script tag, it's in an internal json structure under VideoSource value
script_tag = page.find(lambda tag: tag.name == 'script' and (tag.string and 'VideoSource' in tag.string))
# removes everything before the value of VideoSource including the quote character
tmp_str = re.sub(r'^.*VideoSource"[\s]*:[\s]*"', '', script_tag.string)
# removes everything after the value including the quote character
return re.sub(r'["\s]*,["\s]*.*$', '', tmp_str).strip()
def get_video_url(self, link):
page = self.get_page(link)
source = page.find('source')
if not source:
main_link = self.get_video_main_url(page)
else:
main_link = source['src']
if not main_link:
self.plugin.notify(self.plugin.get_string(30021).encode("utf-8"), delay=7000, image=self.plugin._addon.getAddonInfo('icon'))
return None
# non "index.m3u8" playlists contains video-id together with quality id
video_id=''
if not main_link.endswith('index'):
video_id=main_link.split('/')[-1]
main_link += '.m3u8'
url_parts = urlparse(main_link, 'https')
main_link = urlunparse(url_parts)
index_list = requests.get(main_link).text
qualities = re.findall(video_id+r'(\d+)/index.m3u8', index_list, flags=re.MULTILINE)
if not len(qualities):
self.plugin.notify(self.plugin.get_string(30021).encode("utf-8"), delay=7000, image=self.plugin._addon.getAddonInfo('icon'))
return None
qualities = reversed(sorted(map(int, qualities)))
max_quality = int(self.plugin.get_setting('max_quality'))
for q in qualities:
selected = q
if selected <= max_quality:
break
self.plugin.log.debug('Selected quality: '+str(selected))
if selected > max_quality:
self.plugin.notify(self.plugin.get_string(30020).encode("utf-8") % (str(max_quality), str(selected)), delay=7000, image=self.plugin._addon.getAddonInfo('icon'))
# current live streams contain 'live' text in their link and have to be treated differently
if 'live' not in main_link:
url = '{0}/{1}/index{1}.mp4' if self.plugin.get_setting('format') == 'MP4' else '{0}/{1}/index.m3u8';
return url.format(main_link.replace('/index.m3u8', ''), selected)
else:
url = '{0}{1}/index.m3u8'
return url.format(main_link.replace('.m3u8', ''), selected)