|
1 | 1 | import re |
2 | 2 | import time |
| 3 | +import isodate |
3 | 4 |
|
4 | 5 | import bs4 |
5 | 6 | import requests |
|
11 | 12 |
|
12 | 13 | youtube_re = re.compile(r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)([-_a-zA-Z0-9]+)', re.I) |
13 | 14 |
|
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={}' |
16 | 17 | search_api_url = base_url + 'videos?v=2&alt=jsonc&max-results=1' |
17 | 18 | video_url = "http://youtu.be/%s" |
18 | 19 |
|
19 | 20 |
|
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() |
22 | 23 |
|
23 | 24 | if json.get('error'): |
24 | 25 | return |
25 | 26 |
|
26 | | - data = json['data'] |
| 27 | + data = json['items'] |
| 28 | + snippet = data[0]['snippet'] |
| 29 | + statistics = data[0]['statistics'] |
| 30 | + content_details = data[0]['contentDetails'] |
27 | 31 |
|
28 | | - out = '\x02{}\x02'.format(data['title']) |
| 32 | + out = '\x02{}\x02'.format(snippet['title']) |
29 | 33 |
|
30 | | - if not data.get('duration'): |
| 34 | + if not content_details.get('duration'): |
31 | 35 | return out |
32 | 36 |
|
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)) |
35 | 39 |
|
36 | | - if 'ratingCount' in data: |
| 40 | + if 'likeCount' in statistics: |
37 | 41 | # 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']) |
40 | 45 |
|
41 | | - percent = 100 * float(data['likeCount']) / float(data['ratingCount']) |
| 46 | + percent = 100 * float(statistics['likeCount']) / totalvotes |
42 | 47 | out += ' - {}, {} (\x02{:.1f}\x02%)'.format(likes, |
43 | 48 | dislikes, percent) |
44 | 49 |
|
45 | | - if 'viewCount' in data: |
46 | | - views = data['viewCount'] |
| 50 | + if 'viewCount' in statistics: |
| 51 | + views = int(statistics['viewCount']) |
47 | 52 | out += ' - \x02{:,}\x02 view{}'.format(views, "s"[views == 1:]) |
48 | 53 |
|
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'] |
55 | 55 |
|
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") |
57 | 57 | out += ' - \x02{}\x02 on \x02{}\x02'.format(uploader, |
58 | 58 | time.strftime("%Y.%m.%d", upload_time)) |
59 | 59 |
|
60 | | - if 'contentRating' in data: |
| 60 | + if 'contentRating' in content_details: |
61 | 61 | out += ' - \x034NSFW\x02' |
62 | 62 |
|
63 | 63 | return out |
64 | 64 |
|
65 | 65 |
|
66 | 66 | @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) |
69 | 70 |
|
70 | 71 |
|
71 | 72 | @hook.command("youtube", "you", "yt", "y") |
|
0 commit comments