StreamingCommunity 1.7.6__py3-none-any.whl → 1.8.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of StreamingCommunity might be problematic. Click here for more details.

Files changed (93) hide show
  1. StreamingCommunity/Src/Api/Player/Helper/Vixcloud/js_parser.py +4 -1
  2. StreamingCommunity/Src/Api/Player/Helper/Vixcloud/util.py +166 -166
  3. StreamingCommunity/Src/Api/Player/ddl.py +89 -89
  4. StreamingCommunity/Src/Api/Player/maxstream.py +151 -151
  5. StreamingCommunity/Src/Api/Player/supervideo.py +193 -193
  6. StreamingCommunity/Src/Api/Player/vixcloud.py +224 -212
  7. StreamingCommunity/Src/Api/Site/1337xx/__init__.py +50 -50
  8. StreamingCommunity/Src/Api/Site/1337xx/costant.py +14 -14
  9. StreamingCommunity/Src/Api/Site/1337xx/site.py +83 -83
  10. StreamingCommunity/Src/Api/Site/1337xx/title.py +66 -66
  11. StreamingCommunity/Src/Api/Site/altadefinizione/__init__.py +50 -50
  12. StreamingCommunity/Src/Api/Site/altadefinizione/costant.py +14 -14
  13. StreamingCommunity/Src/Api/Site/altadefinizione/film.py +69 -69
  14. StreamingCommunity/Src/Api/Site/altadefinizione/site.py +86 -86
  15. StreamingCommunity/Src/Api/Site/animeunity/__init__.py +50 -50
  16. StreamingCommunity/Src/Api/Site/animeunity/costant.py +15 -15
  17. StreamingCommunity/Src/Api/Site/animeunity/film_serie.py +131 -131
  18. StreamingCommunity/Src/Api/Site/animeunity/site.py +164 -164
  19. StreamingCommunity/Src/Api/Site/bitsearch/__init__.py +51 -51
  20. StreamingCommunity/Src/Api/Site/bitsearch/costant.py +15 -15
  21. StreamingCommunity/Src/Api/Site/bitsearch/site.py +84 -84
  22. StreamingCommunity/Src/Api/Site/bitsearch/title.py +47 -47
  23. StreamingCommunity/Src/Api/Site/cb01new/__init__.py +51 -51
  24. StreamingCommunity/Src/Api/Site/cb01new/costant.py +15 -15
  25. StreamingCommunity/Src/Api/Site/cb01new/film.py +69 -69
  26. StreamingCommunity/Src/Api/Site/cb01new/site.py +74 -74
  27. StreamingCommunity/Src/Api/Site/ddlstreamitaly/__init__.py +57 -57
  28. StreamingCommunity/Src/Api/Site/ddlstreamitaly/costant.py +16 -16
  29. StreamingCommunity/Src/Api/Site/ddlstreamitaly/series.py +142 -142
  30. StreamingCommunity/Src/Api/Site/ddlstreamitaly/site.py +92 -92
  31. StreamingCommunity/Src/Api/Site/ddlstreamitaly/util/ScrapeSerie.py +82 -82
  32. StreamingCommunity/Src/Api/Site/guardaserie/__init__.py +52 -52
  33. StreamingCommunity/Src/Api/Site/guardaserie/costant.py +15 -15
  34. StreamingCommunity/Src/Api/Site/guardaserie/series.py +195 -195
  35. StreamingCommunity/Src/Api/Site/guardaserie/site.py +84 -84
  36. StreamingCommunity/Src/Api/Site/guardaserie/util/ScrapeSerie.py +110 -110
  37. StreamingCommunity/Src/Api/Site/mostraguarda/__init__.py +48 -48
  38. StreamingCommunity/Src/Api/Site/mostraguarda/costant.py +14 -14
  39. StreamingCommunity/Src/Api/Site/mostraguarda/film.py +94 -94
  40. StreamingCommunity/Src/Api/Site/piratebays/__init__.py +50 -50
  41. StreamingCommunity/Src/Api/Site/piratebays/costant.py +14 -14
  42. StreamingCommunity/Src/Api/Site/piratebays/site.py +88 -88
  43. StreamingCommunity/Src/Api/Site/piratebays/title.py +45 -45
  44. StreamingCommunity/Src/Api/Site/streamingcommunity/__init__.py +55 -55
  45. StreamingCommunity/Src/Api/Site/streamingcommunity/costant.py +14 -14
  46. StreamingCommunity/Src/Api/Site/streamingcommunity/film.py +70 -70
  47. StreamingCommunity/Src/Api/Site/streamingcommunity/series.py +203 -203
  48. StreamingCommunity/Src/Api/Site/streamingcommunity/site.py +125 -125
  49. StreamingCommunity/Src/Api/Template/Class/SearchType.py +101 -101
  50. StreamingCommunity/Src/Api/Template/Util/__init__.py +4 -4
  51. StreamingCommunity/Src/Api/Template/Util/get_domain.py +137 -137
  52. StreamingCommunity/Src/Api/Template/Util/manage_ep.py +153 -153
  53. StreamingCommunity/Src/Api/Template/Util/recall_search.py +37 -37
  54. StreamingCommunity/Src/Api/Template/__init__.py +2 -2
  55. StreamingCommunity/Src/Api/Template/site.py +87 -87
  56. StreamingCommunity/Src/Lib/Downloader/HLS/downloader.py +968 -968
  57. StreamingCommunity/Src/Lib/Downloader/HLS/proxyes.py +110 -110
  58. StreamingCommunity/Src/Lib/Downloader/HLS/segments.py +540 -540
  59. StreamingCommunity/Src/Lib/Downloader/MP4/downloader.py +156 -156
  60. StreamingCommunity/Src/Lib/Downloader/TOR/downloader.py +222 -222
  61. StreamingCommunity/Src/Lib/Downloader/__init__.py +4 -4
  62. StreamingCommunity/Src/Lib/Driver/driver_1.py +76 -76
  63. StreamingCommunity/Src/Lib/FFmpeg/__init__.py +4 -4
  64. StreamingCommunity/Src/Lib/FFmpeg/capture.py +170 -170
  65. StreamingCommunity/Src/Lib/FFmpeg/command.py +292 -292
  66. StreamingCommunity/Src/Lib/FFmpeg/util.py +241 -241
  67. StreamingCommunity/Src/Lib/M3U8/__init__.py +5 -5
  68. StreamingCommunity/Src/Lib/M3U8/decryptor.py +128 -128
  69. StreamingCommunity/Src/Lib/M3U8/estimator.py +172 -172
  70. StreamingCommunity/Src/Lib/M3U8/parser.py +666 -666
  71. StreamingCommunity/Src/Lib/M3U8/url_fixer.py +51 -51
  72. StreamingCommunity/Src/Lib/TMBD/__init__.py +1 -1
  73. StreamingCommunity/Src/Lib/TMBD/obj_tmbd.py +39 -39
  74. StreamingCommunity/Src/Lib/TMBD/tmdb.py +345 -345
  75. StreamingCommunity/Src/Upload/update.py +64 -64
  76. StreamingCommunity/Src/Upload/version.py +5 -5
  77. StreamingCommunity/Src/Util/_jsonConfig.py +204 -204
  78. StreamingCommunity/Src/Util/call_stack.py +42 -42
  79. StreamingCommunity/Src/Util/color.py +20 -20
  80. StreamingCommunity/Src/Util/console.py +12 -12
  81. StreamingCommunity/Src/Util/headers.py +147 -147
  82. StreamingCommunity/Src/Util/logger.py +53 -53
  83. StreamingCommunity/Src/Util/message.py +46 -46
  84. StreamingCommunity/Src/Util/os.py +417 -417
  85. StreamingCommunity/Src/Util/table.py +163 -163
  86. StreamingCommunity/run.py +196 -196
  87. {StreamingCommunity-1.7.6.dist-info → StreamingCommunity-1.8.0.dist-info}/METADATA +1 -1
  88. StreamingCommunity-1.8.0.dist-info/RECORD +97 -0
  89. StreamingCommunity-1.7.6.dist-info/RECORD +0 -97
  90. {StreamingCommunity-1.7.6.dist-info → StreamingCommunity-1.8.0.dist-info}/LICENSE +0 -0
  91. {StreamingCommunity-1.7.6.dist-info → StreamingCommunity-1.8.0.dist-info}/WHEEL +0 -0
  92. {StreamingCommunity-1.7.6.dist-info → StreamingCommunity-1.8.0.dist-info}/entry_points.txt +0 -0
  93. {StreamingCommunity-1.7.6.dist-info → StreamingCommunity-1.8.0.dist-info}/top_level.txt +0 -0
@@ -1,129 +1,129 @@
1
- # 03.04.24
2
-
3
- import sys
4
- import logging
5
- import subprocess
6
- import importlib.util
7
-
8
-
9
- # Internal utilities
10
- from StreamingCommunity.Src.Util.console import console
11
-
12
-
13
- # Check if Crypto module is installed
14
- crypto_spec = importlib.util.find_spec("Crypto")
15
- crypto_installed = crypto_spec is not None
16
-
17
-
18
- if crypto_installed:
19
- logging.info("Decrypy use: Crypto")
20
- from Crypto.Cipher import AES # type: ignore
21
- from Crypto.Util.Padding import unpad # type: ignore
22
-
23
- class M3U8_Decryption:
24
- """
25
- Class for decrypting M3U8 playlist content using AES encryption when the Crypto module is available.
26
- """
27
- def __init__(self, key: bytes, iv: bytes, method: str) -> None:
28
- """
29
- Initialize the M3U8_Decryption object.
30
-
31
- Parameters:
32
- - key (bytes): The encryption key.
33
- - iv (bytes): The initialization vector (IV).
34
- - method (str): The encryption method.
35
- """
36
- self.key = key
37
- if "0x" in str(iv):
38
- self.iv = bytes.fromhex(iv.replace("0x", ""))
39
- else:
40
- self.iv = iv
41
- self.method = method
42
- logging.info(f"Decrypt add: ('key': {self.key}, 'iv': {self.iv}, 'method': {self.method})")
43
-
44
- def decrypt(self, ciphertext: bytes) -> bytes:
45
- """
46
- Decrypt the ciphertext using the specified encryption method.
47
-
48
- Parameters:
49
- - ciphertext (bytes): The encrypted content to decrypt.
50
-
51
- Returns:
52
- bytes: The decrypted content.
53
- """
54
- if self.method == "AES":
55
- cipher = AES.new(self.key, AES.MODE_ECB)
56
- decrypted_data = cipher.decrypt(ciphertext)
57
- return unpad(decrypted_data, AES.block_size)
58
-
59
- elif self.method == "AES-128":
60
- cipher = AES.new(self.key[:16], AES.MODE_CBC, iv=self.iv)
61
- decrypted_data = cipher.decrypt(ciphertext)
62
- return unpad(decrypted_data, AES.block_size)
63
-
64
- elif self.method == "AES-128-CTR":
65
- cipher = AES.new(self.key[:16], AES.MODE_CTR, nonce=self.iv)
66
- return cipher.decrypt(ciphertext)
67
-
68
- else:
69
- raise ValueError("Invalid or unsupported method")
70
-
71
- else:
72
-
73
- # Check if openssl command is available
74
- try:
75
- openssl_available = subprocess.run(["openssl", "version"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0
76
- logging.info("Decrypy use: OPENSSL")
77
- except:
78
- openssl_available = False
79
-
80
- if not openssl_available:
81
- console.log("[red]Neither python library: pycryptodome nor openssl software is installed. Please install either one of them. Read readme.md [Requirement].")
82
- sys.exit(0)
83
-
84
- class M3U8_Decryption:
85
- """
86
- Class for decrypting M3U8 playlist content using OpenSSL when the Crypto module is not available.
87
- """
88
- def __init__(self, key: bytes, iv: bytes, method: str) -> None:
89
- """
90
- Initialize the M3U8_Decryption object.
91
-
92
- Parameters:
93
- - key (bytes): The encryption key.
94
- - iv (bytes): The initialization vector (IV).
95
- - method (str): The encryption method.
96
- """
97
- self.key = key
98
- if "0x" in str(iv):
99
- self.iv = bytes.fromhex(iv.replace("0x", ""))
100
- else:
101
- self.iv = iv
102
- self.method = method
103
- logging.info(f"Decrypt add: ('key': {self.key}, 'iv': {self.iv}, 'method': {self.method})")
104
-
105
- def decrypt(self, ciphertext: bytes) -> bytes:
106
- """
107
- Decrypt the ciphertext using the specified encryption method.
108
-
109
- Parameters:
110
- - ciphertext (bytes): The encrypted content to decrypt.
111
-
112
- Returns:
113
- bytes: The decrypted content.
114
- """
115
- if self.method == "AES":
116
- openssl_cmd = f'openssl enc -d -aes-256-ecb -K {self.key.hex()} -nosalt'
117
- elif self.method == "AES-128":
118
- openssl_cmd = f'openssl enc -d -aes-128-cbc -K {self.key[:16].hex()} -iv {self.iv.hex()}'
119
- elif self.method == "AES-128-CTR":
120
- openssl_cmd = f'openssl enc -d -aes-128-ctr -K {self.key[:16].hex()} -iv {self.iv.hex()}'
121
- else:
122
- raise ValueError("Invalid or unsupported method")
123
-
124
- try:
125
- decrypted_data = subprocess.check_output(openssl_cmd.split(), input=ciphertext, stderr=subprocess.STDOUT)
126
- except subprocess.CalledProcessError as e:
127
- raise ValueError(f"Decryption failed: {e.output.decode()}")
128
-
1
+ # 03.04.24
2
+
3
+ import sys
4
+ import logging
5
+ import subprocess
6
+ import importlib.util
7
+
8
+
9
+ # Internal utilities
10
+ from StreamingCommunity.Src.Util.console import console
11
+
12
+
13
+ # Check if Crypto module is installed
14
+ crypto_spec = importlib.util.find_spec("Crypto")
15
+ crypto_installed = crypto_spec is not None
16
+
17
+
18
+ if crypto_installed:
19
+ logging.info("Decrypy use: Crypto")
20
+ from Crypto.Cipher import AES # type: ignore
21
+ from Crypto.Util.Padding import unpad # type: ignore
22
+
23
+ class M3U8_Decryption:
24
+ """
25
+ Class for decrypting M3U8 playlist content using AES encryption when the Crypto module is available.
26
+ """
27
+ def __init__(self, key: bytes, iv: bytes, method: str) -> None:
28
+ """
29
+ Initialize the M3U8_Decryption object.
30
+
31
+ Parameters:
32
+ - key (bytes): The encryption key.
33
+ - iv (bytes): The initialization vector (IV).
34
+ - method (str): The encryption method.
35
+ """
36
+ self.key = key
37
+ if "0x" in str(iv):
38
+ self.iv = bytes.fromhex(iv.replace("0x", ""))
39
+ else:
40
+ self.iv = iv
41
+ self.method = method
42
+ logging.info(f"Decrypt add: ('key': {self.key}, 'iv': {self.iv}, 'method': {self.method})")
43
+
44
+ def decrypt(self, ciphertext: bytes) -> bytes:
45
+ """
46
+ Decrypt the ciphertext using the specified encryption method.
47
+
48
+ Parameters:
49
+ - ciphertext (bytes): The encrypted content to decrypt.
50
+
51
+ Returns:
52
+ bytes: The decrypted content.
53
+ """
54
+ if self.method == "AES":
55
+ cipher = AES.new(self.key, AES.MODE_ECB)
56
+ decrypted_data = cipher.decrypt(ciphertext)
57
+ return unpad(decrypted_data, AES.block_size)
58
+
59
+ elif self.method == "AES-128":
60
+ cipher = AES.new(self.key[:16], AES.MODE_CBC, iv=self.iv)
61
+ decrypted_data = cipher.decrypt(ciphertext)
62
+ return unpad(decrypted_data, AES.block_size)
63
+
64
+ elif self.method == "AES-128-CTR":
65
+ cipher = AES.new(self.key[:16], AES.MODE_CTR, nonce=self.iv)
66
+ return cipher.decrypt(ciphertext)
67
+
68
+ else:
69
+ raise ValueError("Invalid or unsupported method")
70
+
71
+ else:
72
+
73
+ # Check if openssl command is available
74
+ try:
75
+ openssl_available = subprocess.run(["openssl", "version"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0
76
+ logging.info("Decrypy use: OPENSSL")
77
+ except:
78
+ openssl_available = False
79
+
80
+ if not openssl_available:
81
+ console.log("[red]Neither python library: pycryptodome nor openssl software is installed. Please install either one of them. Read readme.md [Requirement].")
82
+ sys.exit(0)
83
+
84
+ class M3U8_Decryption:
85
+ """
86
+ Class for decrypting M3U8 playlist content using OpenSSL when the Crypto module is not available.
87
+ """
88
+ def __init__(self, key: bytes, iv: bytes, method: str) -> None:
89
+ """
90
+ Initialize the M3U8_Decryption object.
91
+
92
+ Parameters:
93
+ - key (bytes): The encryption key.
94
+ - iv (bytes): The initialization vector (IV).
95
+ - method (str): The encryption method.
96
+ """
97
+ self.key = key
98
+ if "0x" in str(iv):
99
+ self.iv = bytes.fromhex(iv.replace("0x", ""))
100
+ else:
101
+ self.iv = iv
102
+ self.method = method
103
+ logging.info(f"Decrypt add: ('key': {self.key}, 'iv': {self.iv}, 'method': {self.method})")
104
+
105
+ def decrypt(self, ciphertext: bytes) -> bytes:
106
+ """
107
+ Decrypt the ciphertext using the specified encryption method.
108
+
109
+ Parameters:
110
+ - ciphertext (bytes): The encrypted content to decrypt.
111
+
112
+ Returns:
113
+ bytes: The decrypted content.
114
+ """
115
+ if self.method == "AES":
116
+ openssl_cmd = f'openssl enc -d -aes-256-ecb -K {self.key.hex()} -nosalt'
117
+ elif self.method == "AES-128":
118
+ openssl_cmd = f'openssl enc -d -aes-128-cbc -K {self.key[:16].hex()} -iv {self.iv.hex()}'
119
+ elif self.method == "AES-128-CTR":
120
+ openssl_cmd = f'openssl enc -d -aes-128-ctr -K {self.key[:16].hex()} -iv {self.iv.hex()}'
121
+ else:
122
+ raise ValueError("Invalid or unsupported method")
123
+
124
+ try:
125
+ decrypted_data = subprocess.check_output(openssl_cmd.split(), input=ciphertext, stderr=subprocess.STDOUT)
126
+ except subprocess.CalledProcessError as e:
127
+ raise ValueError(f"Decryption failed: {e.output.decode()}")
128
+
129
129
  return decrypted_data
@@ -1,173 +1,173 @@
1
- # 20.02.24
2
-
3
- import os
4
- import time
5
- import logging
6
- import threading
7
- from collections import deque
8
-
9
-
10
- # External libraries
11
- import psutil
12
- from tqdm import tqdm
13
-
14
-
15
- # Internal utilities
16
- from StreamingCommunity.Src.Util.color import Colors
17
- from StreamingCommunity.Src.Util.os import internet_manager
18
- from StreamingCommunity.Src.Util._jsonConfig import config_manager
19
-
20
-
21
- # Variable
22
- TQDM_USE_LARGE_BAR = config_manager.get_int('M3U8_DOWNLOAD', 'tqdm_use_large_bar')
23
-
24
-
25
- class M3U8_Ts_Estimator:
26
- def __init__(self, total_segments: int):
27
- """
28
- Initialize the M3U8_Ts_Estimator object.
29
-
30
- Parameters:
31
- - total_segments (int): Length of total segments to download.
32
- """
33
- self.ts_file_sizes = []
34
- self.now_downloaded_size = 0
35
- self.total_segments = total_segments
36
- self.lock = threading.Lock()
37
- self.speed = {"upload": "N/A", "download": "N/A"} # Default to N/A
38
- self.speed_thread = threading.Thread(target=self.capture_speed)
39
- self.speed_thread.daemon = True
40
- self.speed_thread.start()
41
-
42
- def add_ts_file(self, size: int, size_download: int, duration: float):
43
- """
44
- Add a file size to the list of file sizes.
45
-
46
- Parameters:
47
- - size (int): The size of the ts file to be added.
48
- - size_download (int): Single size of the ts file.
49
- - duration (float): Time to download segment file.
50
- """
51
- if size <= 0 or size_download <= 0 or duration <= 0:
52
- logging.error("Invalid input values: size=%d, size_download=%d, duration=%f", size, size_download, duration)
53
- return
54
-
55
- # Add total size bytes
56
- self.ts_file_sizes.append(size)
57
- self.now_downloaded_size += size_download
58
-
59
- def capture_speed(self, interval: float = 1):
60
- """
61
- Capture the internet speed periodically and store the values.
62
- """
63
- def get_network_io():
64
- """Get network I/O counters, handle missing psutil gracefully."""
65
- try:
66
- io_counters = psutil.net_io_counters()
67
- return io_counters
68
- except Exception as e:
69
- logging.warning(f"Unable to access network I/O counters: {e}")
70
- return None
71
-
72
- while True:
73
- old_value = get_network_io()
74
- if old_value is None: # If psutil is not available, continue with default values
75
- time.sleep(interval)
76
- continue
77
-
78
- time.sleep(interval)
79
- new_value = get_network_io()
80
- if new_value is None: # Handle again if psutil fails in the next call
81
- time.sleep(interval)
82
- continue
83
-
84
- with self.lock:
85
- upload_speed = (new_value.bytes_sent - old_value.bytes_sent) / interval
86
- download_speed = (new_value.bytes_recv - old_value.bytes_recv) / interval
87
-
88
- self.speed = {
89
- "upload": internet_manager.format_transfer_speed(upload_speed),
90
- "download": internet_manager.format_transfer_speed(download_speed)
91
- }
92
-
93
- def get_average_speed(self) -> float:
94
- """
95
- Calculate the average internet speed.
96
-
97
- Returns:
98
- float: The average internet speed in Mbps.
99
- """
100
- with self.lock:
101
- return self.speed['download'].split(" ")
102
-
103
- def calculate_total_size(self) -> str:
104
- """
105
- Calculate the total size of the files.
106
-
107
- Returns:
108
- str: The mean size of the files in a human-readable format.
109
- """
110
- try:
111
- if len(self.ts_file_sizes) == 0:
112
- raise ValueError("No file sizes available to calculate total size.")
113
-
114
- total_size = sum(self.ts_file_sizes)
115
- mean_size = total_size / len(self.ts_file_sizes)
116
-
117
- # Return formatted mean size
118
- return internet_manager.format_file_size(mean_size)
119
-
120
- except ZeroDivisionError as e:
121
- logging.error("Division by zero error occurred: %s", e)
122
- return "0B"
123
-
124
- except Exception as e:
125
- logging.error("An unexpected error occurred: %s", e)
126
- return "Error"
127
-
128
- def get_downloaded_size(self) -> str:
129
- """
130
- Get the total downloaded size formatted as a human-readable string.
131
-
132
- Returns:
133
- str: The total downloaded size as a human-readable string.
134
- """
135
- return internet_manager.format_file_size(self.now_downloaded_size)
136
-
137
- def update_progress_bar(self, total_downloaded: int, duration: float, progress_counter: tqdm) -> None:
138
- """
139
- Updates the progress bar with information about the TS segment download.
140
-
141
- Parameters:
142
- total_downloaded (int): The length of the content of the downloaded TS segment.
143
- duration (float): The duration of the segment download in seconds.
144
- progress_counter (tqdm): The tqdm object representing the progress bar.
145
- """
146
- # Add the size of the downloaded segment to the estimator
147
- self.add_ts_file(total_downloaded * self.total_segments, total_downloaded, duration)
148
-
149
- # Get downloaded size and total estimated size
150
- downloaded_file_size_str = self.get_downloaded_size()
151
- file_total_size = self.calculate_total_size()
152
-
153
- # Fix parameter for prefix
154
- number_file_downloaded = downloaded_file_size_str.split(' ')[0]
155
- number_file_total_size = file_total_size.split(' ')[0]
156
- units_file_downloaded = downloaded_file_size_str.split(' ')[1]
157
- units_file_total_size = file_total_size.split(' ')[1]
158
-
159
- average_internet_speed = self.get_average_speed()[0]
160
- average_internet_unit = self.get_average_speed()[1]
161
-
162
- # Update the progress bar's postfix
163
- if TQDM_USE_LARGE_BAR:
164
- progress_counter.set_postfix_str(
165
- f"{Colors.WHITE}[ {Colors.GREEN}{number_file_downloaded} {Colors.WHITE}< {Colors.GREEN}{number_file_total_size} {Colors.RED}{units_file_total_size} "
166
- f"{Colors.WHITE}| {Colors.CYAN}{average_internet_speed} {Colors.RED}{average_internet_unit}"
167
- )
168
- else:
169
- progress_counter.set_postfix_str(
170
- f"{Colors.WHITE}[ {Colors.GREEN}{number_file_downloaded}{Colors.RED} {units_file_downloaded} "
171
- f"{Colors.WHITE}| {Colors.CYAN}{average_internet_speed} {Colors.RED}{average_internet_unit}"
172
- )
1
+ # 20.02.24
2
+
3
+ import os
4
+ import time
5
+ import logging
6
+ import threading
7
+ from collections import deque
8
+
9
+
10
+ # External libraries
11
+ import psutil
12
+ from tqdm import tqdm
13
+
14
+
15
+ # Internal utilities
16
+ from StreamingCommunity.Src.Util.color import Colors
17
+ from StreamingCommunity.Src.Util.os import internet_manager
18
+ from StreamingCommunity.Src.Util._jsonConfig import config_manager
19
+
20
+
21
+ # Variable
22
+ TQDM_USE_LARGE_BAR = config_manager.get_int('M3U8_DOWNLOAD', 'tqdm_use_large_bar')
23
+
24
+
25
+ class M3U8_Ts_Estimator:
26
+ def __init__(self, total_segments: int):
27
+ """
28
+ Initialize the M3U8_Ts_Estimator object.
29
+
30
+ Parameters:
31
+ - total_segments (int): Length of total segments to download.
32
+ """
33
+ self.ts_file_sizes = []
34
+ self.now_downloaded_size = 0
35
+ self.total_segments = total_segments
36
+ self.lock = threading.Lock()
37
+ self.speed = {"upload": "N/A", "download": "N/A"} # Default to N/A
38
+ self.speed_thread = threading.Thread(target=self.capture_speed)
39
+ self.speed_thread.daemon = True
40
+ self.speed_thread.start()
41
+
42
+ def add_ts_file(self, size: int, size_download: int, duration: float):
43
+ """
44
+ Add a file size to the list of file sizes.
45
+
46
+ Parameters:
47
+ - size (int): The size of the ts file to be added.
48
+ - size_download (int): Single size of the ts file.
49
+ - duration (float): Time to download segment file.
50
+ """
51
+ if size <= 0 or size_download <= 0 or duration <= 0:
52
+ logging.error("Invalid input values: size=%d, size_download=%d, duration=%f", size, size_download, duration)
53
+ return
54
+
55
+ # Add total size bytes
56
+ self.ts_file_sizes.append(size)
57
+ self.now_downloaded_size += size_download
58
+
59
+ def capture_speed(self, interval: float = 1):
60
+ """
61
+ Capture the internet speed periodically and store the values.
62
+ """
63
+ def get_network_io():
64
+ """Get network I/O counters, handle missing psutil gracefully."""
65
+ try:
66
+ io_counters = psutil.net_io_counters()
67
+ return io_counters
68
+ except Exception as e:
69
+ logging.warning(f"Unable to access network I/O counters: {e}")
70
+ return None
71
+
72
+ while True:
73
+ old_value = get_network_io()
74
+ if old_value is None: # If psutil is not available, continue with default values
75
+ time.sleep(interval)
76
+ continue
77
+
78
+ time.sleep(interval)
79
+ new_value = get_network_io()
80
+ if new_value is None: # Handle again if psutil fails in the next call
81
+ time.sleep(interval)
82
+ continue
83
+
84
+ with self.lock:
85
+ upload_speed = (new_value.bytes_sent - old_value.bytes_sent) / interval
86
+ download_speed = (new_value.bytes_recv - old_value.bytes_recv) / interval
87
+
88
+ self.speed = {
89
+ "upload": internet_manager.format_transfer_speed(upload_speed),
90
+ "download": internet_manager.format_transfer_speed(download_speed)
91
+ }
92
+
93
+ def get_average_speed(self) -> float:
94
+ """
95
+ Calculate the average internet speed.
96
+
97
+ Returns:
98
+ float: The average internet speed in Mbps.
99
+ """
100
+ with self.lock:
101
+ return self.speed['download'].split(" ")
102
+
103
+ def calculate_total_size(self) -> str:
104
+ """
105
+ Calculate the total size of the files.
106
+
107
+ Returns:
108
+ str: The mean size of the files in a human-readable format.
109
+ """
110
+ try:
111
+ if len(self.ts_file_sizes) == 0:
112
+ raise ValueError("No file sizes available to calculate total size.")
113
+
114
+ total_size = sum(self.ts_file_sizes)
115
+ mean_size = total_size / len(self.ts_file_sizes)
116
+
117
+ # Return formatted mean size
118
+ return internet_manager.format_file_size(mean_size)
119
+
120
+ except ZeroDivisionError as e:
121
+ logging.error("Division by zero error occurred: %s", e)
122
+ return "0B"
123
+
124
+ except Exception as e:
125
+ logging.error("An unexpected error occurred: %s", e)
126
+ return "Error"
127
+
128
+ def get_downloaded_size(self) -> str:
129
+ """
130
+ Get the total downloaded size formatted as a human-readable string.
131
+
132
+ Returns:
133
+ str: The total downloaded size as a human-readable string.
134
+ """
135
+ return internet_manager.format_file_size(self.now_downloaded_size)
136
+
137
+ def update_progress_bar(self, total_downloaded: int, duration: float, progress_counter: tqdm) -> None:
138
+ """
139
+ Updates the progress bar with information about the TS segment download.
140
+
141
+ Parameters:
142
+ total_downloaded (int): The length of the content of the downloaded TS segment.
143
+ duration (float): The duration of the segment download in seconds.
144
+ progress_counter (tqdm): The tqdm object representing the progress bar.
145
+ """
146
+ # Add the size of the downloaded segment to the estimator
147
+ self.add_ts_file(total_downloaded * self.total_segments, total_downloaded, duration)
148
+
149
+ # Get downloaded size and total estimated size
150
+ downloaded_file_size_str = self.get_downloaded_size()
151
+ file_total_size = self.calculate_total_size()
152
+
153
+ # Fix parameter for prefix
154
+ number_file_downloaded = downloaded_file_size_str.split(' ')[0]
155
+ number_file_total_size = file_total_size.split(' ')[0]
156
+ units_file_downloaded = downloaded_file_size_str.split(' ')[1]
157
+ units_file_total_size = file_total_size.split(' ')[1]
158
+
159
+ average_internet_speed = self.get_average_speed()[0]
160
+ average_internet_unit = self.get_average_speed()[1]
161
+
162
+ # Update the progress bar's postfix
163
+ if TQDM_USE_LARGE_BAR:
164
+ progress_counter.set_postfix_str(
165
+ f"{Colors.WHITE}[ {Colors.GREEN}{number_file_downloaded} {Colors.WHITE}< {Colors.GREEN}{number_file_total_size} {Colors.RED}{units_file_total_size} "
166
+ f"{Colors.WHITE}| {Colors.CYAN}{average_internet_speed} {Colors.RED}{average_internet_unit}"
167
+ )
168
+ else:
169
+ progress_counter.set_postfix_str(
170
+ f"{Colors.WHITE}[ {Colors.GREEN}{number_file_downloaded}{Colors.RED} {units_file_downloaded} "
171
+ f"{Colors.WHITE}| {Colors.CYAN}{average_internet_speed} {Colors.RED}{average_internet_unit}"
172
+ )
173
173