-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIPTV M3U_Plus PLAYER by MY-1.py
1507 lines (1280 loc) · 65.2 KB
/
IPTV M3U_Plus PLAYER by MY-1.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
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import os
import time
import requests
import subprocess
import configparser
import re
import json
import html
from lxml import etree
from datetime import datetime
from dateutil import parser, tz
import xml.etree.ElementTree as ET
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import (
Qt, QTimer, QPropertyAnimation, QEasingCurve, QSize, QObject, pyqtSignal, QRunnable, pyqtSlot, QThreadPool
)
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
)
CUSTOM_USER_AGENT = (
"Connection: Keep-Alive User-Agent: okhttp/5.0.0-alpha.2 "
"Accept-Encoding: gzip, deflate"
)
def normalize_channel_name(name):
name = name.lower().strip()
name = re.sub(r'\s+', ' ', name)
name = re.sub(r'[^\w\s]', '', name)
name = re.sub(r'\b(hd|sd|channel|tv)\b', '', name)
name = name.strip()
return name
class EPGWorkerSignals(QObject):
finished = pyqtSignal(dict, dict)
error = pyqtSignal(str)
class EPGWorker(QRunnable):
def __init__(self, server, username, password, http_method):
super().__init__()
self.server = server
self.username = username
self.password = password
self.http_method = http_method
self.signals = EPGWorkerSignals()
@pyqtSlot()
def run(self):
try:
cache_file = 'epg_cache1.xml'
cache_valid = False
if os.path.exists(cache_file):
cache_age = time.time() - os.path.getmtime(cache_file)
if cache_age < 3600:
cache_valid = True
if cache_valid:
with open(cache_file, 'rb') as f:
epg_xml_data = f.read()
else:
epg_url = f"{self.server}/xmltv.php?username={self.username}&password={self.password}"
headers = {'User-Agent': CUSTOM_USER_AGENT}
if self.http_method == 'POST':
response = requests.post(epg_url, headers=headers, timeout=10)
else:
response = requests.get(epg_url, headers=headers, timeout=10)
response.raise_for_status()
epg_xml_data = response.content
with open(cache_file, 'wb') as f:
f.write(epg_xml_data)
epg_data, channel_id_to_names = self.parse_epg_data(epg_xml_data)
self.signals.finished.emit(epg_data, channel_id_to_names)
except Exception as e:
self.signals.error.emit(str(e))
def parse_epg_data(self, epg_xml_data):
epg_dict = {}
channel_id_to_names = {}
try:
epg_tree = ET.fromstring(epg_xml_data)
for channel in epg_tree.findall('channel'):
channel_id = channel.get('id')
if channel_id:
channel_id = channel_id.strip().lower()
display_names = []
for display_name_elem in channel.findall('display-name'):
if display_name_elem.text:
display_name = display_name_elem.text.strip()
normalized_name = normalize_channel_name(display_name)
display_names.append(normalized_name)
channel_id_to_names[channel_id] = display_names
for programme in epg_tree.findall('programme'):
channel_id = programme.get('channel')
if channel_id:
channel_id = channel_id.strip().lower()
start_time = programme.get('start')
stop_time = programme.get('stop')
title_elem = programme.find('title')
description_elem = programme.find('desc')
title = title_elem.text.strip() if title_elem is not None and title_elem.text else ''
description = description_elem.text.strip() if description_elem is not None and description_elem.text else ''
epg_entry = {
'start_time': start_time,
'stop_time': stop_time,
'title': title,
'description': description
}
if channel_id not in epg_dict:
epg_dict[channel_id] = []
epg_dict[channel_id].append(epg_entry)
return epg_dict, channel_id_to_names
except Exception as e:
print(f"Error parsing EPG data: {e}")
return {}, {}
class AddressBookDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Address Book")
self.setMinimumSize(400, 300)
self.parent = parent
layout = QtWidgets.QVBoxLayout(self)
self.credentials_list = QtWidgets.QListWidget()
layout.addWidget(self.credentials_list)
button_layout = QHBoxLayout()
self.add_button = QPushButton("Add")
self.add_button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_FileDialogNewFolder))
self.select_button = QPushButton("Select")
self.select_button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_DialogYesButton))
self.delete_button = QPushButton("Delete")
self.delete_button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_DialogCancelButton))
button_layout.addWidget(self.add_button)
button_layout.addWidget(self.select_button)
button_layout.addWidget(self.delete_button)
layout.addLayout(button_layout)
self.load_saved_credentials()
self.add_button.clicked.connect(self.add_credentials)
self.select_button.clicked.connect(self.select_credentials)
self.delete_button.clicked.connect(self.delete_credentials)
self.credentials_list.itemDoubleClicked.connect(self.double_click_credentials)
def load_saved_credentials(self):
self.credentials_list.clear()
config = configparser.ConfigParser()
config.read('credentials.ini')
if 'Credentials' in config:
for key in config['Credentials']:
self.credentials_list.addItem(key)
def add_credentials(self):
dialog = AddCredentialsDialog(self)
if dialog.exec_() == QtWidgets.QDialog.Accepted:
method, name, *credentials = dialog.get_credentials()
if name:
config = configparser.ConfigParser()
config.read('credentials.ini')
if 'Credentials' not in config:
config['Credentials'] = {}
if method == 'manual':
server, username, password = credentials
config['Credentials'][name] = f"manual|{server}|{username}|{password}"
elif method == 'm3u_plus':
m3u_url, = credentials
config['Credentials'][name] = f"m3u_plus|{m3u_url}"
with open('credentials.ini', 'w') as config_file:
config.write(config_file)
self.load_saved_credentials()
def select_credentials(self):
selected_item = self.credentials_list.currentItem()
if selected_item:
name = selected_item.text()
config = configparser.ConfigParser()
config.read('credentials.ini')
if 'Credentials' in config and name in config['Credentials']:
data = config['Credentials'][name]
if data.startswith('manual|'):
_, server, username, password = data.split('|')
self.parent.server_entry.setText(server)
self.parent.username_entry.setText(username)
self.parent.password_entry.setText(password)
self.parent.login()
elif data.startswith('m3u_plus|'):
_, m3u_url = data.split('|', 1)
self.parent.extract_credentials_from_m3u_plus_url(m3u_url)
self.parent.login()
self.accept()
def double_click_credentials(self, item):
self.select_credentials()
self.accept()
def delete_credentials(self):
selected_item = self.credentials_list.currentItem()
if selected_item:
name = selected_item.text()
config = configparser.ConfigParser()
config.read('credentials.ini')
if 'Credentials' in config and name in config['Credentials']:
del config['Credentials'][name]
with open('credentials.ini', 'w') as config_file:
config.write(config_file)
self.load_saved_credentials()
class AddCredentialsDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Add Credentials")
layout = QtWidgets.QVBoxLayout(self)
self.method_selector = QtWidgets.QComboBox()
self.method_selector.addItems(["Manual Entry", "m3u_plus URL Entry"])
layout.addWidget(QtWidgets.QLabel("Select Method:"))
layout.addWidget(self.method_selector)
self.stack = QtWidgets.QStackedWidget()
layout.addWidget(self.stack)
self.manual_form = QtWidgets.QWidget()
manual_layout = QFormLayout(self.manual_form)
self.name_entry_manual = QLineEdit()
self.server_entry = QLineEdit()
self.username_entry = QLineEdit()
self.password_entry = QLineEdit()
self.password_entry.setEchoMode(QLineEdit.Password)
manual_layout.addRow("Name:", self.name_entry_manual)
manual_layout.addRow("Server URL:", self.server_entry)
manual_layout.addRow("Username:", self.username_entry)
manual_layout.addRow("Password:", self.password_entry)
self.m3u_form = QtWidgets.QWidget()
m3u_layout = QFormLayout(self.m3u_form)
self.name_entry_m3u = QLineEdit()
self.m3u_url_entry = QLineEdit()
m3u_layout.addRow("Name:", self.name_entry_m3u)
m3u_layout.addRow("m3u_plus URL:", self.m3u_url_entry)
self.stack.addWidget(self.manual_form)
self.stack.addWidget(self.m3u_form)
self.method_selector.currentIndexChanged.connect(self.stack.setCurrentIndex)
buttons = QtWidgets.QDialogButtonBox(
QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel,
Qt.Horizontal, self)
layout.addWidget(buttons)
buttons.accepted.connect(self.validate_and_accept)
buttons.rejected.connect(self.reject)
def validate_and_accept(self):
method = self.method_selector.currentText()
if method == "Manual Entry":
name = self.name_entry_manual.text().strip()
server = self.server_entry.text().strip()
username = self.username_entry.text().strip()
password = self.password_entry.text().strip()
if not name or not server or not username or not password:
QtWidgets.QMessageBox.warning(self, "Input Error", "Please fill all fields for Manual Entry.")
return
self.accept()
else:
name = self.name_entry_m3u.text().strip()
m3u_url = self.m3u_url_entry.text().strip()
if not name or not m3u_url:
QtWidgets.QMessageBox.warning(self, "Input Error", "Please fill all fields for m3u_plus URL Entry.")
return
self.accept()
def get_credentials(self):
method = self.method_selector.currentText()
if method == "Manual Entry":
name = self.name_entry_manual.text().strip()
server = self.server_entry.text().strip()
username = self.username_entry.text().strip()
password = self.password_entry.text().strip()
return ('manual', name, server, username, password)
else:
name = self.name_entry_m3u.text().strip()
m3u_url = self.m3u_url_entry.text().strip()
return ('m3u_plus', name, m3u_url)
class IPTVPlayerApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Xtream IPTV Player by MY-1 V3.7")
self.resize(700, 550)
self.groups = {}
self.entries_per_tab = {
'LIVE': [],
'Movies': [],
'Series': []
}
self.navigation_stacks = {
'LIVE': [],
'Movies': [],
'Series': []
}
self.external_player_command = ""
self.load_external_player_command()
self.top_level_scroll_positions = {
'LIVE': 0,
'Movies': 0,
'Series': 0
}
self.server = ""
self.username = ""
self.password = ""
self.login_type = None
self.epg_data = {}
self.channel_id_to_names = {}
self.epg_last_updated = None
self.threadpool = QThreadPool()
self.threadpool.setMaxThreadCount(10)
self.epg_id_mapping = {}
self.epg_name_map = {}
self.go_back_icon = self.style().standardIcon(QtWidgets.QStyle.SP_ArrowBack)
self.live_channel_icon = self.style().standardIcon(QtWidgets.QStyle.SP_MediaVolume)
self.movies_channel_icon = self.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay)
self.series_channel_icon = self.style().standardIcon(QtWidgets.QStyle.SP_DirIcon)
central_widget = QWidget()
self.setCentralWidget(central_widget)
main_layout = QVBoxLayout(central_widget)
main_layout.setContentsMargins(10, 10, 10, 10)
main_layout.setSpacing(10)
controls_layout = QVBoxLayout()
controls_layout.setSpacing(10)
row1_layout = QHBoxLayout()
row1_layout.setSpacing(15)
self.server_label = QLabel("Server URL:")
self.server_label.setFixedWidth(53)
self.server_entry = QLineEdit()
self.server_entry.setPlaceholderText("Enter Server URL...")
self.server_entry.setClearButtonEnabled(True)
self.username_label = QLabel("Username:")
self.username_label.setFixedWidth(50)
self.username_entry = QLineEdit()
self.username_entry.setPlaceholderText("Enter Username...")
self.username_entry.setClearButtonEnabled(True)
self.password_label = QLabel("Password:")
self.password_label.setFixedWidth(50)
self.password_entry = QLineEdit()
self.password_entry.setPlaceholderText("Enter Password...")
self.password_entry.setEchoMode(QLineEdit.Password)
self.password_entry.setClearButtonEnabled(True)
row1_layout.addWidget(self.server_label)
row1_layout.addWidget(self.server_entry)
row1_layout.addWidget(self.username_label)
row1_layout.addWidget(self.username_entry)
row1_layout.addWidget(self.password_label)
row1_layout.addWidget(self.password_entry)
buttons_layout = QHBoxLayout()
buttons_layout.setSpacing(15)
self.login_button = QPushButton("Login")
self.login_button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_DialogApplyButton))
self.login_button.clicked.connect(self.login)
self.m3u_plus_button = QPushButton("M3u_plus")
search_icon = QIcon.fromTheme("edit-find")
if search_icon.isNull():
search_icon = self.style().standardIcon(QtWidgets.QStyle.SP_FileDialogContentsView)
self.m3u_plus_button.setIcon(search_icon)
self.m3u_plus_button.clicked.connect(self.open_m3u_plus_dialog)
self.address_book_button = QPushButton("Address Book")
self.address_book_button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_DirIcon))
self.address_book_button.setToolTip("Manage Saved Credentials")
self.address_book_button.clicked.connect(self.open_address_book)
self.choose_player_button = QPushButton("Choose Media Player")
self.choose_player_button.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay))
self.choose_player_button.clicked.connect(self.choose_external_player)
buttons_layout.addWidget(self.login_button)
buttons_layout.addWidget(self.m3u_plus_button)
buttons_layout.addWidget(self.address_book_button)
buttons_layout.addWidget(self.choose_player_button)
checkbox_layout = QHBoxLayout()
checkbox_layout.setAlignment(Qt.AlignRight)
checkbox_layout.setSpacing(15)
self.http_method_checkbox = QCheckBox("Use POST Method")
self.http_method_checkbox.setToolTip("Check to use POST instead of GET for server requests")
checkbox_layout.addWidget(self.http_method_checkbox)
self.keep_on_top_checkbox = QCheckBox("Keep on top")
self.keep_on_top_checkbox.setToolTip("Keep the application on top of all windows")
self.keep_on_top_checkbox.stateChanged.connect(self.toggle_keep_on_top)
checkbox_layout.addWidget(self.keep_on_top_checkbox)
self.epg_checkbox = QCheckBox("Download EPG")
self.epg_checkbox.setToolTip("Check to download EPG data for channels")
self.epg_checkbox.stateChanged.connect(self.on_epg_checkbox_toggled)
checkbox_layout.addWidget(self.epg_checkbox)
self.font_size_label = QLabel("Font Size:")
self.font_size_spinbox = QSpinBox()
self.font_size_spinbox.setRange(8, 24)
self.font_size_spinbox.setValue(10)
self.font_size_spinbox.setToolTip("Set the font size for playlist items")
self.font_size_spinbox.valueChanged.connect(self.update_font_size)
self.font_size_spinbox.setFixedWidth(60)
self.default_font_size = 10
checkbox_layout.addWidget(self.font_size_label)
checkbox_layout.addWidget(self.font_size_spinbox)
controls_layout.addLayout(row1_layout)
controls_layout.addLayout(buttons_layout)
controls_layout.addLayout(checkbox_layout)
main_layout.addLayout(controls_layout)
content_widget = QWidget()
content_layout = QVBoxLayout(content_widget)
content_layout.setSpacing(10)
self.tab_widget = QTabWidget()
content_layout.addWidget(self.tab_widget)
self.tab_icon_size = QSize(24, 24)
live_icon = self.style().standardIcon(QtWidgets.QStyle.SP_MediaVolume)
movies_icon = self.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay)
series_icon = self.style().standardIcon(QtWidgets.QStyle.SP_DirIcon)
self.live_tab = QWidget()
self.movies_tab = QWidget()
self.series_tab = QWidget()
self.tab_widget.addTab(self.live_tab, live_icon, "LIVE")
self.tab_widget.addTab(self.movies_tab, movies_icon, "Movies")
self.tab_widget.addTab(self.series_tab, series_icon, "Series")
self.live_layout = QVBoxLayout(self.live_tab)
self.movies_layout = QVBoxLayout(self.movies_tab)
self.series_layout = QVBoxLayout(self.series_tab)
self.search_bar_live = QLineEdit()
self.search_bar_live.setPlaceholderText("Search Live Channels...")
self.search_bar_live.setClearButtonEnabled(True)
self.add_search_icon(self.search_bar_live)
self.search_bar_live.textChanged.connect(lambda text: self.search_in_list('LIVE', text))
self.search_bar_movies = QLineEdit()
self.search_bar_movies.setPlaceholderText("Search Movies...")
self.search_bar_movies.setClearButtonEnabled(True)
self.add_search_icon(self.search_bar_movies)
self.search_bar_movies.textChanged.connect(lambda text: self.search_in_list('Movies', text))
self.search_bar_series = QLineEdit()
self.search_bar_series.setPlaceholderText("Search Series...")
self.search_bar_series.setClearButtonEnabled(True)
self.add_search_icon(self.search_bar_series)
self.search_bar_series.textChanged.connect(lambda text: self.search_in_list('Series', text))
self.add_search_bar(self.live_layout, self.search_bar_live)
self.add_search_bar(self.movies_layout, self.search_bar_movies)
self.add_search_bar(self.series_layout, self.search_bar_series)
self.channel_list_live = QListWidget()
self.channel_list_movies = QListWidget()
self.channel_list_series = QListWidget()
standard_icon_size = QSize(24, 24)
for list_widget in [self.channel_list_live, self.channel_list_movies, self.channel_list_series]:
list_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
list_widget.setIconSize(standard_icon_size)
list_widget.setStyleSheet("""
QListWidget::item {
padding-top: 5px;
padding-bottom: 5px;
}
""")
self.live_layout.addWidget(self.channel_list_live)
self.movies_layout.addWidget(self.channel_list_movies)
self.series_layout.addWidget(self.channel_list_series)
self.list_widgets = {
'LIVE': self.channel_list_live,
'Movies': self.channel_list_movies,
'Series': self.channel_list_series,
}
self.tab_widget.currentChanged.connect(self.on_tab_change)
self.channel_list_live.itemDoubleClicked.connect(self.channel_item_double_clicked)
self.channel_list_movies.itemDoubleClicked.connect(self.channel_item_double_clicked)
self.channel_list_series.itemDoubleClicked.connect(self.channel_item_double_clicked)
self.info_tab = QWidget()
self.info_tab_layout = QVBoxLayout(self.info_tab)
self.result_display = QTextEdit(self.info_tab)
self.result_display.setReadOnly(True)
default_font = QFont()
default_font.setPointSize(self.default_font_size)
self.result_display.setFont(default_font)
self.info_tab_layout.addWidget(self.result_display)
info_icon = self.style().standardIcon(QtWidgets.QStyle.SP_MessageBoxInformation)
self.tab_widget.addTab(self.info_tab, info_icon, "Info")
self.info_tab_initialized = False
main_layout.addWidget(content_widget)
self.progress_bar = QtWidgets.QProgressBar()
self.progress_bar.setValue(0)
self.progress_bar.setMaximum(100)
self.progress_bar.setFixedHeight(25)
self.progress_bar.setTextVisible(True)
main_layout.addWidget(self.progress_bar)
self.playlist_progress_animation = QPropertyAnimation(self.progress_bar, b"value")
self.playlist_progress_animation.setDuration(1000) # longer duration for smoother animation
self.playlist_progress_animation.setEasingCurve(QEasingCurve.InOutQuad)
def add_search_icon(self, search_bar):
search_icon = QIcon.fromTheme("edit-find")
if search_icon.isNull():
search_icon = self.style().standardIcon(QtWidgets.QStyle.SP_FileDialogContentsView)
search_bar.addAction(search_icon, QLineEdit.LeadingPosition)
def toggle_keep_on_top(self, state):
if state == Qt.Checked:
self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
else:
self.setWindowFlags(self.windowFlags() & ~Qt.WindowStaysOnTopHint)
self.show()
def add_search_bar(self, layout, search_bar):
layout.addWidget(search_bar)
def get_http_method(self):
return 'POST' if self.http_method_checkbox.isChecked() else 'GET'
def make_request(self, method, url, params=None, timeout=10):
headers = {'User-Agent': CUSTOM_USER_AGENT}
if method == 'POST':
return requests.post(url, data=params, headers=headers, timeout=timeout)
else:
return requests.get(url, params=params, headers=headers, timeout=timeout)
def open_m3u_plus_dialog(self):
text, ok = QtWidgets.QInputDialog.getText(self, 'M3u_plus Login', 'Enter m3u_plus URL:')
if ok and text:
m3u_plus_url = text.strip()
self.extract_credentials_from_m3u_plus_url(m3u_plus_url)
self.login()
def update_font_size(self, value):
self.default_font_size = value
for tab_name, list_widget in self.list_widgets.items():
for i in range(list_widget.count()):
item = list_widget.item(i)
font = item.font()
font.setPointSize(value)
item.setFont(font)
font = QFont()
font.setPointSize(value)
self.result_display.setFont(font)
def extract_credentials_from_m3u_plus_url(self, url):
try:
pattern = r'(http[s]?://[^/]+)/get\.php\?username=([^&]*)&password=([^&]*)&type=(m3u_plus|m3u|&output=m3u8)'
match = re.match(pattern, url)
if match:
self.server = match.group(1)
self.username = match.group(2)
self.password = match.group(3)
self.server_entry.setText(self.server)
self.username_entry.setText(self.username)
self.password_entry.setText(self.password)
else:
self.animate_progress(0, 100, "Invalid m3u_plus or m3u URL")
except Exception as e:
print(f"Error extracting credentials: {e}")
self.animate_progress(0, 100, "Error extracting credentials")
def set_progress_text(self, text):
self.progress_bar.setFormat(text)
def animate_progress(self, start, end, text):
self.playlist_progress_animation.stop()
self.playlist_progress_animation.setStartValue(start)
self.playlist_progress_animation.setEndValue(end)
self.set_progress_text(text)
self.playlist_progress_animation.start()
def reset_progress_bar(self):
self.playlist_progress_animation.stop()
self.progress_bar.setValue(0)
self.set_progress_text("")
def login(self):
# When logging into another server, reset the progress bar
self.reset_progress_bar()
self.epg_data = {}
self.channel_id_to_names = {}
self.epg_last_updated = None
for tab_name, list_widget in self.list_widgets.items():
list_widget.clear()
cache_file = 'epg_cache1.xml'
if os.path.exists(cache_file):
try:
os.remove(cache_file)
except Exception as e:
print(f"Error deleting EPG cache: {e}")
self.animate_progress(0, 100, "Error deleting EPG cache")
return
server = self.server_entry.text().strip()
username = self.username_entry.text().strip()
password = self.password_entry.text().strip()
if not server or not username or not password:
self.animate_progress(0, 100, "Please fill all fields")
return
# Start loading playlist from 0 to 100
self.reset_progress_bar()
self.animate_progress(0, 30, "Loading playlist...")
self.fetch_categories_only(server, username, password)
def fetch_categories_only(self, server, username, password):
try:
http_method = self.get_http_method()
params = {
'username': username,
'password': password,
'action': 'get_live_categories'
}
categories_url = f"{server}/player_api.php"
live_response = self.make_request(http_method, categories_url, params, timeout=10)
live_response.raise_for_status()
params['action'] = 'get_vod_categories'
movies_response = self.make_request(http_method, categories_url, params, timeout=10)
movies_response.raise_for_status()
params['action'] = 'get_series_categories'
series_response = self.make_request(http_method, categories_url, params, timeout=10)
series_response.raise_for_status()
self.groups = {
"LIVE": live_response.json(),
"Movies": movies_response.json(),
"Series": series_response.json(),
}
self.server = server
self.username = username
self.password = password
self.login_type = 'xtream'
self.navigation_stacks = {'LIVE': [], 'Movies': [], 'Series': []}
self.top_level_scroll_positions = {'LIVE': 0, 'Movies': 0, 'Series': 0}
self.update_category_lists('LIVE')
self.update_category_lists('Movies')
self.update_category_lists('Series')
self.fetch_additional_data(server, username, password)
# Playlist loading complete
self.animate_progress(self.progress_bar.value(), 100, "Playlist loaded")
# After playlist is fully loaded, if EPG is checked and not loaded, load EPG now
if self.epg_checkbox.isChecked() and not self.epg_data:
# Reset to 0 before loading EPG
self.reset_progress_bar()
self.animate_progress(0, 50, "Loading EPG data...")
self.load_epg_data_async()
except requests.exceptions.Timeout:
print("Request timed out")
self.animate_progress(self.progress_bar.value(), 100, "Login timed out")
except requests.RequestException as e:
print(f"Network error: {e}")
self.animate_progress(self.progress_bar.value(), 100, "Network Error")
except ValueError as e:
print(f"JSON decode error: {e}")
self.animate_progress(self.progress_bar.value(), 100, "Invalid server response")
except Exception as e:
print(f"Error fetching categories: {e}")
self.animate_progress(self.progress_bar.value(), 100, "Error fetching categories")
def fetch_additional_data(self, server, username, password):
try:
if not server.startswith("http://") and not server.startswith("https://"):
server = f"http://{server}"
headers = {'User-Agent': CUSTOM_USER_AGENT}
payload = {'username': username, 'password': password}
url = f"{server}/player_api.php"
response = requests.post(url, headers=headers, data=payload, timeout=10)
response.raise_for_status()
additional_data = response.json()
user_info = additional_data.get("user_info", {})
server_info = additional_data.get("server_info", {})
hostname = server_info.get("url", server.replace("http://", "").replace("https://", ""))
port = server_info.get("port", 25461)
host = f"http://{hostname}:{port}"
username = user_info.get("username", "Unknown")
password = user_info.get("password", "Unknown")
max_connections = user_info.get("max_connections", "Unlimited")
active_connections = user_info.get("active_cons", "0")
trial = "Yes" if user_info.get("is_trial") == "1" else "No"
expire_timestamp = user_info.get("exp_date")
expiry = (
datetime.fromtimestamp(int(expire_timestamp)).strftime("%B %d, %Y")
if expire_timestamp else "Unlimited"
)
status = user_info.get("status", "Unknown")
created_at_timestamp = user_info.get("created_at", "Unknown")
created_at = (
datetime.fromtimestamp(int(created_at_timestamp)).strftime("%B %d, %Y")
if created_at_timestamp and created_at_timestamp.isdigit() else "Unknown"
)
timezone = server_info.get("timezone", "Unknown")
formatted_data = (
f"Host: {host}\n"
f"Username: {username}\n"
f"Password: {password}\n"
f"Max Connections: {max_connections}\n"
f"Active Connections: {active_connections}\n"
f"Timezone: {timezone}\n"
f"Trial: {trial}\n"
f"Status: {status}\n"
f"Created At: {created_at}\n"
f"Expiry: {expiry}\n"
)
self.result_display.setText(formatted_data)
self.info_tab_initialized = True
except Exception as e:
print(f"Error fetching additional data: {e}")
def load_epg_data_async(self):
if not self.server or not self.username or not self.password:
# Can't load EPG if not logged in
return
http_method = self.get_http_method()
epg_worker = EPGWorker(self.server, self.username, self.password, http_method)
epg_worker.signals.finished.connect(self.on_epg_loaded)
epg_worker.signals.error.connect(self.on_epg_error)
self.threadpool.start(epg_worker)
def on_epg_loaded(self, epg_data, channel_id_to_names):
self.epg_data = epg_data
self.channel_id_to_names = channel_id_to_names
name_to_id = {}
for cid, names in channel_id_to_names.items():
for n in names:
if n not in name_to_id:
name_to_id[n] = cid
self.epg_name_map = name_to_id
# EPG done
self.animate_progress(self.progress_bar.value(), 100, "EPG data loaded")
def on_epg_error(self, error_message):
print(f"Error fetching EPG data: {error_message}")
self.animate_progress(self.progress_bar.value(), 100, "Error fetching EPG data")
def channel_item_double_clicked(self, item):
try:
sender = self.sender()
category = {
self.channel_list_live: 'LIVE',
self.channel_list_movies: 'Movies',
self.channel_list_series: 'Series'
}.get(sender)
if not category:
return
selected_item = sender.currentItem()
if not selected_item:
return
selected_text = selected_item.text()
list_widget = self.get_list_widget(category)
current_scroll_position = list_widget.verticalScrollBar().value()
stack = self.navigation_stacks[category]
if stack:
stack[-1]['scroll_position'] = current_scroll_position
else:
self.top_level_scroll_positions[category] = current_scroll_position
self.handle_xtream_double_click(selected_item, selected_text, category, sender)
except Exception as e:
print(f"Error occurred while handling double click: {e}")
def update_category_lists(self, tab_name):
if tab_name == 'LIVE':
self.search_bar_live.clear()
elif tab_name == 'Movies':
self.search_bar_movies.clear()
elif tab_name == 'Series':
self.search_bar_series.clear()
try:
list_widget = self.get_list_widget(tab_name)
list_widget.clear()
if self.navigation_stacks[tab_name]:
go_back_item = QListWidgetItem("Go Back")
go_back_item.setIcon(self.go_back_icon)
list_widget.addItem(go_back_item)
if tab_name == 'LIVE':
channel_icon = self.live_channel_icon
elif tab_name == 'Movies':
channel_icon = self.movies_channel_icon
elif tab_name == 'Series':
channel_icon = self.series_channel_icon
else:
channel_icon = self.style().standardIcon(QtWidgets.QStyle.SP_FileIcon)
group_list = self.groups[tab_name]
category_names = sorted(group["category_name"] for group in group_list)
items = []
for category_name in category_names:
item = QListWidgetItem(category_name)
item.setIcon(channel_icon)
items.append(item)
items.sort(key=lambda x: x.text())
for item in items:
list_widget.addItem(item)
scroll_position = self.top_level_scroll_positions.get(tab_name, 0)
list_widget.verticalScrollBar().setValue(scroll_position)
except Exception as e:
print(f"Error updating category lists: {e}")
self.animate_progress(self.progress_bar.value(), 100, "Error updating lists")
def fetch_channels(self, category_name, tab_name):
try:
category_id = next(g["category_id"] for g in self.groups[tab_name] if g["category_name"] == category_name)
list_widget = self.get_list_widget(tab_name)
current_scroll_position = list_widget.verticalScrollBar().value()
stack = self.navigation_stacks[tab_name]
if stack:
stack[-1]['scroll_position'] = current_scroll_position
else:
self.top_level_scroll_positions[tab_name] = current_scroll_position
http_method = self.get_http_method()
params = {
'username': self.username,
'password': self.password,
'action': '',
'category_id': category_id
}
if tab_name == "LIVE":
params['action'] = 'get_live_streams'
stream_type = "live"
elif tab_name == "Movies":
params['action'] = 'get_vod_streams'
stream_type = "movie"
streams_url = f"{self.server}/player_api.php"
response = self.make_request(http_method, streams_url, params)
response.raise_for_status()
data = response.json()
if not isinstance(data, list):
raise ValueError("Expected a list of channels")
self.entries_per_tab[tab_name] = data
entries = self.entries_per_tab[tab_name]
for entry in entries:
stream_id = entry.get("stream_id")
epg_channel_id = entry.get("epg_channel_id")
if epg_channel_id:
epg_channel_id = epg_channel_id.strip().lower()
else:
epg_channel_id = None
container_extension = entry.get("container_extension", "m3u8")
if stream_id:
entry["url"] = f"{self.server}/{stream_type}/{self.username}/{self.password}/{stream_id}.{container_extension}"
else:
entry["url"] = None
entry["epg_channel_id"] = epg_channel_id
self.navigation_stacks[tab_name].append({'level': 'channels', 'data': {'tab_name': tab_name, 'entries': entries}, 'scroll_position': 0})
self.show_channels(list_widget, tab_name)
except requests.RequestException as e:
print(f"Network error: {e}")
self.animate_progress(self.progress_bar.value(), 100, "Network Error")
except ValueError as e:
print(f"Data validation error: {e}")
self.animate_progress(self.progress_bar.value(), 100, "Invalid channel data received")
except Exception as e:
print(f"Error fetching channels: {e}")
self.animate_progress(self.progress_bar.value(), 100, "Error fetching channels")
def handle_xtream_double_click(self, selected_item, selected_text, tab_name, sender):
try:
list_widget = self.get_list_widget(tab_name)
stack = self.navigation_stacks[tab_name]
if selected_text == "Go Back":
if stack:
stack.pop()
if stack:
last_level = stack[-1]
level = last_level['level']
data = last_level['data']
scroll_position = last_level.get('scroll_position', 0)
if level == 'channels':
self.entries_per_tab[tab_name] = data['entries']
self.show_channels(list_widget, tab_name)
list_widget.verticalScrollBar().setValue(scroll_position)
elif level == 'series_categories':
self.show_series_in_category(data['series_list'], restore_scroll_position=True, scroll_position=scroll_position)
elif level == 'series':
self.show_seasons(data['seasons'], restore_scroll_position=True, scroll_position=scroll_position)
elif level == 'season':
self.show_episodes(data['episodes'], restore_scroll_position=True, scroll_position=scroll_position)
else:
self.update_category_lists(tab_name)
list_widget.verticalScrollBar().setValue(self.top_level_scroll_positions.get(tab_name, 0))
else:
self.update_category_lists(tab_name)
list_widget.verticalScrollBar().setValue(self.top_level_scroll_positions.get(tab_name, 0))
return
if tab_name != "Series":
if selected_text in [group["category_name"] for group in self.groups[tab_name]]:
self.fetch_channels(selected_text, tab_name)
else:
selected_entry = selected_item.data(Qt.UserRole)
if selected_entry and "url" in selected_entry:
self.play_channel(selected_entry)
return
# Series logic
if tab_name == "Series":
if not stack:
if selected_text in [group["category_name"] for group in self.groups["Series"]]:
self.fetch_series_in_category(selected_text)
return
elif stack[-1]['level'] == 'series_categories':
series_entry = selected_item.data(Qt.UserRole)
if series_entry and "series_id" in series_entry:
self.fetch_seasons(series_entry)
return
elif stack[-1]['level'] == 'series':
season_number = selected_item.data(Qt.UserRole)
series_entry = stack[-1]['data']['series_entry']
self.fetch_episodes(series_entry, season_number)
return
elif stack[-1]['level'] == 'season':
selected_entry = selected_item.data(Qt.UserRole)
if selected_entry and "url" in selected_entry:
self.play_channel(selected_entry)