forked from Cyogenus/XTREME-IPTV-PLAYER-by-MY-1
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathThreadpools.py
More file actions
722 lines (603 loc) · 32.6 KB
/
Threadpools.py
File metadata and controls
722 lines (603 loc) · 32.6 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
import sys
import os
from os import path
import time
import requests
import subprocess
import configparser
import re
import json
import html
from lxml import etree, html
from datetime import datetime
from dateutil import parser, tz
import xml.etree.ElementTree as ET
from PyQt5.QtGui import QIcon, QFont, QImage, QPixmap, QColor
from PyQt5.QtCore import (
Qt, QTimer, QPropertyAnimation, QEasingCurve, QSize, QObject, pyqtSignal,
QRunnable, pyqtSlot, QThreadPool, QModelIndex, QAbstractItemModel, QVariant
)
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QVBoxLayout, QLineEdit, QLabel, QPushButton,
QListWidget, QWidget, QFileDialog, QCheckBox, QSizePolicy, QHBoxLayout,
QDialog, QFormLayout, QDialogButtonBox, QTabWidget, QListWidgetItem,
QSpinBox, QMenu, QAction, QTextEdit, QGridLayout, QMessageBox, QListView,
QTreeWidget, QTreeWidgetItem, QTreeView
)
import base64
CONNECTION_HEADER = "Keep-Alive"
CONTENT_HEADER = "gzip, deflate"
DEFAULT_USER_AGENT_HEADER = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36"
#Default timeout values
CONNECTION_TIMEOUT = 3
READ_TIMEOUT = 30
LIVE_STATUS_TIMEOUT = 7
class FetchDataWorkerSignals(QObject):
finished = pyqtSignal(dict, dict, dict)
error = pyqtSignal(str)
progress_bar = pyqtSignal(int, int, str)
show_error_msg = pyqtSignal(str, str)
show_info_msg = pyqtSignal(str, str)
class FetchDataWorker(QRunnable):
def __init__(self, server, username, password, live_url_format, movie_url_format, series_url_format, parent=None, fetch_vods=True):
super().__init__()
self.server = server
self.username = username
self.password = password
self.live_url_format = live_url_format
self.movie_url_format = movie_url_format
self.series_url_format = series_url_format
self.fetch_vods = fetch_vods
self.parent = parent
self.signals = FetchDataWorkerSignals()
@pyqtSlot()
def run(self):
try:
categories_per_stream_type = {
'LIVE': [],
'Movies': [],
'Series': []
}
entries_per_stream_type = {
'LIVE': [],
'Movies': [],
'Series': []
}
#Create header
headers = {
"Connection": CONNECTION_HEADER,
"Accept-Encoding": CONTENT_HEADER,
"User-Agent": self.parent.current_user_agent
}
params = {
'username': self.username,
'password': self.password,
'action': ''
}
host_url = f"{self.server}/player_api.php"
print("Going to fetch IPTV data")
#Get IPTV info
self.signals.progress_bar.emit(0, 5, "Fetching IPTV info")
try:
iptv_info_resp = requests.get(host_url, params=params, headers=headers, timeout=(CONNECTION_TIMEOUT, READ_TIMEOUT))
iptv_info_resp.raise_for_status()
iptv_info_data = iptv_info_resp.json()
except Exception as e:
iptv_info_data = {}
print(f"failed fetching IPTV data: {e}")
#Load cached data
cached_data = {}
#Check if cache file exists
if path.isfile(self.parent.cache_file):
print("Cache file is there")
try:
print("Loading cached data")
with open(self.parent.cache_file, 'r') as cache_file:
cached_data = json.load(cache_file)
except Exception as e:
cached_data = {}
# self.signals.show_error_msg.emit('Failed loading cache file',
# "Failed loading cache file.\n"
# "Please check if it is empty or corrupted.")
print("Failed loading cache file. Please check if it is empty or corrupted.")
config = configparser.ConfigParser()
config.read(self.parent.user_data_file)
if 'Debug' in config and config['Debug']['load_with_cache'] == 'True': #For testing purposes only
categories_per_stream_type['LIVE'] = cached_data['LIVE categories']
categories_per_stream_type['Movies'] = cached_data['Movies categories']
categories_per_stream_type['Series'] = cached_data['Series categories']
entries_per_stream_type['LIVE'] = cached_data['LIVE']
entries_per_stream_type['Movies'] = cached_data['Movies']
entries_per_stream_type['Series'] = cached_data['Series']
else:
#Get all category data
print("Fetching Live TV categories")
self.signals.progress_bar.emit(5, 10, "Fetching LIVE Categories")
try:
params['action'] = 'get_live_categories'
live_category_resp = requests.get(host_url, params=params, headers=headers, timeout=(CONNECTION_TIMEOUT, READ_TIMEOUT))
live_category_resp.raise_for_status() #Raises HTTP error is status is 4xx or 5xx
categories_per_stream_type['LIVE'] = live_category_resp.json()
except Exception as e:
print(f"failed fetching LIVE categories: {e}")
if cached_data.get('LIVE categories', 0):
print("Getting LIVE categories from cache")
categories_per_stream_type['LIVE'] = cached_data['LIVE categories']
#Notify user that data is fetched from cache file
# self.signals.show_info_msg.emit('Getting IPTV data from cache',
# "Couldn't get Live TV categories from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.\n"
# "Fortunately, Live TV categories could be loaded from cache.")
print("Failed fetching Live TV categories. Got them from cache.")
else:
#Display error msg that data fetching failed
# self.signals.show_error_msg.emit('Failed fetching data from IPTV provider',
# "Couldn't get Live TV categories from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.")
print("Failed fetching Live TV categories")
if self.fetch_vods:
print("Fetching Movies categories")
self.signals.progress_bar.emit(10, 20, "Fetching VOD Categories")
try:
params['action'] = 'get_vod_categories'
movies_category_resp = requests.get(host_url, params=params, headers=headers, timeout=(CONNECTION_TIMEOUT, READ_TIMEOUT))
movies_category_resp.raise_for_status() #Raises HTTP error is status is 4xx or 5xx
categories_per_stream_type['Movies'] = movies_category_resp.json()
except Exception as e:
print(f"failed fetching VOD categories: {e}")
if cached_data.get('Movies categories', 0):
print("Getting Movies categories from cache")
categories_per_stream_type['Movies'] = cached_data['Movies categories']
#Notify user that data is fetched from cache file
# self.signals.show_info_msg.emit('Getting IPTV data from cache',
# "Couldn't get Movies categories from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.\n"
# "Fortunately, Movies categories could be loaded from cache.")
print("Failed fetching Movies categories. Got them from cache.")
else:
#Display error msg that data fetching failed
# self.signals.show_error_msg.emit('Failed fetching data from IPTV provider',
# "Couldn't get Movies categories from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.")
print("Failed fetching Movies categories")
if self.fetch_vods:
print("Fetching Series categories")
self.signals.progress_bar.emit(20, 30, "Fetching Series Categories")
try:
params['action'] = 'get_series_categories'
series_category_resp = requests.get(host_url, params=params, headers=headers, timeout=(CONNECTION_TIMEOUT, READ_TIMEOUT))
series_category_resp.raise_for_status() #Raises HTTP error is status is 4xx or 5xx
categories_per_stream_type['Series'] = series_category_resp.json()
except Exception as e:
print(f"failed fetching Series categories: {e}")
if cached_data.get('Series categories', 0):
print("Getting Series categories from cache")
categories_per_stream_type['Series'] = cached_data['Series categories']
#Notify user that data is fetched from cache file
# self.signals.show_info_msg.emit('Getting IPTV data from cache',
# "Couldn't get Series categories from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.\n"
# "Fortunately, Series categories could be loaded from cache.")
print("Failed fetching Series categories. Got them from cache.")
else:
#Display error msg that data fetching failed
# self.signals.show_error_msg.emit('Failed fetching data from IPTV provider',
# "Couldn't get Series categories from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.")
print("Failed fetching Series categories")
print("Fetching Live TV streaming data")
#Get all streaming data
self.signals.progress_bar.emit(30, 40, "Fetching LIVE Streaming data")
try:
params['action'] = 'get_live_streams'
live_streams_resp = requests.get(host_url, params=params, headers=headers, timeout=(CONNECTION_TIMEOUT, READ_TIMEOUT))
live_streams_resp.raise_for_status() #Raises HTTP error is status is 4xx or 5xx
entries_per_stream_type['LIVE'] = live_streams_resp.json()
except Exception as e:
print(f"failed fetching LIVE streams: {e}")
if cached_data.get('LIVE', 0):
print("Getting LIVE streams from cache")
entries_per_stream_type['LIVE'] = cached_data['LIVE']
#Notify user that data is fetched from cache file
# self.signals.show_info_msg.emit('Getting IPTV data from cache',
# "Couldn't get Live TV streams from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.\n"
# "Fortunately, Live TV streams could be loaded from cache.")
print("Failed fetching Live TV streams. Got them from cache.")
else:
#Display error msg that data fetching failed
# self.signals.show_error_msg.emit('Failed fetching data from IPTV provider',
# "Couldn't get Live TV streams from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.")
print("Failed fetching Live TV streams")
if self.fetch_vods:
print("Fetching Movies streaming data")
self.signals.progress_bar.emit(40, 60, "Fetching VOD Streaming data")
try:
params['action'] = 'get_vod_streams'
movies_streams_resp = requests.get(host_url, params=params, headers=headers, timeout=(CONNECTION_TIMEOUT, READ_TIMEOUT))
movies_streams_resp.raise_for_status() #Raises HTTP error is status is 4xx or 5xx
entries_per_stream_type['Movies'] = movies_streams_resp.json()
except Exception as e:
print(f"failed fetching VOD streams: {e}")
if cached_data.get('Movies', 0):
print("Getting Movies streams from cache")
entries_per_stream_type['Movies'] = cached_data['Movies']
#Notify user that data is fetched from cache file
# self.signals.show_info_msg.emit('Getting IPTV data from cache',
# "Couldn't get Movies streams from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.\n"
# "Fortunately, Movies streams could be loaded from cache.")
print("Failed fetching Movies streams. Got them from cache.")
else:
#Display error msg that data fetching failed
# self.signals.show_error_msg.emit('Failed fetching data from IPTV provider',
# "Couldn't get Movies streams from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.")
print("Failed fetching Live TV streams")
if self.fetch_vods:
print("Fetching Series streaming data")
self.signals.progress_bar.emit(60, 80, "Fetching Series Streaming data")
try:
params['action'] = 'get_series'
series_streams_resp = requests.get(host_url, params=params, headers=headers, timeout=(CONNECTION_TIMEOUT, READ_TIMEOUT))
series_streams_resp.raise_for_status() #Raises HTTP error is status is 4xx or 5xx
entries_per_stream_type['Series'] = series_streams_resp.json()
except Exception as e:
print(f"failed fetching Series streams: {e}")
if cached_data.get('Series', 0):
print("Getting Series streams from cache")
entries_per_stream_type['Series'] = cached_data['Series']
#Notify user that data is fetched from cache file
# self.signals.show_info_msg.emit('Getting IPTV data from cache',
# "Couldn't get Series streams from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.\n"
# "Fortunately, Series streams could be loaded from cache.")
print("Failed fetching Series streams. Got them from cache.")
else:
#Display error msg that data fetching failed
# self.signals.show_error_msg.emit('Failed fetching data from IPTV provider',
# "Couldn't get Series streams from IPTV provider.\n"
# "Please check your internet connection or if IPTV server is still online.")
print("Failed fetching Series streams")
print("going to create cached data")
all_cached_data = json.dumps({
'LIVE categories': categories_per_stream_type['LIVE'],
'Movies categories': categories_per_stream_type['Movies'],
'Series categories': categories_per_stream_type['Series'],
'LIVE': entries_per_stream_type['LIVE'],
'Movies': entries_per_stream_type['Movies'],
'Series': entries_per_stream_type['Series']
},
indent=4)
with open(self.parent.cache_file, 'w') as cache_file:
cache_file.write(all_cached_data)
# self.set_progress_bar(100, "Finished loading data")
self.signals.progress_bar.emit(80, 100, "Finished Fetching data")
fav_data = {}
#Check if cache file exists
if path.isfile(self.parent.favorites_file):
print("Favorites file is there")
with open(self.parent.favorites_file, 'r') as fav_file:
fav_data = json.load(fav_file)
print("Preparing streaming data")
#Make streaming URL in each entry except for the series
for tab_name in entries_per_stream_type.keys():
for idx, entry in enumerate(entries_per_stream_type[tab_name]):
#Get stream type. If no stream_type is found it is series
stream_type = entry.get('stream_type', 'series')
stream_id = entry.get("stream_id", -1)
series_id = entry.get("series_id", -1)
container_extension = entry.get("container_extension", "m3u8")
#Correct for any vague other stream types. Series stream type is already fixed by code above.
if "live" in stream_type:
stream_type = "live"
if "movie" in stream_type:
stream_type = "movie"
#Check if stream_id is valid
if stream_id:
entries_per_stream_type[tab_name][idx]["url"] = self.generate_url(stream_type, stream_id, container_extension)
#Check if stream id is in favorites list in userdata.ini
if stream_id in fav_data.get('stream_ids', []):
#Add "favorite" parameter to entries_per_stream_type and set to True or False depending if inside userdata.ini
entries_per_stream_type[tab_name][idx]['favorite'] = True
else:
entries_per_stream_type[tab_name][idx]['favorite'] = False
else:
entries_per_stream_type[tab_name][idx]["url"] = None
#Check if stream type is series
if stream_type == 'series':
#Create stream type key for series data
entries_per_stream_type[tab_name][idx]["stream_type"] = stream_type
#Check if series_id is valid
if series_id:
#Check if series id is in favorites list in userdata.ini
if series_id in fav_data.get('series_ids', []):
#Add "favorite" parameter to entries_per_stream_type and set to True or False depending if inside userdata.ini
entries_per_stream_type[tab_name][idx]['favorite'] = True
else:
entries_per_stream_type[tab_name][idx]['favorite'] = False
#Send received data to processing function
self.signals.finished.emit(iptv_info_data, categories_per_stream_type, entries_per_stream_type)
print("Finished downloading IPTV data")
except Exception as e:
print(f"Exception! {e}")
self.signals.error.emit(str(e))
def generate_url(self, stream_type, stream_id, container_extension):
# Select the appropriate format string
if stream_type == 'live':
fmt = self.live_url_format
elif stream_type == 'movie':
fmt = self.movie_url_format
else:
# Fallback format if unknown type
fmt = "{server}/{stream_type}/{username}/{password}/{stream_id}.{container_extension}"
# Remove extension if not included in the format string
if ".{container_extension}" not in fmt:
container_extension = ""
# Format and return the URL
return fmt.format(
server=self.server,
username=self.username,
password=self.password,
stream_type=stream_type,
stream_id=stream_id,
container_extension=container_extension
)
class MovieInfoFetcherSignals(QObject):
finished = pyqtSignal(dict, dict)
error = pyqtSignal(str)
class MovieInfoFetcher(QRunnable):
def __init__(self, server, username, password, vod_id, parent=None):
super().__init__()
self.server = server
self.username = username
self.password = password
self.vod_id = vod_id
self.parent = parent
self.signals = MovieInfoFetcherSignals()
@pyqtSlot()
def run(self):
try:
#Set request parameters
# headers = {'User-Agent': CUSTOM_USER_AGENT}
#Create header
headers = {
"Connection": CONNECTION_HEADER,
"Accept-Encoding": CONTENT_HEADER,
"User-Agent": self.parent.current_user_agent
}
host_url = f"{self.server}/player_api.php"
params = {
'username': self.username,
'password': self.password,
'action': 'get_vod_info',
'vod_id': self.vod_id
}
#Request vod info
vod_info_resp = requests.get(host_url, params=params, headers=headers, timeout=(CONNECTION_TIMEOUT, READ_TIMEOUT))
#Get vod info data
vod_info_data = vod_info_resp.json()
#Get info and movie data
vod_info = vod_info_data.get('info', {})
vod_data = vod_info_data.get('movie_data', {})
#Check if the variable types are valid
if not isinstance(vod_info, dict):
vod_info = {}
if not isinstance(vod_data, dict):
vod_data = {}
#Return movie info data
self.signals.finished.emit(vod_info, vod_data)
except Exception as e:
print(f"Failed fetching movie info: {e}")
self.signals.error.emit(str(e))
class SeriesInfoFetcherSignals(QObject):
finished = pyqtSignal(dict, bool)
error = pyqtSignal(str)
class SeriesInfoFetcher(QRunnable):
def __init__(self, server, username, password, series_id, is_show_request, parent=None):
super().__init__()
self.server = server
self.username = username
self.password = password
self.series_id = series_id
self.is_show_request = is_show_request
self.parent = parent
self.signals = SeriesInfoFetcherSignals()
@pyqtSlot()
def run(self):
try:
#Set request parameters
# headers = {'User-Agent': CUSTOM_USER_AGENT}
#Create header
headers = {
"Connection": CONNECTION_HEADER,
"Accept-Encoding": CONTENT_HEADER,
"User-Agent": self.parent.current_user_agent
}
host_url = f"{self.server}/player_api.php"
params = {
'username': self.username,
'password': self.password,
'action': 'get_series_info',
'series_id': self.series_id
}
#Request series info
series_info_resp = requests.get(host_url, params=params, headers=headers, timeout=(CONNECTION_TIMEOUT, READ_TIMEOUT))
#Get series info data
series_info_data = series_info_resp.json()
#Check if the variable type is valid
if not isinstance(series_info_data, dict):
series_info_data = {}
#Return series info data
self.signals.finished.emit(series_info_data, self.is_show_request)
except Exception as e:
print(f"Failed fetching series info: {e}")
self.signals.error.emit(str(e))
class ImageFetcherSignals(QObject):
finished = pyqtSignal(QPixmap, str)
error = pyqtSignal(str)
class ImageFetcher(QRunnable):
def __init__(self, img_url, stream_type, parent=None):
super().__init__()
self.img_url = img_url
self.stream_type = stream_type
self.parent = parent
self.signals = ImageFetcherSignals()
@pyqtSlot()
def run(self):
try:
#Set header for request
# headers = {'User-Agent': CUSTOM_USER_AGENT}
#Create header
headers = {
"Connection": CONNECTION_HEADER,
"Accept-Encoding": CONTENT_HEADER,
"User-Agent": self.parent.current_user_agent
}
#Request image
image_resp = requests.get(self.img_url, headers=headers, timeout=(CONNECTION_TIMEOUT, READ_TIMEOUT))
#Check if response code is valid, otherwise set replacement image
resp_status = image_resp.status_code
if resp_status == 404:
#Set 404 error as image
image = QPixmap(self.parent.path_to_404_img)
elif not resp_status == 200:
#Set no image
image = QPixmap(self.parent.path_to_no_img)
else:
#Create QPixmap from image data
image = QPixmap()
image.loadFromData(image_resp.content) #Don't combine this with the previous line, then it doesn't work
#Check if Pixmap is valid
if image.isNull():
image = QPixmap(self.parent.path_to_no_img)
#Emit image
self.signals.finished.emit(image, self.stream_type)
except Exception as e:
print(f"Failed fetching image: {e}")
#Emit no image placeholder
image = QPixmap(self.parent.path_to_no_img)
self.signals.finished.emit(image, self.stream_type)
self.signals.error.emit(str(e))
class SearchWorkerSignals(QObject):
list_widget = pyqtSignal(list, str)
error = pyqtSignal(str)
class SearchWorker(QRunnable):
def __init__(self, stream_type, currently_loaded_entries, list_widgets, text):
super().__init__()
self.stream_type = stream_type
self.currently_loaded_entries = currently_loaded_entries[0]
self.list_widgets = list_widgets[0]
self.text = text
self.signals = SearchWorkerSignals()
# self.setAutoDelete(True)
@pyqtSlot()
def run(self):
try:
self.list_widgets[self.stream_type].clear()
print("starting searching through entries")
for entry in self.currently_loaded_entries[self.stream_type]:
if self.text.lower() in entry['name'].lower():
item = QListWidgetItem(entry['name'])
item.setData(Qt.UserRole, entry)
self.list_widgets[self.stream_type].addItem(item)
print(entry['name'])
self.signals.list_widget.emit([self.list_widgets[self.stream_type]], self.stream_type)
except Exception as e:
print(f"failed search worker: {e}")
class EPGWorkerSignals(QObject):
finished = pyqtSignal(list)
error = pyqtSignal(str)
class EPGWorker(QRunnable):
def __init__(self, server, username, password, stream_id, parent=None):
super().__init__()
self.server = server
self.username = username
self.password = password
self.stream_id = stream_id
self.parent = parent
self.signals = EPGWorkerSignals()
@pyqtSlot()
def run(self):
try:
#Creating url for requesting EPG data for specific stream
epg_url = f"{self.server}/player_api.php?username={self.username}&password={self.password}&action=get_simple_data_table&stream_id={self.stream_id}"
# headers = {'User-Agent': CUSTOM_USER_AGENT}
#Create header
headers = {
"Connection": CONNECTION_HEADER,
"Accept-Encoding": CONTENT_HEADER,
"User-Agent": self.parent.current_user_agent
}
#Requesting EPG data
response = requests.get(epg_url, headers=headers, timeout=(CONNECTION_TIMEOUT, READ_TIMEOUT))
epg_data = response.json()
#Decrypt EPG data with base 64
decrypted_epg_data = self.decryptEPGData(epg_data)
self.signals.finished.emit(decrypted_epg_data)
except Exception as e:
self.signals.error.emit(str(e))
def decryptEPGData(self, epg_data):
try:
decrypted_epg_data = []
for epg_entry in epg_data['epg_listings']:
#Get start, stop time and date
start_timestamp = datetime.fromtimestamp(int(epg_entry['start_timestamp']))
stop_timestamp = datetime.fromtimestamp(int(epg_entry['stop_timestamp']))
date = f"{start_timestamp.day:02}-{start_timestamp.month:02}-{start_timestamp.year}"
#Decode program name and descryption
program_name = base64.b64decode(epg_entry['title']).decode("utf-8")
program_description = base64.b64decode(epg_entry['description']).decode("utf-8")
#Put only necessary EPG data in list
decrypted_epg_data.append({
'start_time': start_timestamp,
'stop_time': stop_timestamp,
'program_name': program_name,
'description': program_description,
'date': date
})
#return decrypted EPG data
return decrypted_epg_data
except Exception as e:
print(f"failed decrypting: {e}")
class OnlineWorkerSignals(QObject):
finished = pyqtSignal(int, str)
error = pyqtSignal(str)
class OnlineWorker(QRunnable):
def __init__(self, stream_id, url, parent=None):
super().__init__()
self.stream_id = int(stream_id)
self.url = url
self.parent = parent
self.signals = OnlineWorkerSignals()
@pyqtSlot()
def run(self):
try:
#Create header
headers = {
"Connection": CONNECTION_HEADER,
"Accept-Encoding": CONTENT_HEADER,
"User-Agent": self.parent.current_user_agent
}
#Requesting stream playlist data
response = requests.get(self.url, headers=headers, timeout=(CONNECTION_TIMEOUT, LIVE_STATUS_TIMEOUT))
response_code = response.status_code
url_data = response.text
#Determine if stream looks offline or not
stream_offline = self.checkStatus(response_code, url_data)
self.signals.finished.emit(self.stream_id, str(stream_offline))
except Exception as e:
self.signals.error.emit(str(e))
def checkStatus(self, response_code, url_data):
if response_code != 200: # need HTTP OK status
return False
if "offline" in url_data: #some providers use offline.m3u8 as a dummy video file
return False
if "EXT-X-ENDLIST" in url_data: #m3u file is saying stream is over
return False
if "#EXT-X-MEDIA-SEQUENCE:0" in url_data: #some providers respond with a fresh "Stream starting soon" stream
if "_0.ts" in url_data and "_1.ts" not in url_data: #this technically just means a stream is freshly started, hence the "Maybe" online
return "Maybe" #officially, see https://datatracker.ietf.org/doc/html/rfc8216#section-4.3.3.2
return True