StreamingCommunity 1.9.1__py3-none-any.whl → 1.9.4__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 (96) hide show
  1. StreamingCommunity/run.py +4 -5
  2. {StreamingCommunity-1.9.1.dist-info → StreamingCommunity-1.9.4.dist-info}/METADATA +37 -7
  3. StreamingCommunity-1.9.4.dist-info/RECORD +7 -0
  4. {StreamingCommunity-1.9.1.dist-info → StreamingCommunity-1.9.4.dist-info}/WHEEL +1 -1
  5. {StreamingCommunity-1.9.1.dist-info → StreamingCommunity-1.9.4.dist-info}/entry_points.txt +1 -0
  6. StreamingCommunity/Api/Player/Helper/Vixcloud/js_parser.py +0 -143
  7. StreamingCommunity/Api/Player/Helper/Vixcloud/util.py +0 -166
  8. StreamingCommunity/Api/Player/ddl.py +0 -89
  9. StreamingCommunity/Api/Player/maxstream.py +0 -151
  10. StreamingCommunity/Api/Player/supervideo.py +0 -194
  11. StreamingCommunity/Api/Player/vixcloud.py +0 -224
  12. StreamingCommunity/Api/Site/1337xx/__init__.py +0 -50
  13. StreamingCommunity/Api/Site/1337xx/costant.py +0 -15
  14. StreamingCommunity/Api/Site/1337xx/site.py +0 -84
  15. StreamingCommunity/Api/Site/1337xx/title.py +0 -66
  16. StreamingCommunity/Api/Site/altadefinizione/__init__.py +0 -50
  17. StreamingCommunity/Api/Site/altadefinizione/costant.py +0 -15
  18. StreamingCommunity/Api/Site/altadefinizione/film.py +0 -69
  19. StreamingCommunity/Api/Site/altadefinizione/site.py +0 -86
  20. StreamingCommunity/Api/Site/animeunity/__init__.py +0 -50
  21. StreamingCommunity/Api/Site/animeunity/costant.py +0 -15
  22. StreamingCommunity/Api/Site/animeunity/film_serie.py +0 -130
  23. StreamingCommunity/Api/Site/animeunity/site.py +0 -165
  24. StreamingCommunity/Api/Site/animeunity/util/ScrapeSerie.py +0 -97
  25. StreamingCommunity/Api/Site/bitsearch/__init__.py +0 -51
  26. StreamingCommunity/Api/Site/bitsearch/costant.py +0 -15
  27. StreamingCommunity/Api/Site/bitsearch/site.py +0 -84
  28. StreamingCommunity/Api/Site/bitsearch/title.py +0 -47
  29. StreamingCommunity/Api/Site/cb01new/__init__.py +0 -51
  30. StreamingCommunity/Api/Site/cb01new/costant.py +0 -15
  31. StreamingCommunity/Api/Site/cb01new/film.py +0 -69
  32. StreamingCommunity/Api/Site/cb01new/site.py +0 -74
  33. StreamingCommunity/Api/Site/ddlstreamitaly/__init__.py +0 -57
  34. StreamingCommunity/Api/Site/ddlstreamitaly/costant.py +0 -16
  35. StreamingCommunity/Api/Site/ddlstreamitaly/series.py +0 -141
  36. StreamingCommunity/Api/Site/ddlstreamitaly/site.py +0 -93
  37. StreamingCommunity/Api/Site/ddlstreamitaly/util/ScrapeSerie.py +0 -85
  38. StreamingCommunity/Api/Site/guardaserie/__init__.py +0 -52
  39. StreamingCommunity/Api/Site/guardaserie/costant.py +0 -15
  40. StreamingCommunity/Api/Site/guardaserie/series.py +0 -195
  41. StreamingCommunity/Api/Site/guardaserie/site.py +0 -84
  42. StreamingCommunity/Api/Site/guardaserie/util/ScrapeSerie.py +0 -110
  43. StreamingCommunity/Api/Site/mostraguarda/__init__.py +0 -48
  44. StreamingCommunity/Api/Site/mostraguarda/costant.py +0 -15
  45. StreamingCommunity/Api/Site/mostraguarda/film.py +0 -94
  46. StreamingCommunity/Api/Site/piratebays/__init__.py +0 -50
  47. StreamingCommunity/Api/Site/piratebays/costant.py +0 -15
  48. StreamingCommunity/Api/Site/piratebays/site.py +0 -89
  49. StreamingCommunity/Api/Site/piratebays/title.py +0 -45
  50. StreamingCommunity/Api/Site/streamingcommunity/__init__.py +0 -55
  51. StreamingCommunity/Api/Site/streamingcommunity/costant.py +0 -15
  52. StreamingCommunity/Api/Site/streamingcommunity/film.py +0 -70
  53. StreamingCommunity/Api/Site/streamingcommunity/series.py +0 -205
  54. StreamingCommunity/Api/Site/streamingcommunity/site.py +0 -126
  55. StreamingCommunity/Api/Site/streamingcommunity/util/ScrapeSerie.py +0 -113
  56. StreamingCommunity/Api/Template/Class/SearchType.py +0 -101
  57. StreamingCommunity/Api/Template/Util/__init__.py +0 -5
  58. StreamingCommunity/Api/Template/Util/get_domain.py +0 -137
  59. StreamingCommunity/Api/Template/Util/manage_ep.py +0 -153
  60. StreamingCommunity/Api/Template/Util/recall_search.py +0 -37
  61. StreamingCommunity/Api/Template/__init__.py +0 -3
  62. StreamingCommunity/Api/Template/site.py +0 -87
  63. StreamingCommunity/Lib/Downloader/HLS/downloader.py +0 -968
  64. StreamingCommunity/Lib/Downloader/HLS/proxyes.py +0 -110
  65. StreamingCommunity/Lib/Downloader/HLS/segments.py +0 -538
  66. StreamingCommunity/Lib/Downloader/MP4/downloader.py +0 -156
  67. StreamingCommunity/Lib/Downloader/TOR/downloader.py +0 -222
  68. StreamingCommunity/Lib/Downloader/__init__.py +0 -5
  69. StreamingCommunity/Lib/Driver/driver_1.py +0 -76
  70. StreamingCommunity/Lib/FFmpeg/__init__.py +0 -4
  71. StreamingCommunity/Lib/FFmpeg/capture.py +0 -170
  72. StreamingCommunity/Lib/FFmpeg/command.py +0 -292
  73. StreamingCommunity/Lib/FFmpeg/util.py +0 -242
  74. StreamingCommunity/Lib/M3U8/__init__.py +0 -6
  75. StreamingCommunity/Lib/M3U8/decryptor.py +0 -164
  76. StreamingCommunity/Lib/M3U8/estimator.py +0 -176
  77. StreamingCommunity/Lib/M3U8/parser.py +0 -666
  78. StreamingCommunity/Lib/M3U8/url_fixer.py +0 -52
  79. StreamingCommunity/Lib/TMBD/__init__.py +0 -2
  80. StreamingCommunity/Lib/TMBD/obj_tmbd.py +0 -39
  81. StreamingCommunity/Lib/TMBD/tmdb.py +0 -346
  82. StreamingCommunity/Upload/update.py +0 -68
  83. StreamingCommunity/Upload/version.py +0 -5
  84. StreamingCommunity/Util/_jsonConfig.py +0 -204
  85. StreamingCommunity/Util/call_stack.py +0 -42
  86. StreamingCommunity/Util/color.py +0 -20
  87. StreamingCommunity/Util/console.py +0 -12
  88. StreamingCommunity/Util/ffmpeg_installer.py +0 -275
  89. StreamingCommunity/Util/headers.py +0 -147
  90. StreamingCommunity/Util/logger.py +0 -53
  91. StreamingCommunity/Util/message.py +0 -46
  92. StreamingCommunity/Util/os.py +0 -514
  93. StreamingCommunity/Util/table.py +0 -163
  94. StreamingCommunity-1.9.1.dist-info/RECORD +0 -95
  95. {StreamingCommunity-1.9.1.dist-info → StreamingCommunity-1.9.4.dist-info}/LICENSE +0 -0
  96. {StreamingCommunity-1.9.1.dist-info → StreamingCommunity-1.9.4.dist-info}/top_level.txt +0 -0
@@ -1,968 +0,0 @@
1
- # 17.10.24
2
-
3
- import os
4
- import sys
5
- import logging
6
-
7
-
8
- # External libraries
9
- import httpx
10
-
11
-
12
- # Internal utilities
13
- from StreamingCommunity.Util._jsonConfig import config_manager
14
- from StreamingCommunity.Util.console import console, Panel, Table
15
- from StreamingCommunity.Util.color import Colors
16
- from StreamingCommunity.Util.os import (
17
- compute_sha1_hash,
18
- os_manager,
19
- internet_manager
20
- )
21
-
22
- # Logic class
23
- from ...FFmpeg import (
24
- print_duration_table,
25
- get_video_duration_s,
26
- join_video,
27
- join_audios,
28
- join_subtitle
29
- )
30
- from ...M3U8 import (
31
- M3U8_Parser,
32
- M3U8_Codec,
33
- M3U8_UrlFix
34
- )
35
- from .segments import M3U8_Segments
36
-
37
-
38
- # Config
39
- DOWNLOAD_SPECIFIC_AUDIO = config_manager.get_list('M3U8_DOWNLOAD', 'specific_list_audio')
40
- DOWNLOAD_SPECIFIC_SUBTITLE = config_manager.get_list('M3U8_DOWNLOAD', 'specific_list_subtitles')
41
- DOWNLOAD_VIDEO = config_manager.get_bool('M3U8_DOWNLOAD', 'download_video')
42
- DOWNLOAD_AUDIO = config_manager.get_bool('M3U8_DOWNLOAD', 'download_audio')
43
- MERGE_AUDIO = config_manager.get_bool('M3U8_DOWNLOAD', 'merge_audio')
44
- DOWNLOAD_SUBTITLE = config_manager.get_bool('M3U8_DOWNLOAD', 'download_sub')
45
- MERGE_SUBTITLE = config_manager.get_bool('M3U8_DOWNLOAD', 'merge_subs')
46
- REMOVE_SEGMENTS_FOLDER = config_manager.get_bool('M3U8_DOWNLOAD', 'cleanup_tmp_folder')
47
- FILTER_CUSTOM_REOLUTION = config_manager.get_int('M3U8_PARSER', 'force_resolution')
48
- GET_ONLY_LINK = config_manager.get_bool('M3U8_PARSER', 'get_only_link')
49
-
50
-
51
- # Variable
52
- max_timeout = config_manager.get_int("REQUESTS", "timeout")
53
- headers_index = config_manager.get_dict('REQUESTS', 'user-agent')
54
- m3u8_url_fixer = M3U8_UrlFix()
55
-
56
-
57
-
58
- class PathManager:
59
- def __init__(self, output_filename):
60
- """
61
- Initializes the PathManager with the output filename.
62
-
63
- Args:
64
- output_filename (str): The name of the output file (should end with .mp4).
65
- """
66
- self.output_filename = output_filename
67
-
68
- # Create the base path by removing the '.mp4' extension from the output filename
69
- self.base_path = str(output_filename).replace(".mp4", "")
70
- logging.info(f"class 'PathManager'; set base path: {self.base_path}")
71
-
72
- # Define the path for a temporary directory where segments will be stored
73
- self.base_temp = os.path.join(self.base_path, "tmp")
74
- self.video_segments_path = os.path.join(self.base_temp, "video")
75
- self.audio_segments_path = os.path.join(self.base_temp, "audio")
76
- self.subtitle_segments_path = os.path.join(self.base_temp, "subtitle")
77
-
78
- def create_directories(self):
79
- """
80
- Creates the necessary directories for storing video, audio, and subtitle segments.
81
- """
82
-
83
- os.makedirs(self.base_temp, exist_ok=True)
84
- os.makedirs(self.video_segments_path, exist_ok=True)
85
- os.makedirs(self.audio_segments_path, exist_ok=True)
86
- os.makedirs(self.subtitle_segments_path, exist_ok=True)
87
-
88
-
89
- class HttpClient:
90
- def __init__(self, headers: str = None):
91
- """
92
- Initializes the HttpClient with specified headers.
93
- """
94
- self.headers = headers
95
-
96
- def get(self, url: str):
97
- """
98
- Sends a GET request to the specified URL and returns the response as text.
99
-
100
- Returns:
101
- str: The response body as text if the request is successful, None otherwise.
102
- """
103
- logging.info(f"class 'HttpClient'; make request: {url}")
104
- try:
105
- response = httpx.get(
106
- url=url,
107
- headers=self.headers,
108
- timeout=max_timeout
109
- )
110
-
111
- response.raise_for_status()
112
- return response.text
113
-
114
- except Exception as e:
115
- logging.info(f"Request to {url} failed with error: {e}")
116
- return 404
117
-
118
- def get_content(self, url):
119
- """
120
- Sends a GET request to the specified URL and returns the raw response content.
121
-
122
- Returns:
123
- bytes: The response content as bytes if the request is successful, None otherwise.
124
- """
125
- logging.info(f"class 'HttpClient'; make request: {url}")
126
- try:
127
- response = httpx.get(
128
- url=url,
129
- headers=self.headers,
130
- timeout=max_timeout
131
- )
132
-
133
- response.raise_for_status()
134
- return response.content # Return the raw response content
135
-
136
- except Exception as e:
137
- logging.error(f"Request to {url} failed: {response.status_code} when get content.")
138
- return None
139
-
140
-
141
- class ContentExtractor:
142
- def __init__(self):
143
- """
144
- This class is responsible for extracting audio, subtitle, and video information from an M3U8 playlist.
145
- """
146
- pass
147
-
148
- def start(self, obj_parse: M3U8_Parser):
149
- """
150
- Starts the extraction process by parsing the M3U8 playlist and collecting audio, subtitle, and video data.
151
-
152
- Args:
153
- obj_parse (str): The M3U8_Parser obj of the M3U8 playlist.
154
- """
155
-
156
- self.obj_parse = obj_parse
157
-
158
- # Collect audio, subtitle, and video information
159
- self._collect_audio()
160
- self._collect_subtitle()
161
- self._collect_video()
162
-
163
- def _collect_audio(self):
164
- """
165
- It checks for available audio languages and the specific audio tracks to download.
166
- """
167
- logging.info(f"class 'ContentExtractor'; call _collect_audio()")
168
-
169
- # Collect available audio tracks and their corresponding URIs and names
170
- self.list_available_audio = self.obj_parse._audio.get_all_uris_and_names()
171
-
172
- # Check if there are any audio tracks available; if not, disable download
173
- if self.list_available_audio is not None:
174
-
175
- # Extract available languages from the audio tracks
176
- available_languages = [obj_audio.get('language') for obj_audio in self.list_available_audio]
177
- set_language = DOWNLOAD_SPECIFIC_AUDIO
178
- result = list(set(available_languages) & set(set_language))
179
-
180
- # Create a formatted table to display audio info
181
- if len(available_languages) > 0:
182
- table = Table(show_header=False, box=None)
183
- table.add_row(f"[cyan]Available languages:", f"[purple]{', '.join(available_languages)}")
184
- table.add_row(f"[red]Set audios:", f"[purple]{', '.join(set_language)}")
185
- table.add_row(f"[green]Downloadable:", f"[purple]{', '.join(result)}")
186
-
187
- console.rule("[bold green] AUDIO ", style="bold red")
188
- console.print(table)
189
-
190
- else:
191
- console.log("[red]Can't find a list of audios")
192
-
193
- def _collect_subtitle(self):
194
- """
195
- It checks for available subtitle languages and the specific subtitles to download.
196
- """
197
- logging.info(f"class 'ContentExtractor'; call _collect_subtitle()")
198
-
199
- # Collect available subtitles and their corresponding URIs and names
200
- self.list_available_subtitles = self.obj_parse._subtitle.get_all_uris_and_names()
201
-
202
- # Check if there are any subtitles available; if not, disable download
203
- if self.list_available_subtitles is not None:
204
-
205
- # Extract available languages from the subtitles
206
- available_languages = [obj_subtitle.get('language') for obj_subtitle in self.list_available_subtitles]
207
- set_language = DOWNLOAD_SPECIFIC_SUBTITLE
208
- result = list(set(available_languages) & set(set_language))
209
-
210
- # Create a formatted table to display subtitle info
211
- if len(available_languages) > 0:
212
- table = Table(show_header=False, box=None)
213
- table.add_row(f"[cyan]Available languages:", f"[purple]{', '.join(available_languages)}")
214
- table.add_row(f"[red]Set subtitles:", f"[purple]{', '.join(set_language)}")
215
- table.add_row(f"[green]Downloadable:", f"[purple]{', '.join(result)}")
216
-
217
- console.rule("[bold green] SUBTITLE ", style="bold red")
218
- console.print(table)
219
-
220
- else:
221
- console.log("[red]Can't find a list of subtitles")
222
-
223
- def _collect_video(self):
224
- """
225
- It identifies the best video quality and displays relevant information to the user.
226
- """
227
- logging.info(f"class 'ContentExtractor'; call _collect_video()")
228
-
229
- # Collect custom quality video if a specific resolution is set
230
- if FILTER_CUSTOM_REOLUTION != -1:
231
- self.m3u8_index, video_res = self.obj_parse._video.get_custom_uri(y_resolution=FILTER_CUSTOM_REOLUTION)
232
-
233
- # Otherwise, get the best available video quality
234
- self.m3u8_index, video_res = self.obj_parse._video.get_best_uri()
235
- self.codec: M3U8_Codec = self.obj_parse.codec
236
-
237
- # List all available resolutions
238
- tuple_available_resolution = self.obj_parse._video.get_list_resolution()
239
- list_available_resolution = [str(resolution[0]) + "x" + str(resolution[1]) for resolution in tuple_available_resolution]
240
- logging.info(f"M3U8 index selected: {self.m3u8_index}, with resolution: {video_res}")
241
-
242
- # Create a formatted table to display video info
243
- table = Table(show_header=False, box=None)
244
- table.add_row(f"[cyan]Available resolutions:", f"[purple]{', '.join(list_available_resolution)}")
245
- table.add_row(f"[green]Downloadable:", f"[purple]{video_res[0]}x{video_res[1]}")
246
-
247
- if self.codec is not None:
248
- if config_manager.get_bool("M3U8_CONVERSION", "use_codec"):
249
- table.add_row(f"[green]Codec:", f"([green]'v'[white]: [yellow]{self.codec.video_codec_name}[white] ([green]b[white]: [yellow]{self.codec.video_bitrate // 1000}k[white]), [green]'a'[white]: [yellow]{self.codec.audio_codec_name}[white] ([green]b[white]: [yellow]{self.codec.audio_bitrate // 1000}k[white]))")
250
- else:
251
- table.add_row(f"[green]Codec:", "[purple]copy")
252
-
253
- console.rule("[bold green] VIDEO ", style="bold red")
254
- console.print(table)
255
- print("")
256
-
257
- # Fix the URL if it does not include the full protocol
258
- if "http" not in self.m3u8_index:
259
-
260
- # Generate the full URL
261
- self.m3u8_index = m3u8_url_fixer.generate_full_url(self.m3u8_index)
262
- logging.info(f"Generated index URL: {self.m3u8_index}")
263
-
264
- # Check if a valid HTTPS URL is obtained
265
- if self.m3u8_index is not None and "https" in self.m3u8_index:
266
- #console.print(f"[cyan]Found m3u8 index [white]=> [red]{self.m3u8_index}")
267
- print()
268
-
269
- else:
270
- logging.error("[download_m3u8] Can't find a valid m3u8 index")
271
- raise ValueError("Invalid m3u8 index URL")
272
-
273
-
274
- class DownloadTracker:
275
- def __init__(self, path_manager: PathManager):
276
- """
277
- Initializes the DownloadTracker with paths for audio, subtitle, and video segments.
278
-
279
- Args:
280
- path_manager (PathManager): An instance of the PathManager class to manage file paths.
281
- """
282
-
283
- # Initialize lists to track downloaded audio, subtitles, and video
284
- self.downloaded_audio = []
285
- self.downloaded_subtitle = []
286
- self.downloaded_video = []
287
-
288
- self.video_segment_path = path_manager.video_segments_path
289
- self.audio_segments_path = path_manager.audio_segments_path
290
- self.subtitle_segments_path = path_manager.subtitle_segments_path
291
-
292
- def add_video(self, available_video):
293
- """
294
- Adds a single video to the list of downloaded videos.
295
-
296
- Args:
297
- available_video (str): The URL of the video to be downloaded.
298
- """
299
- logging.info(f"class 'DownloadTracker'; call add_video() with parameter: {available_video}")
300
-
301
- self.downloaded_video.append({
302
- 'type': 'video',
303
- 'url': available_video,
304
- 'path': os.path.join(self.video_segment_path, "0.ts")
305
- })
306
-
307
- def add_audio(self, list_available_audio):
308
- """
309
- Adds available audio tracks to the list of downloaded audio.
310
-
311
- Args:
312
- list_available_audio (list): A list of available audio track objects.
313
- """
314
- logging.info(f"class 'DownloadTracker'; call add_audio() with parameter: {list_available_audio}")
315
-
316
- for obj_audio in list_available_audio:
317
-
318
- # Check if specific audio languages are set for download
319
- if len(DOWNLOAD_SPECIFIC_AUDIO) > 0:
320
-
321
- # Skip this audio track if its language is not in the specified list
322
- if obj_audio.get('language') not in DOWNLOAD_SPECIFIC_AUDIO:
323
- continue
324
-
325
- # Construct the full path for the audio segment directory
326
- full_path_audio = os.path.join(self.audio_segments_path, obj_audio.get('language'))
327
-
328
- # Append the audio information to the downloaded audio list
329
- self.downloaded_audio.append({
330
- 'type': 'audio',
331
- 'url': obj_audio.get('uri'),
332
- 'language': obj_audio.get('language'),
333
- 'path': os.path.join(full_path_audio, "0.ts")
334
- })
335
-
336
- def add_subtitle(self, list_available_subtitles):
337
- """
338
- Adds available subtitles to the list of downloaded subtitles.
339
-
340
- Args:
341
- list_available_subtitles (list): A list of available subtitle objects.
342
- """
343
- logging.info(f"class 'DownloadTracker'; call add_subtitle() with parameter: {list_available_subtitles}")
344
-
345
- for obj_subtitle in list_available_subtitles:
346
-
347
- # Check if specific subtitle languages are set for download
348
- if len(DOWNLOAD_SPECIFIC_SUBTITLE) > 0:
349
-
350
- # Skip this subtitle if its language is not in the specified list
351
- if obj_subtitle.get('language') not in DOWNLOAD_SPECIFIC_SUBTITLE:
352
- continue
353
-
354
- sub_language = obj_subtitle.get('language')
355
-
356
- # Construct the full path for the subtitle file
357
- sub_full_path = os.path.join(self.subtitle_segments_path, sub_language + ".vtt")
358
-
359
- self.downloaded_subtitle.append({
360
- 'type': 'sub',
361
- 'url': obj_subtitle.get('uri'),
362
- 'language': obj_subtitle.get('language'),
363
- 'path': sub_full_path
364
- })
365
-
366
-
367
- class ContentDownloader:
368
- def __init__(self):
369
- """
370
- Initializes the ContentDownloader class.
371
-
372
- Attributes:
373
- expected_real_time (float): Expected real-time duration of the video download.
374
- """
375
- self.expected_real_time = None
376
-
377
- def download_video(self, downloaded_video):
378
- """
379
- Downloads the video if it doesn't already exist.
380
-
381
- Args:
382
- downloaded_video (list): A list containing information about the video to download.
383
- """
384
- logging.info(f"class 'ContentDownloader'; call download_video() with parameter: {downloaded_video}")
385
-
386
- # Check if the video file already exists
387
- if not os.path.exists(downloaded_video[0].get('path')):
388
- folder_name = os.path.dirname(downloaded_video[0].get('path'))
389
-
390
- # Create an instance of M3U8_Segments to handle video segments download
391
- video_m3u8 = M3U8_Segments(downloaded_video[0].get('url'), folder_name)
392
-
393
- # Get information about the video segments (e.g., duration, ts files to download)
394
- video_m3u8.get_info()
395
-
396
- # Store the expected real-time duration of the video
397
- self.expected_real_time = video_m3u8.expected_real_time
398
-
399
- # Download the video streams and print status
400
- video_m3u8.download_streams(f"{Colors.MAGENTA}video")
401
-
402
- # Print duration information of the downloaded video
403
- #print_duration_table(downloaded_video[0].get('path'))
404
-
405
- else:
406
- console.log("[cyan]Video [red]already exists.")
407
-
408
- def download_audio(self, downloaded_audio):
409
- """
410
- Downloads audio tracks if they don't already exist.
411
-
412
- Args:
413
- downloaded_audio (list): A list containing information about audio tracks to download.
414
- """
415
- logging.info(f"class 'ContentDownloader'; call download_audio() with parameter: {downloaded_audio}")
416
-
417
- for obj_audio in downloaded_audio:
418
- folder_name = os.path.dirname(obj_audio.get('path'))
419
-
420
- # Check if the audio file already exists
421
- if not os.path.exists(obj_audio.get('path')):
422
-
423
- # Create an instance of M3U8_Segments to handle audio segments download
424
- audio_m3u8 = M3U8_Segments(obj_audio.get('url'), folder_name)
425
-
426
- # Get information about the audio segments (e.g., duration, ts files to download)
427
- audio_m3u8.get_info()
428
-
429
- # Download the audio segments and print status
430
- audio_m3u8.download_streams(f"{Colors.MAGENTA}audio {Colors.RED}{obj_audio.get('language')}")
431
-
432
- # Print duration information of the downloaded audio
433
- #print_duration_table(obj_audio.get('path'))
434
-
435
- else:
436
- console.log(f"[cyan]Audio [white]([green]{obj_audio.get('language')}[white]) [red]already exists.")
437
-
438
- def download_subtitle(self, downloaded_subtitle):
439
- """
440
- Downloads subtitle files if they don't already exist.
441
-
442
- Args:
443
- downloaded_subtitle (list): A list containing information about subtitles to download.
444
- """
445
- logging.info(f"class 'ContentDownloader'; call download_subtitle() with parameter: {downloaded_subtitle}")
446
-
447
- for obj_subtitle in downloaded_subtitle:
448
- sub_language = obj_subtitle.get('language')
449
-
450
- # Check if the subtitle file already exists
451
- if os.path.exists(obj_subtitle.get("path")):
452
- console.log(f"[cyan]Subtitle [white]([green]{sub_language}[white]) [red]already exists.")
453
- continue # Skip to the next subtitle if it exists
454
-
455
- # Parse the M3U8 file to get the subtitle URI
456
- m3u8_sub_parser = M3U8_Parser()
457
- m3u8_sub_parser.parse_data(
458
- uri=obj_subtitle.get('uri'),
459
- raw_content=httpx.get(obj_subtitle.get('url')).text # Fetch subtitle content
460
- )
461
-
462
- # Print the status of the subtitle download
463
- #console.print(f"[cyan]Downloading subtitle: [red]{sub_language.lower()}")
464
-
465
- # Write the content to the specified file
466
- with open(obj_subtitle.get("path"), "wb") as f:
467
- f.write(HttpClient().get_content(m3u8_sub_parser.subtitle[-1]))
468
-
469
-
470
- class ContentJoiner:
471
- def __init__(self, path_manager):
472
- """
473
- Initializes the ContentJoiner class.
474
-
475
- Args:
476
- path_manager (PathManager): An instance of PathManager to manage output paths.
477
- """
478
- self.path_manager: PathManager = path_manager
479
-
480
- def setup(self, downloaded_video, downloaded_audio, downloaded_subtitle, codec = None):
481
- """
482
- Sets up the content joiner with downloaded media files.
483
-
484
- Args:
485
- downloaded_video (list): List of downloaded video information.
486
- downloaded_audio (list): List of downloaded audio information.
487
- downloaded_subtitle (list): List of downloaded subtitle information.
488
- """
489
- self.downloaded_video = downloaded_video
490
- self.downloaded_audio = downloaded_audio
491
- self.downloaded_subtitle = downloaded_subtitle
492
- self.codec = codec
493
-
494
- # Initialize flags to check if media is available
495
- self.converted_out_path = None
496
- self.there_is_video = len(downloaded_video) > 0
497
- self.there_is_audio = len(downloaded_audio) > 0
498
- self.there_is_subtitle = len(downloaded_subtitle) > 0
499
-
500
- if self.there_is_audio or self.there_is_subtitle:
501
-
502
- # Display the status of available media
503
- table = Table(show_header=False, box=None)
504
-
505
- table.add_row(f"[green]Video - audio", f"[yellow]{self.there_is_audio}")
506
- table.add_row(f"[green]Video - Subtitle", f"[yellow]{self.there_is_subtitle}")
507
-
508
- print("")
509
- console.rule("[bold green] JOIN ", style="bold red")
510
- console.print(table)
511
- print("")
512
-
513
- # Start the joining process
514
- self.conversione()
515
-
516
- def conversione(self):
517
- """
518
- Handles the joining of video, audio, and subtitles based on availability.
519
- """
520
-
521
- # Join audio and video if audio is available
522
- if self.there_is_audio:
523
- if MERGE_AUDIO:
524
-
525
- # Join video with audio tracks
526
- self.converted_out_path = self._join_video_audio()
527
-
528
- else:
529
-
530
- # Process each available audio track
531
- for obj_audio in self.downloaded_audio:
532
- language = obj_audio.get('language')
533
- path = obj_audio.get('path')
534
-
535
- # Set the new path for regular audio
536
- new_path = self.path_manager.output_filename.replace(".mp4", f"_{language}.mp4")
537
-
538
- try:
539
-
540
- # Rename the audio file to the new path
541
- os.rename(path, new_path)
542
- logging.info(f"Audio moved to {new_path}")
543
-
544
- except Exception as e:
545
- logging.error(f"Failed to move audio {path} to {new_path}: {e}")
546
-
547
- # Convert video if available
548
- if self.there_is_video:
549
- self.converted_out_path = self._join_video()
550
-
551
- # If no audio but video is available, join video
552
- else:
553
- if self.there_is_video:
554
- self.converted_out_path = self._join_video()
555
-
556
- # Join subtitles if available
557
- if self.there_is_subtitle:
558
- if MERGE_SUBTITLE:
559
- if self.converted_out_path is not None:
560
- self.converted_out_path = self._join_video_subtitles(self.converted_out_path)
561
-
562
- else:
563
-
564
- # Process each available subtitle track
565
- for obj_sub in self.downloaded_subtitle:
566
- language = obj_sub.get('language')
567
- path = obj_sub.get('path')
568
- forced = 'forced' in language
569
-
570
- # Adjust the language name and set the new path based on forced status
571
- if forced:
572
- language = language.replace("forced-", "")
573
- new_path = self.path_manager.output_filename.replace(".mp4", f".{language}.forced.vtt")
574
- else:
575
- new_path = self.path_manager.output_filename.replace(".mp4", f".{language}.vtt")
576
-
577
- try:
578
- # Rename the subtitle file to the new path
579
- os.rename(path, new_path)
580
- logging.info(f"Subtitle moved to {new_path}")
581
-
582
- except Exception as e:
583
- logging.error(f"Failed to move subtitle {path} to {new_path}: {e}")
584
-
585
- def _join_video(self):
586
- """
587
- Joins video segments into a single video file.
588
-
589
- Returns:
590
- str: The path to the joined video file.
591
- """
592
- path_join_video = os.path.join(self.path_manager.base_path, "v_v.mp4")
593
- logging.info(f"JOIN video path: {path_join_video}")
594
-
595
- # Check if the joined video file already exists
596
- if not os.path.exists(path_join_video):
597
-
598
- # Set codec to None if not defined in class
599
- #if not hasattr(self, 'codec'):
600
- # self.codec = None
601
-
602
- # Join the video segments into a single video file
603
- join_video(
604
- video_path=self.downloaded_video[0].get('path'),
605
- out_path=path_join_video,
606
- codec=self.codec
607
- )
608
-
609
- else:
610
- console.log("[red]Output join video already exists.")
611
-
612
- return path_join_video
613
-
614
- def _join_video_audio(self):
615
- """
616
- Joins video segments with audio tracks into a single video with audio file.
617
-
618
- Returns:
619
- str: The path to the joined video with audio file.
620
- """
621
- path_join_video_audio = os.path.join(self.path_manager.base_path, "v_a.mp4")
622
- logging.info(f"JOIN audio path: {path_join_video_audio}")
623
-
624
- # Check if the joined video with audio file already exists
625
- if not os.path.exists(path_join_video_audio):
626
-
627
- # Set codec to None if not defined in class
628
- #if not hasattr(self, 'codec'):
629
- # self.codec = None
630
-
631
- # Join the video with audio segments
632
- join_audios(
633
- video_path=self.downloaded_video[0].get('path'),
634
- audio_tracks=self.downloaded_audio,
635
- out_path=path_join_video_audio,
636
- codec=self.codec
637
- )
638
-
639
- else:
640
- console.log("[red]Output join video and audio already exists.")
641
-
642
- return path_join_video_audio
643
-
644
- def _join_video_subtitles(self, input_path):
645
- """
646
- Joins subtitles with the video.
647
-
648
- Args:
649
- input_path (str): The path to the video file to which subtitles will be added.
650
-
651
- Returns:
652
- str: The path to the video with subtitles file.
653
- """
654
- path_join_video_subtitle = os.path.join(self.path_manager.base_path, "v_s.mp4")
655
- logging.info(f"JOIN subtitle path: {path_join_video_subtitle}")
656
-
657
- # Check if the video with subtitles file already exists
658
- if not os.path.exists(path_join_video_subtitle):
659
-
660
- # Join the video with subtitles
661
- join_subtitle(
662
- input_path,
663
- self.downloaded_subtitle,
664
- path_join_video_subtitle
665
- )
666
-
667
- return path_join_video_subtitle
668
-
669
-
670
- class HLS_Downloader:
671
- def __init__(self, output_filename: str=None, m3u8_playlist: str=None, m3u8_index: str=None, is_playlist_url: bool=True, is_index_url: bool=True):
672
- """
673
- Initializes the HLS_Downloader class.
674
-
675
- Args:
676
- output_filename (str): The desired output filename for the downloaded content.
677
- m3u8_playlist (str): The URL or content of the m3u8 playlist.
678
- m3u8_index (str): The index URL for m3u8 streams.
679
- is_playlist_url (bool): Flag indicating if the m3u8_playlist is a URL.
680
- is_index_url (bool): Flag indicating if the m3u8_index is a URL.
681
- """
682
- if ((m3u8_playlist == None or m3u8_playlist == "") and output_filename is None) or ((m3u8_index == None or m3u8_index == "") and output_filename is None):
683
- logging.info(f"class 'HLS_Downloader'; call __init__(); no parameter")
684
- sys.exit(0)
685
-
686
- self.output_filename = self._generate_output_filename(output_filename, m3u8_playlist, m3u8_index)
687
- self.path_manager = PathManager(self.output_filename)
688
- self.download_tracker = DownloadTracker(self.path_manager)
689
- self.content_extractor = ContentExtractor()
690
- self.content_downloader = ContentDownloader()
691
- self.content_joiner = ContentJoiner(self.path_manager)
692
-
693
- self.m3u8_playlist = m3u8_playlist
694
- self.m3u8_index = m3u8_index
695
- self.is_playlist_url = is_playlist_url
696
- self.is_index_url = is_index_url
697
- self.expected_real_time = None
698
- self.instace_parserClass = M3U8_Parser()
699
-
700
- self.request_m3u8_playlist = None
701
- self.request_m3u8_index = None
702
- if (m3u8_playlist == None or m3u8_playlist == ""):
703
- self.request_m3u8_index = HttpClient().get(self.m3u8_index)
704
- if (m3u8_index == None or m3u8_index == ""):
705
- self.request_m3u8_playlist = HttpClient().get(self.m3u8_playlist)
706
-
707
- def _generate_output_filename(self, output_filename, m3u8_playlist, m3u8_index):
708
- """
709
- Generates a valid output filename based on provided parameters.
710
-
711
- Args:
712
- output_filename (str): The desired output filename.
713
- m3u8_playlist (str): The m3u8 playlist URL or content.
714
- m3u8_index (str): The m3u8 index URL.
715
-
716
- Returns:
717
- str: The generated output filename.
718
- """
719
- root_path = config_manager.get('DEFAULT', 'root_path')
720
- new_filename = None
721
- new_folder = os.path.join(root_path, "undefined")
722
- logging.info(f"class 'HLS_Downloader'; call _generate_output_filename(); destination folder: {new_folder}")
723
-
724
- # Auto-generate output file name if not present
725
- if (output_filename is None) or ("mp4" not in output_filename):
726
- if m3u8_playlist is not None:
727
- new_filename = os.path.join(new_folder, compute_sha1_hash(m3u8_playlist) + ".mp4")
728
- else:
729
- new_filename = os.path.join(new_folder, compute_sha1_hash(m3u8_index) + ".mp4")
730
-
731
- else:
732
-
733
- # Check if output_filename contains a folder path
734
- folder, base_name = os.path.split(output_filename)
735
-
736
- # If no folder is specified, default to 'undefined'
737
- if not folder:
738
- folder = new_folder
739
-
740
- # Sanitize base name and folder
741
- folder = os_manager.get_sanitize_path(folder)
742
- base_name = os_manager.get_sanitize_file(base_name)
743
- os_manager.create_path(folder)
744
-
745
- # Parse to only ASCII for compatibility across platforms
746
- new_filename = os.path.join(folder, base_name)
747
-
748
- logging.info(f"class 'HLS_Downloader'; call _generate_output_filename(); return path: {new_filename}")
749
- return new_filename
750
-
751
- def start(self):
752
- """
753
- Initiates the downloading process. Checks if the output file already exists and proceeds with processing the playlist or index.
754
- """
755
- if os.path.exists(self.output_filename):
756
- console.log("[red]Output file already exists.")
757
- return 400
758
-
759
- self.path_manager.create_directories()
760
-
761
- # Determine whether to process a playlist or index
762
- if self.m3u8_playlist:
763
- if self.m3u8_playlist is not None:
764
- if self.request_m3u8_playlist != 404:
765
- logging.info(f"class 'HLS_Downloader'; call start(); parse m3u8 data")
766
-
767
- self.instace_parserClass.parse_data(uri=self.m3u8_playlist, raw_content=self.request_m3u8_playlist)
768
- is_masterPlaylist = self.instace_parserClass.is_master_playlist
769
-
770
- # Check if it's a real master playlist
771
- if is_masterPlaylist:
772
- if not GET_ONLY_LINK:
773
- r_proc = self._process_playlist()
774
-
775
- if r_proc == 404:
776
- return 404
777
- else:
778
- return None
779
-
780
- else:
781
- return {
782
- 'path': self.output_filename,
783
- 'url': self.m3u8_playlist
784
- }
785
-
786
- else:
787
- console.log("[red]Error: URL passed to M3U8_Parser is an index playlist; expected a master playlist. Crucimorfo strikes again!")
788
- else:
789
- console.log(f"[red]Error: m3u8_playlist failed request for: {self.m3u8_playlist}")
790
- else:
791
- console.log("[red]Error: m3u8_playlist is None")
792
-
793
- elif self.m3u8_index:
794
- if self.m3u8_index is not None:
795
- if self.request_m3u8_index != 404:
796
- logging.info(f"class 'HLS_Downloader'; call start(); parse m3u8 data")
797
-
798
- self.instace_parserClass.parse_data(uri=self.m3u8_index, raw_content=self.request_m3u8_index)
799
- is_masterPlaylist = self.instace_parserClass.is_master_playlist
800
-
801
- # Check if it's a real index playlist
802
- if not is_masterPlaylist:
803
- if not GET_ONLY_LINK:
804
- self._process_index()
805
- return None
806
-
807
- else:
808
- return {
809
- 'path': self.output_filename,
810
- 'url': self.m3u8_index
811
- }
812
-
813
- else:
814
- console.log("[red]Error: URL passed to M3U8_Parser is an master playlist; expected a index playlist. Crucimorfo strikes again!")
815
- else:
816
- console.log("[red]Error: m3u8_index failed request")
817
- else:
818
- console.log("[red]Error: m3u8_index is None")
819
-
820
- def _clean(self, out_path: str) -> None:
821
- """
822
- Cleans up temporary files and folders after downloading and processing.
823
-
824
- Args:
825
- out_path (str): The path of the output file to be cleaned up.
826
- """
827
- def dict_to_seconds(d):
828
- """Converts a dictionary of time components to total seconds."""
829
- if d is not None:
830
- return d['h'] * 3600 + d['m'] * 60 + d['s']
831
- return 0
832
-
833
- # Check if the final output file exists
834
- logging.info(f"Check if end file converted exists: {out_path}")
835
- if out_path is None or not os.path.isfile(out_path):
836
- logging.error("Video file converted does not exist.")
837
- sys.exit(0)
838
-
839
- # Rename the output file to the desired output filename if it does not already exist
840
- if not os.path.exists(self.output_filename):
841
-
842
- # Rename the converted file to the specified output filename
843
- os.rename(out_path, self.output_filename)
844
-
845
- # Get duration information for the output file
846
- end_output_time = print_duration_table(self.output_filename, description=False, return_string=False)
847
-
848
- # Calculate file size and duration for reporting
849
- formatted_size = internet_manager.format_file_size(os.path.getsize(self.output_filename))
850
- formatted_duration = print_duration_table(self.output_filename, description=False, return_string=True)
851
-
852
- expected_real_seconds = dict_to_seconds(self.content_downloader.expected_real_time)
853
- end_output_seconds = dict_to_seconds(end_output_time)
854
-
855
- # Check if the downloaded content is complete based on expected duration
856
- if expected_real_seconds is not None:
857
- missing_ts = not (expected_real_seconds - 3 <= end_output_seconds <= expected_real_seconds + 3)
858
- else:
859
- missing_ts = "Undefined"
860
-
861
- # Second check for missing segments
862
- if not missing_ts:
863
- if get_video_duration_s(self.output_filename) < int(expected_real_seconds) - 5:
864
- missing_ts = True
865
-
866
- # Prepare the report panel content
867
- print("")
868
- panel_content = (
869
- f"[bold green]Download completed![/bold green]\n"
870
- f"[cyan]File size: [bold red]{formatted_size}[/bold red]\n"
871
- f"[cyan]Duration: [bold]{formatted_duration}[/bold]\n"
872
- f"[cyan]Missing TS: [bold red]{missing_ts}[/bold red]"
873
- )
874
-
875
- # Display the download completion message
876
- console.print(Panel(
877
- panel_content,
878
- title=f"{os.path.basename(self.output_filename.replace('.mp4', ''))}",
879
- border_style="green"
880
- ))
881
-
882
- # Handle missing segments
883
- if missing_ts:
884
- os.rename(self.output_filename, self.output_filename.replace(".mp4", "_failed.mp4"))
885
-
886
- # Delete all temporary files except for the output file
887
- os_manager.remove_files_except_one(self.path_manager.base_path, os.path.basename(self.output_filename.replace(".mp4", "_failed.mp4")))
888
-
889
- # Remove the base folder if specified
890
- if REMOVE_SEGMENTS_FOLDER:
891
- os_manager.remove_folder(self.path_manager.base_path)
892
-
893
- else:
894
- logging.info("Video file converted already exists.")
895
-
896
- def _valida_playlist(self):
897
- """
898
- Validates the m3u8 playlist content, saves it to a temporary file, and collects playlist information.
899
- """
900
- logging.info("class 'HLS_Downloader'; call _valida_playlist()")
901
-
902
- # Retrieve the m3u8 playlist content
903
- if self.is_playlist_url:
904
- if self.request_m3u8_playlist != 404:
905
- m3u8_playlist_text = self.request_m3u8_playlist
906
- m3u8_url_fixer.set_playlist(self.m3u8_playlist)
907
-
908
- else:
909
- logging.info(f"class 'HLS_Downloader'; call _process_playlist(); return 404")
910
- return 404
911
-
912
- else:
913
- m3u8_playlist_text = self.m3u8_playlist
914
-
915
- # Check if the m3u8 content is valid
916
- if m3u8_playlist_text is None:
917
- console.log("[red]Playlist m3u8 to download is empty.")
918
- sys.exit(0)
919
-
920
- # Save the m3u8 playlist text to a temporary file
921
- open(os.path.join(self.path_manager.base_temp, "playlist.m3u8"), "w+", encoding="utf-8").write(m3u8_playlist_text)
922
-
923
- # Collect information about the playlist
924
- if self.is_playlist_url:
925
- self.content_extractor.start(self.instace_parserClass)
926
- else:
927
- self.content_extractor.start("https://fake.com", m3u8_playlist_text)
928
-
929
- def _process_playlist(self):
930
- """
931
- Processes the m3u8 playlist to download video, audio, and subtitles.
932
- """
933
- self._valida_playlist()
934
-
935
- # Add downloaded elements to the tracker
936
- self.download_tracker.add_video(self.content_extractor.m3u8_index)
937
- self.download_tracker.add_audio(self.content_extractor.list_available_audio)
938
- self.download_tracker.add_subtitle(self.content_extractor.list_available_subtitles)
939
-
940
- # Download each type of content
941
- if DOWNLOAD_VIDEO and len(self.download_tracker.downloaded_video) > 0:
942
- self.content_downloader.download_video(self.download_tracker.downloaded_video)
943
- if DOWNLOAD_AUDIO and len(self.download_tracker.downloaded_audio) > 0:
944
- self.content_downloader.download_audio(self.download_tracker.downloaded_audio)
945
- if DOWNLOAD_SUBTITLE and len(self.download_tracker.downloaded_subtitle) > 0:
946
- self.content_downloader.download_subtitle(self.download_tracker.downloaded_subtitle)
947
-
948
- # Join downloaded content
949
- self.content_joiner.setup(self.download_tracker.downloaded_video, self.download_tracker.downloaded_audio, self.download_tracker.downloaded_subtitle, self.content_extractor.codec)
950
-
951
- # Clean up temporary files and directories
952
- self._clean(self.content_joiner.converted_out_path)
953
-
954
- def _process_index(self):
955
- """
956
- Processes the m3u8 index to download only video.
957
- """
958
- m3u8_url_fixer.set_playlist(self.m3u8_index)
959
-
960
- # Download video
961
- self.download_tracker.add_video(self.m3u8_index)
962
- self.content_downloader.download_video(self.download_tracker.downloaded_video)
963
-
964
- # Join video
965
- self.content_joiner.setup(self.download_tracker.downloaded_video, [], [])
966
-
967
- # Clean up temporary files and directories
968
- self._clean(self.content_joiner.converted_out_path)