-
Notifications
You must be signed in to change notification settings - Fork 0
/
ytcrawler.py
322 lines (227 loc) · 8.77 KB
/
ytcrawler.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
321
#!/usr/bin/env python
from pafy import Pafy
import sys
from urllib import urlencode
import collections
import logging
from urllib2 import build_opener, HTTPError, URLError
import time
from json import load
from urllib2 import urlopen
import re
import math
from urllib import urlencode
uni, byt, xinput = unicode, str, raw_input
uni = unicode
utf8_encode = lambda x: x.encode("utf8") if type(x) == uni else x
utf8_decode = lambda x: x.decode("utf8") if type(x) == byt else x
def fmt_time(seconds):
""" Format number of seconds to %H:%M:%S. """
hms = time.strftime('%H:%M:%S', time.gmtime(int(seconds)))
H, M, S = hms.split(":")
if H == "00":
hms = M + ":" + S
elif H == "01" and int(M) < 40:
hms = uni(int(M) + 60) + ":" + S
elif H.startswith("0"):
hms = ":".join([H[1], M, S])
return hms
class Playlist(object):
""" Representation of a playist, has list of songs. """
def __init__(self, name=None, songs=None):
self.name = name
self.creation = time.time()
self.songs = songs or []
@property
def is_empty(self):
""" Return True / False if songs are populated or not. """
return bool(not self.songs)
@property
def size(self):
""" Return number of tracks. """
return len(self.songs)
@property
def duration(self):
""" Sum duration of the playlist. """
duration = sum(s.length for s in self.songs)
duration = time.strftime('%H:%M:%S', time.gmtime(int(duration)))
return duration
def yt_datetime(yt_date_time):
""" Return a time object and locale formated date string. """
time_obj = time.strptime(yt_date_time, "%Y-%m-%dT%H:%M:%S.000Z")
locale_date = time.strftime("%x", time_obj)
# strip first two digits of four digit year
short_date = re.sub(r"(\d\d\D\d\d\D)20(\d\d)$", r"\1\2", locale_date)
return time_obj, short_date
def num_repr(num):
""" Return up to four digit string representation of a number, eg 2.6m. """
if num <= 9999:
return uni(num)
digit_count = lambda x: int(math.floor(math.log10(x)) + 1)
digits = digit_count(num)
sig = 3 if digits % 3 == 0 else 2
rounded = int(round(num, int(sig - digits)))
digits = digit_count(rounded)
suffix = "_kmBTqXYX"[(digits - 1) // 3]
front = 3 if digits % 3 == 0 else digits % 3
if not front == 1:
return uni(rounded)[0:front] + suffix
return uni(rounded)[0] + "." + uni(rounded)[1] + suffix
class ConfigItem(object):
""" A configuration item. """
def __init__(self, name, value, minval=None, maxval=None, check_fn=None):
""" If specified, the check_fn should return a dict.
{valid: bool, message: success/fail mesage, value: value to set}
"""
self.default = self.value = value
self.name = name
self.type = type(value)
self.maxval, self.minval = maxval, minval
self.check_fn = check_fn
self.require_known_player = False
self.allowed_values = []
@property
def get(self):
""" Return value. """
return self.value
@property
def display(self):
""" Return value in a format suitable for display. """
retval = self.value
if self.name == "max_res":
retval = uni(retval) + "p"
return retval
def set(self, value):
""" Set value with checks. """
# note: fail_msg should contain %s %s for self.name, value
# success_msg should not
# pylint: disable=R0912
# too many branches
success_msg = fail_msg = ""
value = value.strip()
value_orig = value
green = lambda x: "%s%s%s" % (c.g, x, c.w)
# handle known player not set
if self.allowed_values and not value in self.allowed_values:
fail_msg = "%s must be one of * - not %s"
fail_msg = fail_msg.replace("*", ", ".join(self.allowed_values))
if self.require_known_player and not known_player_set():
fail_msg = "%s requires mpv or mplayer, can't set to %s"
# handle true / false values
elif self.type == bool:
if value.upper() in "0 OFF NO DISABLED FALSE".split():
value = False
success_msg = "%s set to False" % green(self.name)
elif value.upper() in "1 ON YES ENABLED TRUE".split():
value = True
success_msg = "%s set to True" % green(self.name)
else:
fail_msg = "%s requires True/False, got %s"
# handle int values
elif self.type == int:
if not value.isdigit():
fail_msg = "%s requires a number, got %s"
else:
value = int(value)
if self.maxval and self.minval:
if not self.minval <= value <= self.maxval:
m = " must be between %s and %s, got "
m = m % (self.minval, self.maxval)
fail_msg = "%s" + m + "%s"
if not fail_msg:
dispval = value or "None"
success_msg = "%s set to %s" % (green(self.name), dispval)
# handle space separated list
elif self.type == list:
success_msg = "%s set to %s" % (green(self.name), value)
value = value.split()
# handle string values
elif self.type == str:
dispval = value or "None"
success_msg = "%s set to %s" % (green(self.name), green(dispval))
# handle failure
if fail_msg:
failed_val = value_orig.strip() or "<nothing>"
colvals = c.y + self.name + c.w, c.y + failed_val + c.w
return fail_msg % colvals
elif self.check_fn:
checked = self.check_fn(value)
value = checked.get("value") or value
if checked['valid']:
value = checked.get("value", value)
self.value = value
saveconfig()
return checked.get("message", success_msg)
else:
return checked.get('message', fail_msg)
elif success_msg:
self.value = value
saveconfig()
return success_msg
class Config(object):
""" Holds various configuration values. """
ORDER = ConfigItem("order", "relevance")
ORDER.allowed_values = "relevance date views rating".split()
MAX_RESULTS = ConfigItem("max_results", 19, maxval=50, minval=1)
def generate_search_qs(term, page):
""" Return query string. """
aliases = dict(relevance="relevance", date="published", rating="rating",
views="viewCount")
term = utf8_encode(term)
qs = {
'q': term,
'v': 2,
'alt': 'jsonc',
'start-index': ((page - 1) * Config.MAX_RESULTS.get + 1) or 1,
'safeSearch': "none",
#'max-results': Config.MAX_RESULTS.get,
'paid-content': "false",
'orderby': aliases[Config.ORDER.get]
}
return qs
def get_tracks_from_json(jsons):
""" Get search results from web page. """
try:
items = jsons['data']['items']
except KeyError:
items = []
songs = []
item = items[0]
url = "https://www.youtube.com/watch?v="+item['id']
video = Pafy(url)
print "-------------------------"
print "Title: %s" % item['title']
print "Uploader: %s" % item['uploader']
print "Category: %s" % item['category']
print "Duration: %s" % fmt_time(item['duration'])
print "Description: %s" % video.description
print "Keywords:"
for keyword in video.keywords:
print keyword
print "-------------------------"
if not items:
dbg("got unexpected data or no search results")
return False
return songs
def _search(url, progtext, qs=None, splash=True, pre_load=False):
url = url + "?" + urlencode(qs) if qs else url
try:
response = urlopen(url)
json_obj = load(response)
songs = get_tracks_from_json(json_obj)
except (URLError, HTTPError) as e:
print "error"
return True
def search(term, page=1, splash=True):
#term = "Carly Rae Jepsen - OFF SESSION - Tug Of War"
original_term = term
logging.info("search for %s", original_term)
url = "https://gdata.youtube.com/feeds/api/videos"
query = generate_search_qs(term, page)
_search(url, original_term, query)
if __name__ == "__main__":
args = sys.argv[1:]
if not args:
print >> sys.stderr, 'SYNTAX: test.py [video title]'
sys.exit(-1)
search(' '.join(args), 1, True)