Skip to content

Commit 34c0f45

Browse files
author
Foxlet
committed
youtube.py - Basic Support for v3 API
1 parent 1ab3459 commit 34c0f45

1 file changed

Lines changed: 26 additions & 25 deletions

File tree

plugins/youtube.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
import time
3+
import isodate
34

45
import bs4
56
import requests
@@ -11,61 +12,61 @@
1112

1213
youtube_re = re.compile(r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)([-_a-zA-Z0-9]+)', re.I)
1314

14-
base_url = 'http://gdata.youtube.com/feeds/api/'
15-
api_url = base_url + 'videos/{}?v=2&alt=jsonc'
15+
base_url = 'https://www.googleapis.com/youtube/v3/'
16+
api_url = base_url + 'videos?part=contentDetails%2C+snippet%2C+statistics&id={}&key={}'
1617
search_api_url = base_url + 'videos?v=2&alt=jsonc&max-results=1'
1718
video_url = "http://youtu.be/%s"
1819

1920

20-
def get_video_description(video_id):
21-
json = requests.get(api_url.format(video_id)).json()
21+
def get_video_description(video_id, key):
22+
json = requests.get(api_url.format(video_id, key)).json()
2223

2324
if json.get('error'):
2425
return
2526

26-
data = json['data']
27+
data = json['items']
28+
snippet = data[0]['snippet']
29+
statistics = data[0]['statistics']
30+
content_details = data[0]['contentDetails']
2731

28-
out = '\x02{}\x02'.format(data['title'])
32+
out = '\x02{}\x02'.format(snippet['title'])
2933

30-
if not data.get('duration'):
34+
if not content_details.get('duration'):
3135
return out
3236

33-
length = data['duration']
34-
out += ' - length \x02{}\x02'.format(timeformat.format_time(length, simple=True))
37+
length = isodate.parse_duration(content_details['duration'])
38+
out += ' - length \x02{}\x02'.format(timeformat.format_time(int(length.total_seconds()), simple=True))
3539

36-
if 'ratingCount' in data:
40+
if 'likeCount' in statistics:
3741
# format
38-
likes = pluralize(int(data['likeCount']), "like")
39-
dislikes = pluralize(data['ratingCount'] - int(data['likeCount']), "dislike")
42+
likes = pluralize(int(statistics['likeCount']), "like")
43+
dislikes = pluralize(int(statistics['dislikeCount']), "dislike")
44+
totalvotes = float(statistics['likeCount']) + float(statistics['dislikeCount'])
4045

41-
percent = 100 * float(data['likeCount']) / float(data['ratingCount'])
46+
percent = 100 * float(statistics['likeCount']) / totalvotes
4247
out += ' - {}, {} (\x02{:.1f}\x02%)'.format(likes,
4348
dislikes, percent)
4449

45-
if 'viewCount' in data:
46-
views = data['viewCount']
50+
if 'viewCount' in statistics:
51+
views = int(statistics['viewCount'])
4752
out += ' - \x02{:,}\x02 view{}'.format(views, "s"[views == 1:])
4853

49-
try:
50-
json = requests.get(base_url + "users/{}?alt=json".format(data["uploader"])).json()
51-
uploader = json["entry"]["author"][0]["name"][
52-
"$t"]
53-
except:
54-
uploader = data["uploader"]
54+
uploader = snippet['channelTitle']
5555

56-
upload_time = time.strptime(data['uploaded'], "%Y-%m-%dT%H:%M:%S.000Z")
56+
upload_time = time.strptime(snippet['publishedAt'], "%Y-%m-%dT%H:%M:%S.000Z")
5757
out += ' - \x02{}\x02 on \x02{}\x02'.format(uploader,
5858
time.strftime("%Y.%m.%d", upload_time))
5959

60-
if 'contentRating' in data:
60+
if 'contentRating' in content_details:
6161
out += ' - \x034NSFW\x02'
6262

6363
return out
6464

6565

6666
@hook.regex(youtube_re)
67-
def youtube_url(match):
68-
return get_video_description(match.group(1))
67+
def youtube_url(match, bot):
68+
dev_key = bot.config.get("api_keys", {}).get("google_dev_key")
69+
return get_video_description(match.group(1), dev_key)
6970

7071

7172
@hook.command("youtube", "you", "yt", "y")

0 commit comments

Comments
 (0)