/
bottube.py
73 lines (66 loc) · 2.89 KB
/
bottube.py
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
from gr3ybot_settings import *
from apiclient.discovery import build
import urllib
import urllib2
from pytz import timezone
import pytz
from time import strftime, sleep, localtime
import time
import datetime
import json
import sys
if YOUTUBE_LINKS == False:
sys.exit()
if __name__ == "__main__":
print "You can't run this on its own!"
sys.exit()
#-- Logging...
timeformat = "%m/%d/%y %H:%M:%S"
def log(text):
localnow = datetime.datetime.now(timezone(LOCALTZ))
with open(LOGFILE, 'a+') as f:
f.write("{0} --==-- {1}\r\n".format(strftime(timeformat),text))
f.close()
#--
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,developerKey=YOUTUBE_DEVELOPER_KEY)
# Call the search.list method to retrieve results matching the specified
# query term.
def getVideo(term):
results = []
search_response = youtube.search().list(
q=term,
part="id,snippet"
).execute()
# Add each result to the appropriate list, and then display the lists of
# matching videos, channels, and playlists.
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
if LOGLEVEL >= 1: log("Found a YouTube video matching ID: {0} (URL: https://www.youtube.com/watch?v={0})".format(term))
results.append("{0}".format(search_result["snippet"]["title"].encode('utf-8')))
break
url='https://www.googleapis.com/youtube/v3/videos?id={0}&part=contentDetails&key={1}'.format(term,YOUTUBE_DEVELOPER_KEY)
results[0] = "".join(a for a in results[0] if (a.isalnum() or a in (",",".","'","\"","?","!","@","#","$","%","^","&","*","(",")","_","+","=","-","\\","|","]","}","{","[",";",":","/",">","<","`","~"," ")))
lists = urllib2.urlopen(url, None)
page = json.loads(lists.read())
duration = page["items"][0]
duration = duration["contentDetails"]["duration"]
duration = duration.replace('PT','')
hoursnominutes = False
# Here's where we do some formatting magic to get the length...
if 'H' in duration and 'M' not in duration: hoursnominutes = True
if 'H' not in duration: duration = "00:{0}".format(duration)
else: duration = duration.replace('H',':')
if 'M' not in duration and not hoursnominutes: duration = "00:{0}".format(duration)
elif 'M' not in duration and hoursnominutes: duration = duration.replace(":",":00:")
else: duration = duration.replace('M',':')
if 'S' not in duration: duration = "{0}00".format(duration)
else: duration = duration.replace('S','')
duration = duration.split(':')
if int(duration[0]) < 10 and duration[0] != '00': duration[0] = "0{0}".format(duration[0])
if int(duration[1]) < 10 and duration[1] != '00': duration[1] = "0{0}".format(duration[1])
if int(duration[2]) < 10 and duration[2] != '00': duration[2] = "0{0}".format(duration[2])
duration = ":".join(duration)
results.append(duration)
if LOGLEVEL >= 1: log("Video Info --==-- Title: {0}, Length: {1}".format(results[0],results[1]))
return results