-
Notifications
You must be signed in to change notification settings - Fork 0
/
wco_streamer.py
219 lines (178 loc) · 5.9 KB
/
wco_streamer.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import bs4
import vlc
from urllib2 import urlopen as uReq
from bs4 import BeautifulSoup as soup
import random
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import gi
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gdk
from gi.repository import Gtk
Gdk.threads_init ()
show_titles = ['']
base_url = 'https://www.watchcartoononline.com/anime/'
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options);
# uses random.choice to pick a random episode from a random title in show_titles[]
def get_src_of_rand_video():
_url = base_url+random.choice(show_titles)
uClient = uReq(_url)
page_soup = soup(uClient.read(), "html.parser")
uClient.close()
episodes = page_soup.select("div.cat-eps a.sonra")
link = random.choice(episodes).get('href')
#get video source
driver.get(link)
iframes = driver.find_elements_by_tag_name('iframe')
driver.switch_to.frame(1)
src = driver.find_element_by_id('video-js').find_element_by_tag_name('source').get_attribute('src')
return src
class ApplicationWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Python-Vlc Media Player")
self.player_paused=False
self.is_player_active = False
self.connect("destroy",Gtk.main_quit)
self.isFullscreen = False
self.volume = 100
def show(self):
self.show_all()
def setup_objects_and_events(self):
self.connect("key-press-event",self._key_press_event)
self.playback_button = Gtk.Button()
self.stop_button = Gtk.Button()
self.update_button = Gtk.Button()
self.play_image = Gtk.Image.new_from_icon_name(
"gtk-media-play",
Gtk.IconSize.MENU
)
self.pause_image = Gtk.Image.new_from_icon_name(
"gtk-media-pause",
Gtk.IconSize.MENU
)
self.stop_image = Gtk.Image.new_from_icon_name(
"gtk-media-stop",
Gtk.IconSize.MENU
)
self.update_image = Gtk.Image.new_from_icon_name(
"gtk-media-update",
Gtk.IconSize.MENU
)
self.playback_button.set_image(self.play_image)
self.stop_button.set_image(self.stop_image)
self.update_button.set_image(self.update_image)
self.playback_button.connect("clicked", self.toggle_player_playback)
self.stop_button.connect("clicked", self.stop_player)
self.update_button.connect("clicked", self.update)
self.draw_area = Gtk.DrawingArea()
self.draw_area.set_size_request(300,300)
self.draw_area.connect("realize",self._realized)
self.hbox = Gtk.Box(spacing=6)
self.hbox.pack_start(self.playback_button, True, True, 0)
self.hbox.pack_start(self.stop_button, True, True, 0)
self.hbox.pack_start(self.update_button, True, True, 0)
self.vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(self.vbox)
self.vbox.pack_start(self.draw_area, True, True, 0)
self.vbox.pack_start(self.hbox, False, False, 0)
def toggle_player_playback(self, widget, data=None):
"""
Handler for Player's Playback Button (Play/Pause).
"""
if self.is_player_active == False and self.player_paused == False:
self.player.play()
self.playback_button.set_image(self.pause_image)
self.is_player_active = True
elif self.is_player_active == True and self.player_paused == True:
self.player.play()
self.playback_button.set_image(self.pause_image)
self.player_paused = False
elif self.is_player_active == True and self.player_paused == False:
self.player.pause()
self.playback_button.set_image(self.play_image)
self.player_paused = True
else:
pass
def _key_press_event(self,widget,event):
keyval = event.keyval
keyval_name = Gdk.keyval_name(keyval)
state = event.state
print(keyval_name)
if keyval_name == 'space':
self.toggle_player_playback(self)
elif keyval_name == 'Tab':
self.update(self)
elif keyval_name == "s":
self.stop_player(self)
elif keyval_name == "Escape":
self.destroy()
elif keyval_name == "f":
self.toggle_fullscreen(self)
elif keyval_name == "Up":
if self.volume < 100:
self.volume +=1
print(self.volume)
self.player.audio_set_volume(self.volume)
elif keyval_name == "Down":
if self.volume > 0:
self.volume -=1
print(self.volume)
self.player.audio_set_volume(self.volume)
else:
return False
return True
def update(self,widget, data=None):
self.stop_player(self)
self.src = get_src_of_rand_video()
self.set_my_media(self.player, self.vlcInstance, self.src)
self.player.play()
def toggle_fullscreen(self, widget, data=None):
if self.isFullscreen == True:
self.isFullscreen = False
self.hbox.show()
self.set_decorated(True)
self.unmaximize()
self.unfullscreen()
elif self.isFullscreen == False:
self.isFullscreen = True
self.set_decorated(False)
self.hbox.hide()
self.maximize()
self.fullscreen()
def stop_player(self, widget, data=None):
self.player.stop()
self.is_player_active = False
self.playback_button.set_image(self.play_image)
def set_my_media(self, _player, _instance, _source):
_media = _instance.media_new(_source)
_media.get_mrl
_player.set_media(_media)
return _player
def end_reached(self):
self.update(self)
def _realized(self, widget, data=None):
self.vlcInstance = vlc.Instance("--no-xlib")
self.src = get_src_of_rand_video()
self.player = self.vlcInstance.media_player_new()
win_id = widget.get_window().get_xid()
self.player.set_xwindow(win_id)
self.player.set_fullscreen(True)
self.player = self.set_my_media(self.player, self.vlcInstance, self.src)
self.player.play()
self.playback_button.set_image(self.pause_image)
self.is_player_active = True
if __name__ == '__main__':
window = ApplicationWindow()
window.setup_objects_and_events()
window.show()
Gtk.main()
window.player.stop()
window.vlcInstance.release()
# attatch function to start new video when the current one ends
def end_reached(self):
src = get_src_of_rand_video()
set_my_media(Instance, player, src)
player.play()