Skip to content

Commit 289c456

Browse files
author
Foxlet
committed
youtube.py - v3 API Searching
1 parent f06c36a commit 289c456

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

plugins/youtube.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
base_url = 'https://www.googleapis.com/youtube/v3/'
1616
api_url = base_url + 'videos?part=contentDetails%2C+snippet%2C+statistics&id={}&key={}'
17-
search_api_url = base_url + 'videos?v=2&alt=jsonc&max-results=1'
17+
search_api_url = base_url + 'search?part=id&q={}&maxResults=1&key={}'
1818
video_url = "http://youtu.be/%s"
1919

2020

@@ -63,24 +63,29 @@ def get_video_description(video_id, key):
6363
return out
6464

6565

66+
@hook.onload()
67+
def load_key(bot):
68+
global dev_key
69+
dev_key = bot.config.get("api_keys", {}).get("google_dev_key")
70+
71+
6672
@hook.regex(youtube_re)
6773
def youtube_url(match, bot):
68-
dev_key = bot.config.get("api_keys", {}).get("google_dev_key")
6974
return get_video_description(match.group(1), dev_key)
7075

7176

7277
@hook.command("youtube", "you", "yt", "y")
7378
def youtube(text):
7479
"""youtube <query> -- Returns the first YouTube search result for <query>."""
75-
json = requests.get(search_api_url, params={"q": text}).json()
80+
json = requests.get(search_api_url, params={"q": text, "key": dev_key}).json()
7681

7782
if 'error' in json:
7883
return 'error performing search'
7984

80-
if json['data']['totalItems'] == 0:
85+
if json['pageInfo']['totalResults'] == 0:
8186
return 'no results found'
8287

83-
video_id = json['data']['items'][0]['id']
88+
video_id = json['items'][0]['id']
8489

8590
return get_video_description(video_id) + " - " + video_url % video_id
8691

0 commit comments

Comments
 (0)