2010-02-13 05:13:55 +00:00
|
|
|
import re
|
2010-02-14 01:11:42 +00:00
|
|
|
import time
|
2010-01-17 04:24:36 +00:00
|
|
|
|
2010-04-23 03:47:41 +00:00
|
|
|
from util import hook, http
|
2010-03-01 01:08:54 +00:00
|
|
|
|
2010-01-17 04:24:36 +00:00
|
|
|
|
2010-03-12 00:19:36 +00:00
|
|
|
youtube_re = (r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)'
|
|
|
|
'([-_a-z0-9]+)', re.I)
|
2010-01-17 04:24:36 +00:00
|
|
|
|
2015-05-06 18:34:23 +00:00
|
|
|
base_url = 'https://www.googleapis.com/youtube/v3/'
|
|
|
|
info_url = base_url + 'videos?part=snippet,contentDetails,statistics'
|
|
|
|
search_api_url = base_url + 'search'
|
2012-04-15 18:54:58 +00:00
|
|
|
video_url = 'http://youtube.com/watch?v=%s'
|
2010-03-01 01:08:54 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2015-05-06 18:34:23 +00:00
|
|
|
def get_video_description(vid_id, api_key):
|
|
|
|
j = http.get_json(info_url, id=vid_id, key=api_key)
|
2010-02-14 01:11:42 +00:00
|
|
|
|
2015-05-06 18:34:23 +00:00
|
|
|
if not j['pageInfo']['totalResults']:
|
2010-03-01 02:32:41 +00:00
|
|
|
return
|
2010-02-13 05:13:55 +00:00
|
|
|
|
2015-05-06 18:34:23 +00:00
|
|
|
j = j['items'][0]
|
2010-02-13 05:13:55 +00:00
|
|
|
|
2015-05-06 18:34:23 +00:00
|
|
|
duration = j['contentDetails']['duration'].replace('PT', '').lower()
|
2010-02-13 05:13:55 +00:00
|
|
|
|
2015-05-06 18:34:23 +00:00
|
|
|
published = time.strptime(j['snippet']['publishedAt'],
|
|
|
|
"%Y-%m-%dT%H:%M:%S.000Z")
|
|
|
|
published = time.strftime("%Y.%m.%d", published)
|
2010-12-02 21:25:51 +00:00
|
|
|
|
2015-05-06 18:34:23 +00:00
|
|
|
views = group_int_digits(j['statistics']['viewCount'], ',')
|
2010-02-14 01:11:42 +00:00
|
|
|
|
2015-05-06 18:34:23 +00:00
|
|
|
out = (u'\x02{snippet[title]}\x02 - length \x02{duration}\x02 - '
|
|
|
|
u'{statistics[likeCount]}\u2191{statistics[dislikeCount]}\u2193 - '
|
|
|
|
u'\x02{views}\x02 views - '
|
|
|
|
u'\x02{snippet[channelTitle]}\x02 on \x02{published}\x02'
|
|
|
|
).format(duration=duration, views=views, published=published, **j)
|
2010-02-14 01:11:42 +00:00
|
|
|
|
2015-05-06 18:34:23 +00:00
|
|
|
# TODO: figure out how to detect NSFW videos
|
2010-02-13 05:13:55 +00:00
|
|
|
|
2010-02-28 13:09:56 +00:00
|
|
|
return out
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2015-04-21 17:35:05 +00:00
|
|
|
|
2012-04-15 18:54:58 +00:00
|
|
|
def group_int_digits(number, delimiter=' ', grouping=3):
|
|
|
|
base = str(number).strip()
|
|
|
|
builder = []
|
|
|
|
while base:
|
|
|
|
builder.append(base[-grouping:])
|
|
|
|
base = base[:-grouping]
|
|
|
|
builder.reverse()
|
|
|
|
return delimiter.join(builder)
|
2010-02-14 19:10:21 +00:00
|
|
|
|
2015-04-21 17:35:05 +00:00
|
|
|
|
2015-05-06 18:34:23 +00:00
|
|
|
@hook.api_key('google')
|
2010-03-12 00:19:36 +00:00
|
|
|
@hook.regex(*youtube_re)
|
2015-05-06 18:34:23 +00:00
|
|
|
def youtube_url(match, api_key=None):
|
|
|
|
return get_video_description(match.group(1), api_key)
|
2010-03-01 01:08:54 +00:00
|
|
|
|
2010-03-01 01:39:29 +00:00
|
|
|
|
2015-04-21 17:35:05 +00:00
|
|
|
@hook.api_key('google')
|
2014-01-09 01:29:35 +00:00
|
|
|
@hook.command('yt')
|
2010-03-01 01:08:54 +00:00
|
|
|
@hook.command('y')
|
2010-03-01 01:39:29 +00:00
|
|
|
@hook.command
|
2015-04-21 17:35:05 +00:00
|
|
|
def youtube(inp, api_key=None):
|
2010-03-01 01:08:54 +00:00
|
|
|
'.youtube <query> -- returns the first YouTube search result for <query>'
|
2010-03-05 23:09:36 +00:00
|
|
|
|
2015-04-21 17:35:05 +00:00
|
|
|
params = {
|
|
|
|
'key': api_key,
|
|
|
|
'fields': 'items(id,snippet(channelId,title))',
|
|
|
|
'part': 'snippet',
|
|
|
|
'type': 'video',
|
|
|
|
'q': inp
|
|
|
|
}
|
|
|
|
|
|
|
|
j = http.get_json(search_api_url, **params)
|
2010-03-01 01:39:29 +00:00
|
|
|
|
|
|
|
if 'error' in j:
|
2012-04-15 18:54:58 +00:00
|
|
|
return 'error while performing the search'
|
2010-03-01 01:39:29 +00:00
|
|
|
|
2015-04-21 17:35:05 +00:00
|
|
|
results = j.get("items")
|
|
|
|
|
|
|
|
if not results:
|
2010-03-01 01:39:29 +00:00
|
|
|
return 'no results found'
|
|
|
|
|
2015-04-21 17:35:05 +00:00
|
|
|
vid_id = j['items'][0]['id']['videoId']
|
2010-03-01 01:39:29 +00:00
|
|
|
|
2015-05-06 18:34:23 +00:00
|
|
|
return get_video_description(vid_id, api_key) + " - " + video_url % vid_id
|