-
Notifications
You must be signed in to change notification settings - Fork 5
/
youtube.py
executable file
·320 lines (211 loc) · 6.89 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import random
import logging
from youtube_player.youtube_player import YoutubePlayer
# the player, uses the vlc media player python bindings
import gdata.youtube.service
# http://gdata-python-client.googlecode.com/hg/pydocs/gdata.youtube.service.html#YouTubeVideoQuery
# https://developers.google.com/youtube/1.0/developers_guide_python
from stream import stream
from stream import download
# https://bitbucket.org/rg3/youtube-dl/wiki/Home
import urwid
# http://excess.org/urwid/reference.html
class CustomEdit(urwid.Edit):
__metaclass__ = urwid.signals.MetaSignals
signals = ['done']
def keypress(self, size, key):
if key == 'enter':
urwid.emit_signal(self, 'done', self.get_edit_text())
return
elif key == 'esc':
urwid.emit_signal(self, 'done', None)
return
urwid.Edit.keypress(self, size, key)
class ItemWidget (urwid.WidgetWrap):
def __init__ (self, item, entry=None):
try:
logging.basicConfig(filename='.youtube-cli.log',level=logging.DEBUG)
if item == 'content' and entry:
self.content = entry.media.player.url
title = ('weight', 3, urwid.Padding(urwid.AttrWrap(
urwid.Text('%s' % ( entry.title.text)), 'body', 'focus')))
rating = ('weight', 1, urwid.Padding(urwid.AttrWrap(
urwid.Text('Rating:%s' % ( entry.rating.average)), 'body')))
viewcount = ('weight', 1, urwid.Padding(urwid.AttrWrap(
urwid.Text('Viewcount:%s' % ( entry.statistics.view_count )), 'body')))
item = [title, rating, viewcount]
content = urwid.Columns(item)
self.__super.__init__(content)
elif item == 'header':
title = ('weight', 3, urwid.Padding(urwid.AttrWrap(
urwid.Text('Title'), 'header', 'focus')))
rating = ('weight', 1, urwid.Padding(urwid.AttrWrap(
urwid.Text('Rating'), 'header', 'focus')))
viewcount = ('weight', 1, urwid.Padding(urwid.AttrWrap(
urwid.Text('Viewcount'), 'header', 'focus')))
item = [title, rating, viewcount]
header = urwid.Columns(item)
self.__super.__init__(header)
except Exception as e:
logging.exception(e)
def selectable (self):
return True
def keypress(self, size, key):
return key
class YoutubeClient:
def __init__(self,keyword,q,order,num_results,shuffle=None,time=None):
try:
logging.basicConfig(filename='.youtube-cli.log',level=logging.DEBUG)
keywords = ['search', 'download', 'stream']
if keyword in keywords:
self.keyword = keyword
else:
sys.exit('invalid keyword')
self.q = q
ordering_key = {'rating':'rating', 'viewcount':'viewCount','relevance':'relevance','published':'published'}
ordering = ['rating','viewcount','relevance']
if order in ordering:
self.order = ordering_key[order]
else:
sys.exit('invalid ordering')
try:
self.num = int(num_results)
except Exception as e:
sys.exit('invalid number %s' %e)
if shuffle is not None:
self.shuffle = shuffle
else:
self.shuffle = False
times_key = {'today':'today','week':'this_week','month':'this_month','time':'all_time'}
times = ['today', 'this_week', 'this_month', 'all_time']
if time is not None and time in times:
self.time = times_key[time]
else:
self.time = False
except Exception as e:
logging.exception(e)
self.execute()
def search(self,feed):
try:
self.palette = [
('body','dark cyan', '', 'standout'),
('focus'
,'dark red', '', 'standout'),
('header','dark red', '', 'standout'),
]
self.videolist = []
self.videolist.append(ItemWidget('header'))
for entry in feed.entry:
self.videolist.append(ItemWidget('content', entry))
self.listbox = urwid.ListBox(urwid.SimpleListWalker(self.videolist))
self.view = urwid.Frame(urwid.AttrWrap(self.listbox, 'body'))
self.loop = urwid.MainLoop(self.view, self.palette, unhandled_input=self.keystroke)
self.loop.run()
except Exception as e:
logging.exception(e)
def download(self, entry):
try:
download.main(entry)
except Exception as e:
logging.exception(e)
def downloadFeed(self,feed):
try:
for entry in feed.entry:
download.main(entry.media.player.url)
except Exception as e:
logging.exception(e)
def stream(self, entry):
urls = []
try:
stream.main(entry)
except stream.ReturnUrl as url:
urls.append(str(url))
except Exception as e:
logging.exception(e)
player = YoutubePlayer(urls)
def streamFeed(self,feed):
urls = []
try:
for entry in feed.entry:
stream.main(entry.media.player.url)
except stream.ReturnUrl as url:
urls.append(str(url))
except Exception as e:
logging.exception(e)
player = YoutubePlayer(urls)
def execute(self):
try:
self.client = gdata.youtube.service.YouTubeService()
query = gdata.youtube.service.YouTubeVideoQuery()
query.format = '5'
query.hd = True
query.vq = self.q
query.max_results = self.num
query.start_index = 1
query.racy = 'exclude'
query.orderby = self.order
if self.time:
query.time = self.time
feed = self.client.YouTubeQuery(query)
if self.shuffle:
random.shuffle(feed.entry)
command = self.keyword
if command == 'download':
self.downloadFeed(feed)
if command == 'stream':
self.streamFeed(feed)
if command == 'search':
self.search(feed)
except Exception as e:
logging.exception(e)
def keystroke (self,input):
if input in ('e', 'E'):
try:
self.edit()
except Exception as e:
logging.exception(e)
if input is 'enter':
try:
self.focus = self.listbox.get_focus()[0].content
self.stream(self.focus)
except Exception as e:
logging.exception(e)
if input is ' ':
try:
self.focus = self.listbox.get_focus()[0].content
self.download(self.focus)
except Exception as e:
logging.exception(e)
if input in ('q', 'Q'):
raise urwid.ExitMainLoop()
def edit(self):
try:
self.foot = CustomEdit('search: ')
self.footer = urwid.AttrWrap(self.foot, 'header', 'focus')
self.view.set_footer(self.footer)
self.view.set_focus('footer')
urwid.connect_signal(self.foot, 'done', self.edit_done)
except Exception as e:
logging.exception(e)
def edit_done(self, content):
try:
self.view.set_focus('body')
urwid.disconnect_signal(self, self.foot, 'done', self.edit_done)
if content:
self.q = '\"'+content+'\"'
self.num = 50
self.order = 'relevance'
self.execute()
self.view.set_footer(None)
except Exception as e:
logging.exception(e)
if len(sys.argv) == 7:
YoutubeClient(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6])
if len(sys.argv) == 6:
if sys.argv[5] == 'shuffle': YoutubeClient(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], None)
else: YoutubeClient(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], None, sys.argv[5])
if len(sys.argv) == 5:
YoutubeClient(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])