webscout 3.9__py3-none-any.whl → 4.1__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 webscout might be problematic. Click here for more details.

@@ -0,0 +1,1105 @@
1
+ import argparse
2
+ from datetime import datetime
3
+ import json
4
+ import logging
5
+ from time import sleep
6
+ import requests
7
+ from tqdm import tqdm
8
+ from colorama import Fore
9
+ from os import makedirs, path, getcwd
10
+ from threading import Thread
11
+ from sys import stdout
12
+ from click import launch as launch_media, confirm as confirm_from_user
13
+ import warnings
14
+
15
+ from os import getcwd, remove
16
+ import appdirs
17
+ """
18
+ - query string
19
+ - format mp4/3
20
+ - quality 720p/128kbps
21
+ - keywords
22
+ - Specify video author
23
+ - download related
24
+ - max-video limit
25
+ - min-video quality
26
+ - max-video quality
27
+ - path to file containing links
28
+ """
29
+ __version__ = "4.0"
30
+ __prog__ = "webscout"
31
+ session = requests.session()
32
+
33
+ headers = {
34
+ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
35
+ "User-Agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36",
36
+ "Accept-Encoding": "gzip, deflate, br",
37
+ "Accept-Language": "en-US,en;q=0.9",
38
+ "referer": "https://y2mate.com",
39
+ }
40
+
41
+ session.headers.update(headers)
42
+
43
+ get_excep = lambda e: e.args[1] if len(e.args) > 1 else e
44
+
45
+ appdir = appdirs.AppDirs(__prog__)
46
+
47
+ if not path.isdir(appdir.user_cache_dir):
48
+ try:
49
+ makedirs(appdir.user_cache_dir)
50
+ except Exception as e:
51
+ print(
52
+ f"Error : {get_excep(e)} while creating site directory - "
53
+ + appdir.user_cache_dir
54
+ )
55
+
56
+ history_path = path.join(appdir.user_cache_dir, "history.json")
57
+
58
+
59
+ class utils:
60
+ @staticmethod
61
+ def error_handler(resp=None, exit_on_error=False, log=True):
62
+ r"""Execption handler decorator"""
63
+
64
+ def decorator(func):
65
+ def main(*args, **kwargs):
66
+ try:
67
+ try:
68
+ return func(*args, **kwargs)
69
+ except KeyboardInterrupt as e:
70
+ print()
71
+ logging.info(f"^KeyboardInterrupt quitting. Goodbye!")
72
+ exit(1)
73
+ except Exception as e:
74
+ if log:
75
+ # logging.exception(e)
76
+ logging.debug(f"Function ({func.__name__}) : {get_excep(e)}")
77
+ logging.error(get_excep(e))
78
+ if exit_on_error:
79
+ exit(1)
80
+
81
+ return resp
82
+
83
+ return main
84
+
85
+ return decorator
86
+
87
+ @staticmethod
88
+ def get(*args, **kwargs):
89
+ r"""Sends http get request"""
90
+ resp = session.get(*args, **kwargs)
91
+ return all([resp.ok, "application/json" in resp.headers["content-type"]]), resp
92
+
93
+ @staticmethod
94
+ def post(*args, **kwargs):
95
+ r"""Sends http post request"""
96
+ resp = session.post(*args, **kwargs)
97
+ return all([resp.ok, "application/json" in resp.headers["content-type"]]), resp
98
+
99
+ @staticmethod
100
+ def add_history(data: dict) -> None:
101
+ f"""Adds entry to history
102
+ :param data: Response of `third query`
103
+ :type data: dict
104
+ :rtype: None
105
+ """
106
+ try:
107
+ if not path.isfile(history_path):
108
+ data1 = {__prog__: []}
109
+ with open(history_path, "w") as fh:
110
+ json.dump(data1, fh)
111
+ with open(history_path) as fh:
112
+ saved_data = json.load(fh).get(__prog__)
113
+ data["datetime"] = datetime.now().strftime("%c")
114
+ saved_data.append(data)
115
+ with open(history_path, "w") as fh:
116
+ json.dump({__prog__: saved_data}, fh, indent=4)
117
+ except Exception as e:
118
+ logging.error(f"Failed to add to history - {get_excep(e)}")
119
+
120
+ @staticmethod
121
+ def get_history(dump: bool = False) -> list:
122
+ r"""Loads download history
123
+ :param dump: (Optional) Return whole history as str
124
+ :type dump: bool
125
+ :rtype: list|str
126
+ """
127
+ try:
128
+ resp = []
129
+ if not path.isfile(history_path):
130
+ data1 = {__prog__: []}
131
+ with open(history_path, "w") as fh:
132
+ json.dump(data1, fh)
133
+ with open(history_path) as fh:
134
+ if dump:
135
+ return json.dumps(json.load(fh), indent=4)
136
+ entries = json.load(fh).get(__prog__)
137
+ for entry in entries:
138
+ resp.append(entry.get("vid"))
139
+ return resp
140
+ except Exception as e:
141
+ logging.error(f"Failed to load history - {get_excep(e)}")
142
+ return []
143
+
144
+
145
+ class first_query:
146
+ def __init__(self, query: str):
147
+ r"""Initializes first query class
148
+ :param query: Video name or youtube link
149
+ :type query: str
150
+ """
151
+ self.query_string = query
152
+ self.url = "https://www.y2mate.com/mates/analyzeV2/ajax"
153
+ self.payload = self.__get_payload()
154
+ self.processed = False
155
+ self.is_link = False
156
+
157
+ def __get_payload(self):
158
+ return {
159
+ "hl": "en",
160
+ "k_page": "home",
161
+ "k_query": self.query_string,
162
+ "q_auto": "0",
163
+ }
164
+
165
+ def __str__(self):
166
+ return """
167
+ {
168
+ "page": "search",
169
+ "status": "ok",
170
+ "keyword": "happy birthday",
171
+ "vitems": [
172
+ {
173
+ "v": "_z-1fTlSDF0",
174
+ "t": "Happy Birthday song"
175
+ },
176
+ ]
177
+ }"""
178
+
179
+ def __enter__(self, *args, **kwargs):
180
+ return self.__call__(*args, **kwargs)
181
+
182
+ def __exit__(self, *args, **kwargs):
183
+ self.processed = False
184
+
185
+ def __call__(self, timeout: int = 30):
186
+ return self.main(timeout)
187
+
188
+ def main(self, timeout=30):
189
+ r"""Sets class attributes
190
+ :param timeout: (Optional) Http requests timeout
191
+ :type timeout: int
192
+ """
193
+ logging.debug(f"Making first query : {self.payload.get('k_query')}")
194
+ okay_status, resp = utils.post(self.url, data=self.payload, timeout=timeout)
195
+ # print(resp.headers["content-type"])
196
+ # print(resp.content)
197
+ if okay_status:
198
+ dict_data = resp.json()
199
+ self.__setattr__("raw", dict_data)
200
+ for key in dict_data.keys():
201
+ self.__setattr__(key, dict_data.get(key))
202
+ self.is_link = not hasattr(self, "vitems")
203
+ self.processed = True
204
+ else:
205
+ logging.debug(f"{resp.headers.get('content-type')} - {resp.content}")
206
+ logging.error(f"First query failed - [{resp.status_code} : {resp.reason}")
207
+ return self
208
+
209
+
210
+ class second_query:
211
+ def __init__(self, query_one: object, item_no: int = 0):
212
+ r"""Initializes second_query class
213
+ :param query_one: Query_one class
214
+ :type query_one: object
215
+ :param item_no: (Optional) Query_one.vitems index
216
+ :type item_no: int
217
+ """
218
+ assert query_one.processed, "First query failed"
219
+
220
+ self.query_one = query_one
221
+ self.item_no = item_no
222
+ self.processed = False
223
+ self.video_dict = None
224
+ self.url = "https://www.y2mate.com/mates/analyzeV2/ajax"
225
+ # self.payload = self.__get_payload()
226
+
227
+ def __str__(self):
228
+ return """
229
+ {
230
+ "status": "ok",
231
+ "mess": "",
232
+ "page": "detail",
233
+ "vid": "_z-1fTlSDF0",
234
+ "extractor": "youtube",
235
+ "title": "Happy Birthday song",
236
+ "t": 62,
237
+ "a": "infobells",
238
+ "links": {
239
+ "mp4": {
240
+ "136": {
241
+ "size": "5.5 MB",
242
+ "f": "mp4",
243
+ "q": "720p",
244
+ "q_text": "720p (.mp4) <span class=\"label label-primary\"><small>m-HD</small></span>",
245
+ "k": "joVBVdm2xZWhaZWhu6vZ8cXxAl7j4qpyhNgqkwx0U/tcutx/harxdZ8BfPNcg9n1"
246
+ },
247
+ },
248
+ "mp3": {
249
+ "140": {
250
+ "size": "975.1 KB",
251
+ "f": "m4a",
252
+ "q": ".m4a",
253
+ "q_text": ".m4a (128kbps)",
254
+ "k": "joVBVdm2xZWhaZWhu6vZ8cXxAl7j4qpyhNhuxgxyU/NQ9919mbX2dYcdevRBnt0="
255
+ },
256
+ },
257
+ "related": [
258
+ {
259
+ "title": "Related Videos",
260
+ "contents": [
261
+ {
262
+ "v": "KK24ZvxLXGU",
263
+ "t": "Birthday Songs - Happy Birthday To You | 15 minutes plus"
264
+ },
265
+ ]
266
+ }
267
+ ]
268
+ }
269
+ """
270
+
271
+ def __call__(self, *args, **kwargs):
272
+ return self.main(*args, **kwargs)
273
+
274
+ def get_item(self, item_no=0):
275
+ r"""Return specific items on `self.query_one.vitems`"""
276
+ if self.video_dict:
277
+ return self.video_dict
278
+ if self.query_one.is_link:
279
+ return {"v": self.query_one.vid, "t": self.query_one.title}
280
+ all_items = self.query_one.vitems
281
+ assert (
282
+ self.item_no < len(all_items) - 1
283
+ ), "The item_no is greater than largest item's index - try lower value"
284
+
285
+ return self.query_one.vitems[item_no or self.item_no]
286
+
287
+ def get_payload(self):
288
+ return {
289
+ "hl": "en",
290
+ "k_page": "home",
291
+ "k_query": f"https://www.youtube.com/watch?v={self.get_item().get('v')}",
292
+ "q_auto": "1",
293
+ }
294
+
295
+ def __main__(self, *args, **kwargs):
296
+ return self.main(*args, **kwargs)
297
+
298
+ def __enter__(self, *args, **kwargs):
299
+ return self.__main__(*args, **kwargs)
300
+
301
+ def __exit__(self, *args, **kwargs):
302
+ self.processed = False
303
+
304
+ def main(self, item_no: int = 0, timeout: int = 30):
305
+ r"""Requests for video formats and related videos
306
+ :param item_no: (Optional) Index of query_one.vitems
307
+ :type item_no: int
308
+ :param timeout: (Optional)Http request timeout
309
+ :type timeout: int
310
+ """
311
+ self.processed = False
312
+ if item_no:
313
+ self.item_no = item_no
314
+ okay_status, resp = utils.post(
315
+ self.url, data=self.get_payload(), timeout=timeout
316
+ )
317
+
318
+ if okay_status:
319
+ dict_data = resp.json()
320
+ for key in dict_data.keys():
321
+ self.__setattr__(key, dict_data.get(key))
322
+ links = dict_data.get("links")
323
+ self.__setattr__("video", links.get("mp4"))
324
+ self.__setattr__("audio", links.get("mp3"))
325
+ self.__setattr__("related", dict_data.get("related")[0].get("contents"))
326
+ self.__setattr__("raw", dict_data)
327
+ self.processed = True
328
+
329
+ else:
330
+ logging.debug(f"{resp.headers.get('content-type')} - {resp.content}")
331
+ logging.error(f"Second query failed - [{resp.status_code} : {resp.reason}]")
332
+ return self
333
+
334
+
335
+ class third_query:
336
+ def __init__(self, query_two: object):
337
+ assert query_two.processed, "Unprocessed second_query object parsed"
338
+ self.query_two = query_two
339
+ self.url = "https://www.y2mate.com/mates/convertV2/index"
340
+ self.formats = ["mp4", "mp3"]
341
+ self.qualities_plus = ["best", "worst"]
342
+ self.qualities = {
343
+ self.formats[0]: [
344
+ "4k",
345
+ "1080p",
346
+ "720p",
347
+ "480p",
348
+ "360p",
349
+ "240p",
350
+ "144p",
351
+ "auto",
352
+ ]
353
+ + self.qualities_plus,
354
+ self.formats[1]: ["mp3", "m4a", ".m4a", "128kbps", "192kbps", "328kbps"],
355
+ }
356
+
357
+ def __call__(self, *args, **kwargs):
358
+ return self.main(*args, **kwargs)
359
+
360
+ def __enter__(self, *args, **kwargs):
361
+ return self
362
+
363
+ def __exit__(self, *args, **kwargs):
364
+ pass
365
+
366
+ def __str__(self):
367
+ return """
368
+ {
369
+ "status": "ok",
370
+ "mess": "",
371
+ "c_status": "CONVERTED",
372
+ "vid": "_z-1fTlSDF0",
373
+ "title": "Happy Birthday song",
374
+ "ftype": "mp4",
375
+ "fquality": "144p",
376
+ "dlink": "https://dl165.dlmate13.online/?file=M3R4SUNiN3JsOHJ6WWQ2a3NQS1Y5ZGlxVlZIOCtyZ01tY1VxM2xzQkNMbFlyb2t1enErekxNZElFYkZlbWQ2U1g5TkVvWGplZU55T0R4K0lvcEI3QnlHbjd0a29yU3JOOXN0eWY4UmhBbE9xdmI3bXhCZEprMHFrZU96QkpweHdQVWh0OGhRMzQyaWUzS1dTdmhEMzdsYUk0VWliZkMwWXR5OENNUENOb01rUWd6NmJQS2UxaGRZWHFDQ2c0WkpNMmZ2QTVVZmx5cWc3NVlva0Nod3NJdFpPejhmeDNhTT0%3D"
377
+ }
378
+ """
379
+
380
+ def get_payload(self, keys):
381
+ return {"k": keys.get("k"), "vid": self.query_two.vid}
382
+
383
+ def main(
384
+ self,
385
+ format: str = "mp4",
386
+ quality="auto",
387
+ resolver: str = None,
388
+ timeout: int = 30,
389
+ ):
390
+ r"""
391
+ :param format: (Optional) Media format mp4/mp3
392
+ :param quality: (Optional) Media qualiy such as 720p
393
+ :param resolver: (Optional) Additional format info : [m4a,3gp,mp4,mp3]
394
+ :param timeout: (Optional) Http requests timeout
395
+ :type type: str
396
+ :type quality: str
397
+ :type timeout: int
398
+ """
399
+ if not resolver:
400
+ resolver = "mp4" if format == "mp4" else "mp3"
401
+ if format == "mp3" and quality == "auto":
402
+ quality = "128kbps"
403
+ assert (
404
+ format in self.formats
405
+ ), f"'{format}' is not in supported formats - {self.formats}"
406
+
407
+ assert (
408
+ quality in self.qualities[format]
409
+ ), f"'{quality}' is not in supported qualities - {self.qualities[format]}"
410
+
411
+ items = self.query_two.video if format == "mp4" else self.query_two.audio
412
+ hunted = []
413
+ if quality in self.qualities_plus:
414
+ keys = list(items.keys())
415
+ if quality == self.qualities_plus[0]:
416
+ hunted.append(items[keys[0]])
417
+ else:
418
+ hunted.append(items[keys[len(keys) - 2]])
419
+ else:
420
+ for key in items.keys():
421
+ if items[key].get("q") == quality:
422
+ hunted.append(items[key])
423
+ if len(hunted) > 1:
424
+ for entry in hunted:
425
+ if entry.get("f") == resolver:
426
+ hunted.insert(0, entry)
427
+ if hunted:
428
+
429
+ def hunter_manager(souped_entry: dict = hunted[0], repeat_count=0):
430
+ payload = self.get_payload(souped_entry)
431
+ okay_status, resp = utils.post(self.url, data=payload)
432
+ if okay_status:
433
+ sanitized_feedback = resp.json()
434
+ if sanitized_feedback.get("c_status") == "CONVERTING":
435
+ if repeat_count >= 4:
436
+ return (False, {})
437
+ else:
438
+ logging.debug(
439
+ f"Converting video : sleeping for 5s - round {repeat_count+1}"
440
+ )
441
+ sleep(5)
442
+ repeat_count += 1
443
+ return hunter_manager(souped_entry)
444
+ return okay_status, resp
445
+ return okay_status, resp
446
+
447
+ okay_status, resp = hunter_manager()
448
+
449
+ if okay_status:
450
+ resp_data = hunted[0]
451
+ resp_data.update(resp.json())
452
+ return resp_data
453
+
454
+ else:
455
+ logging.debug(f"{resp.headers.get('content-type')} - {resp.content}")
456
+ logging.error(
457
+ f"Third query failed - [{resp.status_code} : {resp.reason}]"
458
+ )
459
+ return {}
460
+ else:
461
+ logging.error(
462
+ f"Zero media hunted with params : {{quality : {quality}, format : {format} }}"
463
+ )
464
+ return {}
465
+ class Handler:
466
+ def __init__(
467
+ self,
468
+ query: str,
469
+ author: str = None,
470
+ timeout: int = 30,
471
+ confirm: bool = False,
472
+ unique: bool = False,
473
+ thread: int = 0,
474
+ ):
475
+ r"""Initializes this `class`
476
+ :param query: Video name or youtube link
477
+ :type query: str
478
+ :param author: (Optional) Author (Channel) of the videos
479
+ :type author: str
480
+ :param timeout: (Optional) Http request timeout
481
+ :type timeout: int
482
+ :param confirm: (Optional) Confirm before downloading media
483
+ :type confirm: bool
484
+ :param unique: (Optional) Ignore previously downloaded media
485
+ :type confirm: bool
486
+ :param thread: (Optional) Thread the download process through `auto-save` method
487
+ :type thread int
488
+ """
489
+ self.query = query
490
+ self.author = author
491
+ self.timeout = timeout
492
+ self.keyword = None
493
+ self.confirm = confirm
494
+ self.unique = unique
495
+ self.thread = thread
496
+ self.vitems = []
497
+ self.related = []
498
+ self.dropped = []
499
+ self.total = 1
500
+ self.saved_videos = utils.get_history()
501
+
502
+ def __str__(self):
503
+ return self.query
504
+
505
+ def __enter__(self, *args, **kwargs):
506
+ return self
507
+
508
+ def __exit__(self, *args, **kwargs):
509
+ self.vitems.clear()
510
+ self.total = 1
511
+
512
+ def __call__(self, *args, **kwargs):
513
+ return self.run(*args, **kwargs)
514
+
515
+ def __filter_videos(self, entries: list) -> list:
516
+ f"""Filter videos based on keyword
517
+ :param entries: List containing dict of video id and their titles
518
+ :type entries: list
519
+ :rtype: list
520
+ """
521
+ if self.keyword:
522
+ keyword = self.keyword.lower()
523
+ resp = []
524
+ for entry in entries:
525
+ if keyword in entry.get("t").lower():
526
+ resp.append(entry)
527
+ return resp
528
+
529
+ else:
530
+ return entries
531
+
532
+ def __make_first_query(self):
533
+ r"""Sets query_one attribute to `self`"""
534
+ query_one = first_query(self.query)
535
+ self.__setattr__("query_one", query_one.main(self.timeout))
536
+ if self.query_one.is_link == False:
537
+ self.vitems.extend(self.__filter_videos(self.query_one.vitems))
538
+
539
+ @utils.error_handler(exit_on_error=True)
540
+ def __verify_item(self, second_query_obj) -> bool:
541
+ video_id = second_query_obj.vid
542
+ video_author = second_query_obj.a
543
+ video_title = second_query_obj.title
544
+ if video_id in self.saved_videos:
545
+ if self.unique:
546
+ return False, "Duplicate"
547
+ if self.confirm:
548
+ choice = confirm_from_user(
549
+ f">> Re-download : {Fore.GREEN+video_title+Fore.RESET} by {Fore.YELLOW+video_author+Fore.RESET}"
550
+ )
551
+ print("\n[*] Ok processing...", end="\r")
552
+ return choice, "User's choice"
553
+ if self.confirm:
554
+ choice = confirm_from_user(
555
+ f">> Download : {Fore.GREEN+video_title+Fore.RESET} by {Fore.YELLOW+video_author+Fore.RESET}"
556
+ )
557
+ print("\n[*] Ok processing...", end="\r")
558
+ return choice, "User's choice"
559
+ return True, "Auto"
560
+
561
+ def __make_second_query(self):
562
+ r"""Links first query with 3rd query"""
563
+ init_query_two = second_query(self.query_one)
564
+ x = 0
565
+ if not self.query_one.is_link:
566
+ for video_dict in self.vitems:
567
+ init_query_two.video_dict = video_dict
568
+ query_2 = init_query_two.main(timeout=self.timeout)
569
+ if query_2.processed:
570
+ if query_2.vid in self.dropped:
571
+ continue
572
+ if self.author and not self.author.lower() in query_2.a.lower():
573
+ logging.warning(
574
+ f"Dropping {Fore.YELLOW+query_2.title+Fore.RESET} by {Fore.RED+query_2.a+Fore.RESET}"
575
+ )
576
+ continue
577
+ else:
578
+ yes_download, reason = self.__verify_item(query_2)
579
+ if not yes_download:
580
+ logging.warning(
581
+ f"Skipping {Fore.YELLOW+query_2.title+Fore.RESET} by {Fore.MAGENTA+query_2.a+Fore.RESET} - Reason : {Fore.BLUE+reason+Fore.RESET}"
582
+ )
583
+ self.dropped.append(query_2.vid)
584
+ continue
585
+ self.related.append(query_2.related)
586
+ yield query_2
587
+ x += 1
588
+ if x >= self.total:
589
+ break
590
+ else:
591
+ logging.warning(
592
+ f"Dropping unprocessed query_two object of index {x}"
593
+ )
594
+
595
+ else:
596
+ query_2 = init_query_two.main(timeout=self.timeout)
597
+ if query_2.processed:
598
+ # self.related.extend(query_2.related)
599
+ self.vitems.extend(query_2.related)
600
+ self.query_one.is_link = False
601
+ if self.total == 1:
602
+ yield query_2
603
+ else:
604
+ for video_dict in self.vitems:
605
+ init_query_two.video_dict = video_dict
606
+ query_2 = init_query_two.main(timeout=self.timeout)
607
+ if query_2.processed:
608
+ if (
609
+ self.author
610
+ and not self.author.lower() in query_2.a.lower()
611
+ ):
612
+ logging.warning(
613
+ f"Dropping {Fore.YELLOW+query_2.title+Fore.RESET} by {Fore.RED+query_2.a+Fore.RESET}"
614
+ )
615
+ continue
616
+ else:
617
+ yes_download, reason = self.__verify_item(query_2)
618
+ if not yes_download:
619
+ logging.warning(
620
+ f"Skipping {Fore.YELLOW+query_2.title+Fore.RESET} by {Fore.MAGENTA+query_2.a+Fore.RESET} - Reason : {Fore.BLUE+reason+Fore.RESET}"
621
+ )
622
+ self.dropped.append(query_2.vid)
623
+ continue
624
+
625
+ self.related.append(query_2.related)
626
+ yield query_2
627
+ x += 1
628
+ if x >= self.total:
629
+ break
630
+ else:
631
+ logging.warning(
632
+ f"Dropping unprocessed query_two object of index {x}"
633
+ )
634
+ yield
635
+ else:
636
+ logging.warning("Dropping unprocessed query_two object")
637
+ yield
638
+
639
+ def run(
640
+ self,
641
+ format: str = "mp4",
642
+ quality: str = "auto",
643
+ resolver: str = None,
644
+ limit: int = 1,
645
+ keyword: str = None,
646
+ author: str = None,
647
+ ):
648
+ r"""Generate and yield video dictionary
649
+ :param format: (Optional) Media format mp4/mp3
650
+ :param quality: (Optional) Media qualiy such as 720p/128kbps
651
+ :param resolver: (Optional) Additional format info : [m4a,3gp,mp4,mp3]
652
+ :param limit: (Optional) Total videos to be generated
653
+ :param keyword: (Optional) Video keyword
654
+ :param author: (Optional) Author of the videos
655
+ :type quality: str
656
+ :type total: int
657
+ :type keyword: str
658
+ :type author: str
659
+ :rtype: object
660
+ """
661
+ self.author = author
662
+ self.keyword = keyword
663
+ self.total = limit
664
+ self.__make_first_query()
665
+ for query_two_obj in self.__make_second_query():
666
+ if query_two_obj:
667
+ self.vitems.extend(query_two_obj.related)
668
+ yield third_query(query_two_obj).main(
669
+ **dict(
670
+ format=format,
671
+ quality=quality,
672
+ resolver=resolver,
673
+ timeout=self.timeout,
674
+ )
675
+ )
676
+ else:
677
+ logging.error(f"Empty object - {query_two_obj}")
678
+
679
+ def generate_filename(self, third_dict: dict, naming_format: str = None) -> str:
680
+ r"""Generate filename based on the response of `third_query`
681
+ :param third_dict: response of `third_query.main()` object
682
+ :param naming_format: (Optional) Format for generating filename based on `third_dict` keys
683
+ :type third_dict: dict
684
+ :type naming_format: str
685
+ :rtype: str
686
+ """
687
+ fnm = (
688
+ f"{naming_format}" % third_dict
689
+ if naming_format
690
+ else f"{third_dict['title']} {third_dict['vid']}_{third_dict['fquality']}.{third_dict['ftype']}"
691
+ )
692
+
693
+ def sanitize(nm):
694
+ trash = [
695
+ "\\",
696
+ "/",
697
+ ":",
698
+ "*",
699
+ "?",
700
+ '"',
701
+ "<",
702
+ "|",
703
+ ">",
704
+ "y2mate.com",
705
+ "y2mate com",
706
+ ]
707
+ for val in trash:
708
+ nm = nm.replace(val, "")
709
+ return nm.strip()
710
+
711
+ return sanitize(fnm)
712
+
713
+ def auto_save(
714
+ self,
715
+ dir: str = "",
716
+ iterator: object = None,
717
+ progress_bar=True,
718
+ quiet: bool = False,
719
+ naming_format: str = None,
720
+ chunk_size: int = 512,
721
+ play: bool = False,
722
+ resume: bool = False,
723
+ *args,
724
+ **kwargs,
725
+ ):
726
+ r"""Query and save all the media
727
+ :param dir: (Optional) Path to Directory for saving the media files
728
+ :param iterator: (Optional) Function that yields third_query object - `Handler.run`
729
+ :param progress_bar: (Optional) Display progress bar
730
+ :param quiet: (Optional) Not to stdout anything
731
+ :param naming_format: (Optional) Format for generating filename
732
+ :param chunk_size: (Optional) Chunk_size for downloading files in KB
733
+ :param play: (Optional) Auto-play the media after download
734
+ :param resume: (Optional) Resume the incomplete download
735
+ :type dir: str
736
+ :type iterator: object
737
+ :type progress_bar: bool
738
+ :type quiet: bool
739
+ :type naming_format: str
740
+ :type chunk_size: int
741
+ :type play: bool
742
+ :type resume: bool
743
+ args & kwargs for the iterator
744
+ :rtype: None
745
+ """
746
+ iterator_object = iterator or self.run(*args, **kwargs)
747
+
748
+ for x, entry in enumerate(iterator_object):
749
+ if self.thread:
750
+ t1 = Thread(
751
+ target=self.save,
752
+ args=(
753
+ entry,
754
+ dir,
755
+ False,
756
+ quiet,
757
+ naming_format,
758
+ chunk_size,
759
+ play,
760
+ resume,
761
+ ),
762
+ )
763
+ t1.start()
764
+ thread_count = x + 1
765
+ if thread_count % self.thread == 0 or thread_count == self.total:
766
+ logging.debug(
767
+ f"Waiting for current running threads to finish - thread_count : {thread_count}"
768
+ )
769
+ t1.join()
770
+ else:
771
+ self.save(
772
+ entry,
773
+ dir,
774
+ progress_bar,
775
+ quiet,
776
+ naming_format,
777
+ chunk_size,
778
+ play,
779
+ resume,
780
+ )
781
+
782
+ def save(
783
+ self,
784
+ third_dict: dict,
785
+ dir: str = "",
786
+ progress_bar=True,
787
+ quiet: bool = False,
788
+ naming_format: str = None,
789
+ chunk_size: int = 512,
790
+ play: bool = False,
791
+ resume: bool = False,
792
+ disable_history=False,
793
+ ):
794
+ r"""Download media based on response of `third_query` dict-data-type
795
+ :param third_dict: Response of `third_query.run()`
796
+ :param dir: (Optional) Directory for saving the contents
797
+ :param progress_bar: (Optional) Display download progress bar
798
+ :param quiet: (Optional) Not to stdout anything
799
+ :param naming_format: (Optional) Format for generating filename
800
+ :param chunk_size: (Optional) Chunk_size for downloading files in KB
801
+ :param play: (Optional) Auto-play the media after download
802
+ :param resume: (Optional) Resume the incomplete download
803
+ :param disable_history (Optional) Don't save the download to history.
804
+ :type third_dict: dict
805
+ :type dir: str
806
+ :type progress_bar: bool
807
+ :type quiet: bool
808
+ :type naming_format: str
809
+ :type chunk_size: int
810
+ :type play: bool
811
+ :type resume: bool
812
+ :type disable_history: bool
813
+ :rtype: None
814
+ """
815
+ if third_dict:
816
+ assert third_dict.get(
817
+ "dlink"
818
+ ), "The video selected does not support that quality, try lower qualities."
819
+ if third_dict.get("mess"):
820
+ logging.warning(third_dict.get("mess"))
821
+
822
+ current_downloaded_size = 0
823
+ current_downloaded_size_in_mb = 0
824
+ filename = self.generate_filename(third_dict, naming_format)
825
+ save_to = path.join(dir, filename)
826
+ mod_headers = headers
827
+
828
+ if resume:
829
+ assert path.exists(save_to), f"File not found in path - '{save_to}'"
830
+ current_downloaded_size = path.getsize(save_to)
831
+ # Set the headers to resume download from the last byte
832
+ mod_headers = {"Range": f"bytes={current_downloaded_size}-"}
833
+ current_downloaded_size_in_mb = round(
834
+ current_downloaded_size / 1000000, 2
835
+ ) # convert to mb
836
+
837
+ resp = requests.get(third_dict["dlink"], stream=True, headers=mod_headers)
838
+
839
+ default_content_length = 0
840
+ size_in_bytes = int(
841
+ resp.headers.get("content-length", default_content_length)
842
+ )
843
+ if not size_in_bytes:
844
+ if resume:
845
+ raise FileExistsError(
846
+ f"Download completed for the file in path - '{save_to}'"
847
+ )
848
+ else:
849
+ raise Exception(
850
+ f"Cannot download file of content-length {size_in_bytes} bytes"
851
+ )
852
+
853
+ if resume:
854
+ assert (
855
+ size_in_bytes != current_downloaded_size
856
+ ), f"Download completed for the file in path - '{save_to}'"
857
+
858
+ size_in_mb = (
859
+ round(size_in_bytes / 1000000, 2) + current_downloaded_size_in_mb
860
+ )
861
+ chunk_size_in_bytes = chunk_size * 1024
862
+
863
+ third_dict["saved_to"] = (
864
+ save_to
865
+ if any([save_to.startswith("/"), ":" in save_to])
866
+ else path.join(getcwd(), dir, filename)
867
+ )
868
+ try_play_media = (
869
+ lambda: launch_media(third_dict["saved_to"]) if play else None
870
+ )
871
+ saving_mode = "ab" if resume else "wb"
872
+ if progress_bar:
873
+ if not quiet:
874
+ print(f"{filename}")
875
+ with tqdm(
876
+ total=size_in_bytes + current_downloaded_size,
877
+ bar_format="%s%d MB %s{bar} %s{l_bar}%s"
878
+ % (Fore.GREEN, size_in_mb, Fore.CYAN, Fore.YELLOW, Fore.RESET),
879
+ initial=current_downloaded_size,
880
+ ) as p_bar:
881
+ # p_bar.update(current_downloaded_size)
882
+ with open(save_to, saving_mode) as fh:
883
+ for chunks in resp.iter_content(chunk_size=chunk_size_in_bytes):
884
+ fh.write(chunks)
885
+ p_bar.update(chunk_size_in_bytes)
886
+ if not disable_history:
887
+ utils.add_history(third_dict)
888
+ try_play_media()
889
+ return save_to
890
+ else:
891
+ with open(save_to, saving_mode) as fh:
892
+ for chunks in resp.iter_content(chunk_size=chunk_size_in_bytes):
893
+ fh.write(chunks)
894
+ if not disable_history:
895
+ utils.add_history(third_dict)
896
+
897
+ try_play_media()
898
+ logging.info(f"{filename} - {size_in_mb}MB ✅")
899
+ return save_to
900
+ else:
901
+ logging.error(f"Empty `third_dict` parameter parsed : {third_dict}")
902
+
903
+ mp4_qualities = [
904
+ "4k",
905
+ "1080p",
906
+ "720p",
907
+ "480p",
908
+ "360p",
909
+ "240p",
910
+ "144p",
911
+ "auto",
912
+ "best",
913
+ "worst",
914
+ ]
915
+ mp3_qualities = ["mp3", "m4a", ".m4a", "128kbps", "192kbps", "328kbps"]
916
+ resolvers = ["m4a", "3gp", "mp4", "mp3"]
917
+ media_qualities = mp4_qualities + mp3_qualities
918
+ logging.basicConfig(
919
+ format="%(asctime)s - %(levelname)s : %(message)s",
920
+ datefmt="%H:%M:%S",
921
+ level=logging.INFO,
922
+ )
923
+
924
+
925
+ def get_args():
926
+ parser = argparse.ArgumentParser(
927
+ description="Youtube video downloader", add_help=True, exit_on_error=True
928
+ )
929
+ parser.add_argument(
930
+ "-v", "--version", action="version", version=f"%(prog)s v{__version__}"
931
+ )
932
+ parser.add_argument(
933
+ "query", nargs="*", help="Youtube video title, link or id - %(default)s"
934
+ )
935
+ parser.add_argument(
936
+ "-f",
937
+ "--format",
938
+ help="Specify media type - audio/video",
939
+ choices=["mp3", "mp4"],
940
+ metavar="mp3|mp4",
941
+ )
942
+ parser.add_argument(
943
+ "-q",
944
+ "--quality",
945
+ help="Media quality - %(default)s",
946
+ choices=media_qualities,
947
+ metavar="|".join(media_qualities),
948
+ default="auto",
949
+ )
950
+ parser.add_argument(
951
+ "-r",
952
+ "--resolver",
953
+ help="Other media formats incase of multiple options - mp4/mp3",
954
+ choices=resolvers,
955
+ metavar="|".join(resolvers),
956
+ )
957
+ parser.add_argument(
958
+ "-k",
959
+ "--keyword",
960
+ nargs="*",
961
+ help="Media should contain this keywords - %(default)s",
962
+ )
963
+ parser.add_argument(
964
+ "-a",
965
+ "--author",
966
+ nargs="*",
967
+ help="Media author i.e YouTube channel name - %(default)s",
968
+ )
969
+ parser.add_argument(
970
+ "-l",
971
+ "--limit",
972
+ help="Total videos to be downloaded - %(default)s",
973
+ type=int,
974
+ default=1,
975
+ )
976
+ parser.add_argument(
977
+ "-d",
978
+ "--dir",
979
+ help="Directory for saving the contents - %(default)s",
980
+ default=getcwd(),
981
+ metavar="PATH",
982
+ )
983
+ parser.add_argument(
984
+ "-t",
985
+ "--timeout",
986
+ help="Http request timeout in seconds - %(default)s",
987
+ type=int,
988
+ default=30,
989
+ )
990
+ parser.add_argument(
991
+ "-c",
992
+ "--chunk",
993
+ help="Chunk-size for downloading files in KB - %(default)s",
994
+ type=int,
995
+ default=256,
996
+ )
997
+ parser.add_argument(
998
+ "-i",
999
+ "--input",
1000
+ help="Path to text file containing query per line - %(default)s",
1001
+ metavar="PATH",
1002
+ )
1003
+ parser.add_argument(
1004
+ "-o",
1005
+ "--output",
1006
+ metavar="FORMAT",
1007
+ help="Format for generating filename %%(key)s : [title,vid,fquality,ftype] or 'pretty' - %(default)s",
1008
+ )
1009
+ parser.add_argument(
1010
+ "-thr",
1011
+ "--thread",
1012
+ help="Download [x] amount of videos/audios at once - 1",
1013
+ type=int,
1014
+ default=0,
1015
+ )
1016
+ parser.add_argument(
1017
+ "--disable-bar",
1018
+ help="Disable download progress bar - %(default)s",
1019
+ action="store_true",
1020
+ )
1021
+ parser.add_argument(
1022
+ "--confirm",
1023
+ help="Confirm before downloading file - %(default)s",
1024
+ action="store_true",
1025
+ )
1026
+ parser.add_argument(
1027
+ "--unique",
1028
+ help="Auto-skip any media that you once dowloaded - %(default)s",
1029
+ action="store_true",
1030
+ )
1031
+ parser.add_argument(
1032
+ "--quiet",
1033
+ help="Not to stdout anything other than logs - %(default)s",
1034
+ action="store_true",
1035
+ )
1036
+ parser.add_argument(
1037
+ "--history",
1038
+ help="Stdout all media metadata ever downloaded - %(default)s",
1039
+ action="store_true",
1040
+ )
1041
+ parser.add_argument(
1042
+ "--clear",
1043
+ help="Clear all download histories - %(default)s",
1044
+ action="store_true",
1045
+ )
1046
+ parser.add_argument(
1047
+ "--resume", action="store_true", help="Resume downloading incomplete downloads"
1048
+ )
1049
+ parser.add_argument(
1050
+ "--play", help="Play media after download - %(default)s", action="store_true"
1051
+ )
1052
+ return parser.parse_args()
1053
+
1054
+
1055
+ @utils.error_handler(exit_on_error=True)
1056
+ def main():
1057
+ args = get_args()
1058
+ if args.history:
1059
+ print(utils.get_history(dump=True))
1060
+ exit(0)
1061
+ if args.clear:
1062
+ remove(history_path)
1063
+ logging.info("Histories cleared successfully!")
1064
+ exit(0)
1065
+ if not args.format:
1066
+ raise Exception("You must specify media format [ -f mp3/4]")
1067
+ h_mult_args = lambda v: v if not v else " ".join(v)
1068
+ handler_init_args = dict(
1069
+ query=h_mult_args(args.query),
1070
+ author=args.author,
1071
+ timeout=args.timeout,
1072
+ confirm=args.confirm,
1073
+ unique=args.unique,
1074
+ thread=args.thread,
1075
+ )
1076
+ auto_save_args = dict(
1077
+ dir=args.dir,
1078
+ progress_bar=args.disable_bar == False,
1079
+ quiet=args.quiet,
1080
+ naming_format=f"%(title)s{' - %(fquality)s' if args.format=='mp4' else ''}.%(ftype)s"
1081
+ if str(args.output).lower() == "pretty"
1082
+ else args.output,
1083
+ chunk_size=args.chunk,
1084
+ play=args.play,
1085
+ format=args.format,
1086
+ quality=args.quality,
1087
+ resolver=args.resolver,
1088
+ limit=args.limit,
1089
+ keyword=h_mult_args(args.keyword),
1090
+ author=h_mult_args(args.author),
1091
+ resume=args.resume,
1092
+ )
1093
+ logging.info(f"webscout - v{__version__}")
1094
+ if args.input:
1095
+ for query in open(args.input).read().strip().split("\n"):
1096
+ handler_init_args["query"] = query
1097
+ auto_save_args["limit"] = 1
1098
+ Handler(**handler_init_args).auto_save(**auto_save_args)
1099
+ else:
1100
+ Handler(**handler_init_args).auto_save(**auto_save_args)
1101
+ logging.info(
1102
+ f"Done downloading ({args.limit}) {'audio' if args.format=='mp3' else 'video'}{'' if args.limit==1 else 's'}"
1103
+ )
1104
+ if __name__ == "__main__":
1105
+ main()