-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYoutube.py
56 lines (43 loc) · 2.14 KB
/
Youtube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import json
import os
import m3u8_To_MP4
import re
import requests as requests
from Parser_engine import ParserEngine
class Youtube(ParserEngine):
def __init__(self, path, link):
self.user_path = path
self.user_link = link
self.check_user_input()
def check_user_input(self):
while True:
if self.user_link.find("https://rutube.ru/video/") != -1 \
or self.user_link.find("https://rutube.ru/channel/") != -1 \
or self.user_link.find("https://rutube.ru/search/") != -1:
break
else:
self.user_link = input("Некорректная ссылка. Повторите ввод: ")
self.parsing(self.user_path, self.user_link)
self.user_link = ""
def parsing(self, download_path, rutube_link):
if rutube_link.find("https://www.youtube.com/watch/") != -1:
self.parsing_current_video(download_path, rutube_link)
if rutube_link.find("https://www.youtube.com/c/") != -1:
self.parsing_channel(download_path, rutube_link)
def parsing_current_video(self, path, link):
right_border_link = link.rfind("/")
id_video = link[link[:right_border_link].rfind("/") + 1:right_border_link]
video_json = json.loads(requests.get(f"http://rutube.ru/api/play/options/{id_video}/").content)
html = video_json['video_balancer']['m3u8']
content_html = requests.get(html).content.decode('utf-8')
final_link_m3u8 = content_html[content_html.rfind("http"):content_html.rfind("?")]
file_name = video_json['title']
if file_name[-1:] == '.' or file_name[-1:] == ' ':
file_name = file_name[:-1]
mp4_file_name = re.sub(r':*<*>*\"*\\*/*\|*\?*\**\«*\»*', "", file_name) + ".mp4"
m3u8_To_MP4.download(final_link_m3u8, mp4_file_dir=path)
file_old_name = os.path.join(path, "m3u8_To_Mp4.mp4")
file_new_name = os.path.join(path, mp4_file_name)
os.rename(file_old_name, file_new_name)
def parsing_channel(self, link, download_path):
pass