This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/youtube.py

186 lines
5.7 KiB
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
import re
import time
from util import hook, http, text
2011-11-20 10:23:31 +01:00
youtube_re = (r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)'
'([-_a-zA-Z0-9]+)', re.I)
2011-11-20 10:23:31 +01:00
base_url = 'http://gdata.youtube.com/feeds/api/'
2012-11-12 11:46:38 +01:00
api_url = base_url + 'videos/{}?v=2&alt=jsonc'
2011-11-20 10:23:31 +01:00
search_api_url = base_url + 'videos?v=2&alt=jsonc&max-results=1'
2012-08-20 22:34:24 +02:00
video_url = "http://youtu.be/%s"
2011-11-20 10:23:31 +01:00
2013-11-29 04:35:41 +01:00
def plural(num=0, text=''):
2013-11-30 07:19:04 +01:00
return "{:,} {}{}".format(num, text, "s"[num==1:])
2013-11-29 04:35:41 +01:00
2013-11-30 12:56:07 +01:00
def format_time(seconds, count=3, simple=False):
if simple:
periods = [
2013-11-30 12:49:09 +01:00
('c', 60 * 60 * 24 * 365 * 100),
('de', 60 * 60 * 24 * 365 * 10),
('y', 60 * 60 * 24 * 365),
('m', 60 * 60 * 24 * 30),
('d', 60 * 60 * 24),
('h', 60 * 60),
2013-11-30 12:39:40 +01:00
('m', 60),
('s', 1)
]
else:
periods = [
2013-11-30 12:49:09 +01:00
(('century', 'centuries'), 60 * 60 * 24 * 365 * 100),
(('decade', 'decades'), 60 * 60 * 24 * 365 * 10),
(('year', 'years'), 60 * 60 * 24 * 365),
(('month', 'months'), 60 * 60 * 24 * 30),
(('day', 'days'), 60 * 60 * 24),
(('hour', 'hours'), 60 * 60),
(('minute', 'minutes'), 60),
(('second', 'seconds'), 1)
]
strings = []
i = 0
for period_name, period_seconds in periods:
2013-11-30 12:56:07 +01:00
if i < count:
if seconds > period_seconds:
period_value, seconds = divmod(seconds,period_seconds)
i += 1
2013-11-30 12:49:09 +01:00
if simple:
strings.append("{}{}".format(period_value, period_name))
else:
2013-11-30 12:49:09 +01:00
if period_value == 1:
strings.append("{} {}".format(period_value, period_name[0]))
else:
strings.append("{} {}".format(period_value, period_name[1]))
else:
break
if simple:
return " ".join(strings)
else:
return text.get_text_list(strings, "and")
2012-11-12 11:46:38 +01:00
def get_video_description(video_id):
request = http.get_json(api_url.format(video_id))
2011-11-20 10:23:31 +01:00
2012-08-27 21:08:09 +02:00
if request.get('error'):
2011-11-20 10:23:31 +01:00
return
2012-08-27 21:08:09 +02:00
data = request['data']
2013-11-30 07:19:04 +01:00
out = '\x02{}\x02'.format(data['title'])
2011-11-20 10:23:31 +01:00
2012-08-27 21:08:09 +02:00
if not data.get('duration'):
2011-11-20 10:23:31 +01:00
return out
2012-08-27 21:08:09 +02:00
length = data['duration']
out += ' - length \x02{}\x02'.format(format_time(length, simple=True))
2011-11-20 10:23:31 +01:00
2013-11-29 04:30:06 +01:00
if 'ratingCount' in data:
2013-11-30 07:19:04 +01:00
# format
2013-11-29 04:35:41 +01:00
likes = plural(int(data['likeCount']), "like")
dislikes = plural(data['ratingCount'] - int(data['likeCount']), "dislike")
2013-11-29 04:30:06 +01:00
2013-11-29 04:35:41 +01:00
percent = 100 * float(data['likeCount'])/float(data['ratingCount'])
out += ' - {}, {} (\x02{:.1f}\x02%)'.format(likes,
2013-11-30 07:19:04 +01:00
dislikes, percent)
2012-03-04 01:47:14 +01:00
2012-08-30 12:30:29 +02:00
if 'viewCount' in data:
2013-11-30 07:19:04 +01:00
views = data['viewCount']
out += ' - \x02{:,}\x02 view{}'.format(views, "s"[views==1:])
2011-11-20 10:23:31 +01:00
try:
uploader = http.get_json(base_url + "users/{}?alt=json".format(data["uploader"]))["entry"]["author"][0]["name"]["$t"]
except:
uploader = data["uploader"]
2013-11-30 07:19:04 +01:00
2012-08-27 21:08:09 +02:00
upload_time = time.strptime(data['uploaded'], "%Y-%m-%dT%H:%M:%S.000Z")
2013-11-30 07:19:04 +01:00
out += ' - \x02{}\x02 on \x02{}\x02'.format(uploader,
time.strftime("%Y.%m.%d", upload_time))
2011-11-20 10:23:31 +01:00
2012-08-27 21:08:09 +02:00
if 'contentRating' in data:
out += ' - \x034NSFW\x02'
2011-11-20 10:23:31 +01:00
return out
2011-11-20 10:23:31 +01:00
2012-02-29 06:58:38 +01:00
2011-11-20 10:23:31 +01:00
@hook.regex(*youtube_re)
def youtube_url(match):
return get_video_description(match.group(1))
2012-02-29 06:58:38 +01:00
2012-02-15 08:06:52 +01:00
@hook.command('yt')
2011-11-20 10:23:31 +01:00
@hook.command('y')
@hook.command
def youtube(inp):
2013-09-04 12:30:04 +02:00
"""youtube <query> -- Returns the first YouTube search result for <query>."""
2011-11-20 10:23:31 +01:00
2012-08-27 21:08:09 +02:00
request = http.get_json(search_api_url, q=inp)
2011-11-20 10:23:31 +01:00
2012-08-27 21:08:09 +02:00
if 'error' in request:
2011-11-20 10:23:31 +01:00
return 'error performing search'
2012-08-27 21:08:09 +02:00
if request['data']['totalItems'] == 0:
2011-11-20 10:23:31 +01:00
return 'no results found'
2012-08-27 21:08:09 +02:00
video_id = request['data']['items'][0]['id']
2011-11-20 10:23:31 +01:00
2012-08-27 21:08:09 +02:00
return get_video_description(video_id) + " - " + video_url % video_id
2013-06-27 12:42:18 +02:00
2013-09-04 12:30:04 +02:00
@hook.command('ytime')
@hook.command
def youtime(inp):
"""youtime <query> -- Gets the total run time of the first YouTube search result for <query>."""
request = http.get_json(search_api_url, q=inp)
if 'error' in request:
return 'error performing search'
if request['data']['totalItems'] == 0:
return 'no results found'
video_id = request['data']['items'][0]['id']
request = http.get_json(api_url.format(video_id))
if request.get('error'):
return
data = request['data']
if not data.get('duration'):
return
length = data['duration']
views = data['viewCount']
total = int(length * views)
length_text = format_time(length, simple=True)
total_text = format_time(total)
return u'The video \x02{}\x02 has a length of {} and has been viewed {:,} times for ' \
'a total run time of {}!'.format(data['title'], length_text, views, \
total_text)
2013-06-27 12:42:18 +02:00
ytpl_re = (r'(.*:)//(www.youtube.com/playlist|youtube.com/playlist)(:[0-9]+)?(.*)', re.I)
2013-09-04 12:30:04 +02:00
2013-06-27 12:42:18 +02:00
@hook.regex(*ytpl_re)
def ytplaylist_url(match):
location = match.group(4).split("=")[-1]
try:
2013-09-04 12:30:04 +02:00
soup = http.get_soup("https://www.youtube.com/playlist?list=" + location)
2013-06-27 12:42:18 +02:00
except Exception:
2013-09-04 12:30:04 +02:00
return "\x034\x02Invalid response."
2013-06-27 12:42:18 +02:00
title = soup.find('title').text.split('-')[0].strip()
author = soup.find('img', {'class': 'channel-header-profile-image'})['title']
numvideos = soup.find('ul', {'class': 'header-stats'}).findAll('li')[0].text.split(' ')[0]
views = soup.find('ul', {'class': 'header-stats'}).findAll('li')[1].text.split(' ')[0]
return u"\x02%s\x02 - \x02%s\x02 views - \x02%s\x02 videos - \x02%s\x02" % (title, views, numvideos, author)