-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathyoutube-mini.py
140 lines (120 loc) · 4.6 KB
/
youtube-mini.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf8 -*-
"""
youtube.py - Willie YouTube Module
Copyright 2012, Dimitri Molenaars, Tyrope.nl.
Copyright © 2012-2013, Elad Alfassa, <[email protected]>
Copyright 2012, Edward Powell, embolalia.net
Licensed under the Eiffel Forum License 2.
http://willie.dfbta.net
This module will respond to .yt and .youtube commands and searches the youtubes.
"""
from willie import web, tools
from willie.module import rule, commands, example
import json
import re
from HTMLParser import HTMLParser
def setup(bot):
regex = re.compile('(youtube.com/watch\S*v=|youtu.be/)([\w-]+)')
if not bot.memory.contains('url_callbacks'):
bot.memory['url_callbacks'] = tools.WillieMemory()
bot.memory['url_callbacks'][regex] = ytinfo
def ytget(bot, trigger, uri):
try:
bytes = web.get(uri)
result = json.loads(bytes)
if 'feed' in result:
video_entry = result['feed']['entry'][0]
else:
video_entry = result['entry']
except:
bot.say('Something went wrong when accessing the YouTube API.')
return 'err'
vid_info = {}
try:
# The ID format is tag:youtube.com,2008:video:RYlCVwxoL_g
# So we need to split by : and take the last item
vid_id = video_entry['id']['$t'].split(':')
vid_id = vid_id[len(vid_id) - 1] # last item is the actual ID
vid_info['link'] = 'http://youtu.be/' + vid_id
except KeyError:
vid_info['link'] = 'N/A'
try:
vid_info['title'] = video_entry['title']['$t']
except KeyError:
vid_info['title'] = 'N/A'
#get youtube channel
try:
vid_info['uploader'] = video_entry['author'][0]['name']['$t']
except KeyError:
vid_info['uploader'] = 'N/A'
#get upload time in format: yyyy-MM-ddThh:mm:ss.sssZ
try:
upraw = video_entry['published']['$t']
#parse from current format to output format: DD/MM/yyyy, hh:mm
vid_info['uploaded'] = '%s/%s/%s, %s:%s' % (upraw[8:10], upraw[5:7],
upraw[0:4], upraw[11:13],
upraw[14:16])
except KeyError:
vid_info['uploaded'] = 'N/A'
#get duration in seconds
try:
duration = int(video_entry['media$group']['yt$duration']['seconds'])
#Detect liveshow + parse duration into proper time format.
if duration < 1:
vid_info['length'] = 'LIVE'
else:
hours = duration / (60 * 60)
minutes = duration / 60 - (hours * 60)
seconds = duration % 60
vid_info['length'] = ''
if hours:
vid_info['length'] = str(hours) + 'hours'
if minutes or seconds:
vid_info['length'] = vid_info['length'] + ' '
if minutes:
vid_info['length'] = vid_info['length'] + str(minutes) + 'mins'
if seconds:
vid_info['length'] = vid_info['length'] + ' '
if seconds:
vid_info['length'] = vid_info['length'] + str(seconds) + 'secs'
except KeyError:
vid_info['length'] = 'N/A'
#get views
try:
views = video_entry['yt$statistics']['viewCount']
vid_info['views'] = str('{0:20,d}'.format(int(views))).lstrip(' ')
except KeyError:
vid_info['views'] = 'N/A'
#get comment count
try:
comments = video_entry['gd$comments']['gd$feedLink']['countHint']
vid_info['comments'] = str('{0:20,d}'.format(int(comments))).lstrip(' ')
except KeyError:
vid_info['comments'] = 'N/A'
#get likes & dislikes
try:
likes = video_entry['yt$rating']['numLikes']
vid_info['likes'] = str('{0:20,d}'.format(int(likes))).lstrip(' ')
except KeyError:
vid_info['likes'] = 'N/A'
try:
dislikes = video_entry['yt$rating']['numDislikes']
vid_info['dislikes'] = str('{0:20,d}'.format(int(dislikes))).lstrip(' ')
except KeyError:
vid_info['dislikes'] = 'N/A'
return vid_info
@rule('.*(youtube.com/watch\S*v=|youtu.be/)([\w-]+).*')
def ytinfo(bot, trigger, found_match=None):
"""
Get information about the latest video uploaded by the channel provided.
"""
match = found_match or trigger
#Grab info from YT API
uri = 'http://gdata.youtube.com/feeds/api/videos/' + match.group(2) + '?v=2&alt=json'
video_info = ytget(bot, trigger, uri)
if video_info is 'err':
return
#combine variables and print
message = '[YouTube] ' + video_info['title'] + \
' - ' + video_info['length']
bot.say(HTMLParser().unescape(message))