-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytmachine.py
153 lines (137 loc) · 3.38 KB
/
ytmachine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import time
import ujson as json
import pafy
import vlc
import requests
from bs4 import BeautifulSoup
class LiveStreamPlayer:
def __init__(self):
self.state = 3 # 0 - stopped, 1 - playing, 2 - paused
self.player = None
self.Instance = None
def update_permanent_url(self, url:str):
try:
data = self.read_json("data.json")
data["permanent_url"] = url
self.write_json(data, "data.json")
return True
except Exception as e:
print(e)
return False
def get_video_stream(self):
try:
data = self.read_json("data.json")
url = False
if data["video_url"]:
print("Found url")
url = data["video_url"]
else:
print("Not an URL")
print("Getting real url...")
url = self.get_video_id(data["permanent_url"])
if url:
data["video_url"] = url
self.write_json(data, "data.json")
else:
data["video_url"] = ""
self.write_json(data, "data.json")
raise Exception("Channel not on Live!!!")
print("URL found:", url)
print("Getting Best URL...")
playurl = self.get_video(url)
if not playurl:
print("Getting real url...")
url = self.get_video_id(data["permanent_url"])
if url:
data["video_url"] = url
self.write_json(data, "data.json")
playurl = self.get_video(url)
else:
data["video_url"] = ""
self.write_json(data, "data.json")
raise Exception("Channel not on Live!!!")
return playurl
except Exception as e:
print(e)
return False
def reload_video(self):
try:
print("Reloading video...")
playurl = self.get_video_stream()
if playurl:
Media = self.Instance.media_new(playurl)
Media.get_mrl()
self.player.set_media(Media)
print("Player ready!")
return True
return False
except Exception as e:
print(e)
return False
def pause(self):
self.state = 2
self.player.pause()
def play(self):
ready = True
if self.state == 0:
ready = self.reload_video()
if ready:
self.state = 1
self.player.play()
else:
print("Video couldn't be loaded.")
def stop(self):
self.state = 0
self.player.stop()
def write_json(self, data: dict, name: str = "data.json") -> bool:
try:
with open(name, 'w', encoding='utf-8') as outfile:
json.dump(data, outfile, ensure_ascii=False, indent=4)
return True
except Exception as e:
print(e)
return False
def read_json(self, name: str = "data.json") -> dict:
try:
data = []
with open(name) as json_file:
data = json.load(json_file)
return data
except Exception as e:
print(e)
return []
def get_video(self, url: str) -> str:
try:
video = pafy.new(url)
best = video.getbest()
playurl = best.url
#print(playurl)
return playurl
except Exception as e:
print(e)
return False
def get_video_id(self, url: str) -> str:
r = requests.get(url)
if int(r.status_code) == 200:
soup = BeautifulSoup(r.content, "html5lib")
murl = soup.find("link", {"rel":"canonical"})
return murl["href"]
else:
pass
def main(self) -> bool:
try:
playurl = self.get_video_stream()
if playurl:
print("Best URL found!")
print("Starting VLC...")
self.Instance = vlc.Instance('--no-video')
self.player = self.Instance.media_player_new()
Media = self.Instance.media_new(playurl)
Media.get_mrl()
self.player.set_media(Media)
print("Player ready!")
return True
return False
except Exception as e:
print(e)
return False