Update link titler module for new youtube api
This commit is contained in:
parent
58d5586713
commit
cac50fb23a
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"agent": "pyircbot3 by /u/(changeme)",
|
||||||
|
"youtube_api_key": ""
|
||||||
|
}
|
|
@ -1 +0,0 @@
|
||||||
agent: pyircbot3 by /u/(changeme)
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"api_key": ""
|
||||||
|
}
|
|
@ -90,33 +90,49 @@ class LinkTitler(ModuleBase):
|
||||||
self.bot.act_PRIVMSG(args[0], "%s: \x02%s\x02" % (sender.nick, title))
|
self.bot.act_PRIVMSG(args[0], "%s: \x02%s\x02" % (sender.nick, title))
|
||||||
return
|
return
|
||||||
|
|
||||||
# For youtbue
|
# For youtube
|
||||||
|
def getISOdurationseconds(self, stamp):
|
||||||
|
ISO_8601_period_rx = re.compile(
|
||||||
|
'P' # designates a period
|
||||||
|
'(?:(?P<years>\d+)Y)?' # years
|
||||||
|
'(?:(?P<months>\d+)M)?' # months
|
||||||
|
'(?:(?P<weeks>\d+)W)?' # weeks
|
||||||
|
'(?:(?P<days>\d+)D)?' # days
|
||||||
|
'(?:T' # time part must begin with a T
|
||||||
|
'(?:(?P<hours>\d+)H)?' # hours
|
||||||
|
'(?:(?P<minutes>\d+)M)?' # minutes
|
||||||
|
'(?:(?P<seconds>\d+)S)?' # seconds
|
||||||
|
')?' # end of time part
|
||||||
|
) # http://stackoverflow.com/a/16742742
|
||||||
|
return ISO_8601_period_rx.match(stamp).groupdict()
|
||||||
def get_video_description(self, vid_id):
|
def get_video_description(self, vid_id):
|
||||||
j = get("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc" % vid_id).json()
|
apidata = get('https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=%s&key=%s' % (vid_id, self.config["youtube_api_key"])).json()
|
||||||
if j.get('error'):
|
|
||||||
|
if not apidata['pageInfo']['totalResults']:
|
||||||
return
|
return
|
||||||
j = j['data']
|
|
||||||
out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % j['title']
|
video = apidata['items'][0]
|
||||||
if not j.get('duration'):
|
snippet = video["snippet"]
|
||||||
return out
|
duration = self.getISOdurationseconds(video["contentDetails"]["duration"])
|
||||||
|
|
||||||
|
out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % snippet["title"]
|
||||||
|
|
||||||
out += ' - length \x02'
|
out += ' - length \x02'
|
||||||
length = j['duration']
|
if duration["hours"]!=None:
|
||||||
if length / 3600: # > 1 hour
|
out += '%dh ' % int(duration["hours"])
|
||||||
out += '%dh ' % (length / 3600)
|
if duration["minutes"]!=None:
|
||||||
if length / 60:
|
out += '%dm ' % int(duration["minutes"])
|
||||||
out += '%dm ' % (length / 60 % 60)
|
out += "%ds\x02" % int(duration["seconds"])
|
||||||
out += "%ds\x02" % (length % 60)
|
|
||||||
if 'rating' in j:
|
totalvotes = float(video["statistics"]["dislikeCount"])+float(video["statistics"]["likeCount"])
|
||||||
out += ' - rated \x02%.2f/5.0\x02 (%d)' % (j['rating'],
|
rating = float(video["statistics"]["likeCount"]) / totalvotes
|
||||||
j['ratingCount'])
|
out += ' - rated \x02%.2f/5\x02' % round(rating*5,1)
|
||||||
if 'viewCount' in j:
|
out += ' - \x02%s\x02 views' % self.group_int_digits(video["statistics"]["viewCount"])
|
||||||
out += ' - \x02%s\x02 views' % self.group_int_digits(j['viewCount'])
|
upload_time = time.strptime(snippet['publishedAt'], "%Y-%m-%dT%H:%M:%S.000Z")
|
||||||
upload_time = time.strptime(j['uploaded'], "%Y-%m-%dT%H:%M:%S.000Z")
|
out += ' - by \x02%s\x02 on \x02%s\x02' % (snippet['channelTitle'], time.strftime("%Y.%m.%d", upload_time))
|
||||||
out += ' - \x02%s\x02 on \x02%s\x02' % (
|
|
||||||
j['uploader'], time.strftime("%Y.%m.%d", upload_time))
|
|
||||||
if 'contentRating' in j:
|
|
||||||
out += ' - \x034NSFW\x02'
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def group_int_digits(self, number, delimiter=',', grouping=3):
|
def group_int_digits(self, number, delimiter=',', grouping=3):
|
||||||
base = str(number).strip()
|
base = str(number).strip()
|
||||||
builder = []
|
builder = []
|
||||||
|
|
|
@ -10,12 +10,28 @@
|
||||||
from pyircbot.modulebase import ModuleBase,ModuleHook
|
from pyircbot.modulebase import ModuleBase,ModuleHook
|
||||||
from requests import get
|
from requests import get
|
||||||
import time
|
import time
|
||||||
|
import re
|
||||||
|
|
||||||
class Youtube(ModuleBase):
|
class Youtube(ModuleBase):
|
||||||
def __init__(self, bot, moduleName):
|
def __init__(self, bot, moduleName):
|
||||||
ModuleBase.__init__(self, bot, moduleName);
|
ModuleBase.__init__(self, bot, moduleName);
|
||||||
self.hooks=[ModuleHook("PRIVMSG", self.youtube)]
|
self.hooks=[ModuleHook("PRIVMSG", self.youtube)]
|
||||||
|
|
||||||
|
def getISOdurationseconds(self, stamp):
|
||||||
|
ISO_8601_period_rx = re.compile(
|
||||||
|
'P' # designates a period
|
||||||
|
'(?:(?P<years>\d+)Y)?' # years
|
||||||
|
'(?:(?P<months>\d+)M)?' # months
|
||||||
|
'(?:(?P<weeks>\d+)W)?' # weeks
|
||||||
|
'(?:(?P<days>\d+)D)?' # days
|
||||||
|
'(?:T' # time part must begin with a T
|
||||||
|
'(?:(?P<hours>\d+)H)?' # hours
|
||||||
|
'(?:(?P<minutes>\d+)M)?' # minutes
|
||||||
|
'(?:(?P<seconds>\d+)S)?' # seconds
|
||||||
|
')?' # end of time part
|
||||||
|
) # http://stackoverflow.com/a/16742742
|
||||||
|
return ISO_8601_period_rx.match(stamp).groupdict()
|
||||||
|
|
||||||
def youtube(self, args, prefix, trailing):
|
def youtube(self, args, prefix, trailing):
|
||||||
|
|
||||||
cmd = self.bot.messageHasCommand(".youtube", trailing)
|
cmd = self.bot.messageHasCommand(".youtube", trailing)
|
||||||
|
@ -34,41 +50,33 @@ class Youtube(ModuleBase):
|
||||||
self.bot.act_PRIVMSG(args[0], "http://youtu.be/%s :: %s" % (vid_id, self.get_video_description(vid_id)))
|
self.bot.act_PRIVMSG(args[0], "http://youtu.be/%s :: %s" % (vid_id, self.get_video_description(vid_id)))
|
||||||
|
|
||||||
def get_video_description(self, vid_id):
|
def get_video_description(self, vid_id):
|
||||||
j = get("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc" % vid_id).json()
|
apidata = get('https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=%s&key=%s' % (vid_id, self.config["api_key"])).json()
|
||||||
|
|
||||||
if j.get('error'):
|
if not apidata['pageInfo']['totalResults']:
|
||||||
return
|
return
|
||||||
|
|
||||||
j = j['data']
|
video = apidata['items'][0]
|
||||||
|
snippet = video["snippet"]
|
||||||
out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % j['title']
|
duration = self.getISOdurationseconds(video["contentDetails"]["duration"])
|
||||||
|
|
||||||
if not j.get('duration'):
|
out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % snippet["title"]
|
||||||
return out
|
|
||||||
|
|
||||||
out += ' - length \x02'
|
out += ' - length \x02'
|
||||||
length = j['duration']
|
if duration["hours"]!=None:
|
||||||
if length / 3600: # > 1 hour
|
out += '%dh ' % int(duration["hours"])
|
||||||
out += '%dh ' % (length / 3600)
|
if duration["minutes"]!=None:
|
||||||
if length / 60:
|
out += '%dm ' % int(duration["minutes"])
|
||||||
out += '%dm ' % (length / 60 % 60)
|
out += "%ds\x02" % int(duration["seconds"])
|
||||||
out += "%ds\x02" % (length % 60)
|
|
||||||
|
totalvotes = float(video["statistics"]["dislikeCount"])+float(video["statistics"]["likeCount"])
|
||||||
if 'rating' in j:
|
rating = float(video["statistics"]["likeCount"]) / totalvotes
|
||||||
out += ' - rated \x02%.2f/5.0\x02 (%d)' % (j['rating'],
|
out += ' - rated \x02%.2f/5\x02' % round(rating*5,1)
|
||||||
j['ratingCount'])
|
out += ' - \x02%s\x02 views' % self.group_int_digits(video["statistics"]["viewCount"])
|
||||||
|
upload_time = time.strptime(snippet['publishedAt'], "%Y-%m-%dT%H:%M:%S.000Z")
|
||||||
if 'viewCount' in j:
|
out += ' - by \x02%s\x02 on \x02%s\x02' % (snippet['channelTitle'], time.strftime("%Y.%m.%d", upload_time))
|
||||||
out += ' - \x02%s\x02 views' % self.group_int_digits(j['viewCount'])
|
|
||||||
|
|
||||||
upload_time = time.strptime(j['uploaded'], "%Y-%m-%dT%H:%M:%S.000Z")
|
|
||||||
out += ' - \x02%s\x02 on \x02%s\x02' % (
|
|
||||||
j['uploader'], time.strftime("%Y.%m.%d", upload_time))
|
|
||||||
|
|
||||||
if 'contentRating' in j:
|
|
||||||
out += ' - \x034NSFW\x02'
|
|
||||||
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def group_int_digits(self, number, delimiter=',', grouping=3):
|
def group_int_digits(self, number, delimiter=',', grouping=3):
|
||||||
base = str(number).strip()
|
base = str(number).strip()
|
||||||
builder = []
|
builder = []
|
||||||
|
@ -76,4 +84,4 @@ class Youtube(ModuleBase):
|
||||||
builder.append(base[-grouping:])
|
builder.append(base[-grouping:])
|
||||||
base = base[:-grouping]
|
base = base[:-grouping]
|
||||||
builder.reverse()
|
builder.reverse()
|
||||||
return delimiter.join(builder)
|
return delimiter.join(builder)
|
||||||
|
|
|
@ -104,6 +104,7 @@ class PyIRCBot:
|
||||||
:type moduleName: str"""
|
:type moduleName: str"""
|
||||||
" check if already exists "
|
" check if already exists "
|
||||||
if not name in self.modules:
|
if not name in self.modules:
|
||||||
|
self.log.debug("Importing %s" % name)
|
||||||
" attempt to load "
|
" attempt to load "
|
||||||
try:
|
try:
|
||||||
moduleref = __import__(name)
|
moduleref = __import__(name)
|
||||||
|
@ -123,6 +124,7 @@ class PyIRCBot:
|
||||||
|
|
||||||
:param moduleName: Name of the module to import
|
:param moduleName: Name of the module to import
|
||||||
:type moduleName: str"""
|
:type moduleName: str"""
|
||||||
|
self.log.debug("Deporting %s" % name)
|
||||||
" unload if necessary "
|
" unload if necessary "
|
||||||
if name in self.moduleInstances:
|
if name in self.moduleInstances:
|
||||||
self.unloadmodule(name)
|
self.unloadmodule(name)
|
||||||
|
|
Loading…
Reference in New Issue