-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathyoutuber.py
More file actions
executable file
·73 lines (62 loc) · 1.8 KB
/
youtuber.py
File metadata and controls
executable file
·73 lines (62 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
import os
import sys
import json
import requests
from googleapiclient.discovery import build
from oauth2client.tools import argparser
# DEAD SIMPLE PLUGIN
# call it with
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
config = \
{
"name": "youtuber",
"triggers": ["/youtube", "/yt"],
"description": "Searches youtube for keywords"
}
def print_help():
print "usage: /yt keyword search"
def fetch_key():
conf_file = os.path.join(os.path.dirname(__file__), "_youtuber.json")
with open(conf_file, "r") as f:
try:
conf = json.load(f)
except Exception as e:
print "Could not read configuration file: %s" % e
sys.exit(2)
try:
api_key = conf["api_key"]
return api_key
except Exception as e:
print "The config file is dead simple: {\"api_key\": \"XXXXX\"}"
sys.exit(2)
def search(args):
api_key = fetch_key()
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=api_key)
search_response = youtube.search().list(
q="+".join(args),
part="id,snippet",
maxResults=10
).execute()
results = search_response.get("items", [])
for result in results:
vid = result["id"]
if vid["kind"] == "youtube#video":
print "https://youtu.be/" + vid["videoId"]
sys.exit(0)
else:
print "No results found for query \"%s\"" % " ".join(args)
sys.exit(0)
def main():
try:
if sys.argv[1] == "register":
print json.dumps(config)
elif sys.argv[1] == "help":
print_help
else:
search(sys.argv[1:])
except IndexError as e:
print_help()
if __name__ == "__main__":
main()