Update link titler module for new youtube api

dave/xdcc
dpedu 7 years ago
parent 58d5586713
commit cac50fb23a
  1. 4
      examples/data/config/LinkTitler.json
  2. 1
      examples/data/config/LinkTitler.yml
  3. 3
      examples/data/config/Youtube.json
  4. 62
      pyircbot/modules/LinkTitler.py
  5. 74
      pyircbot/modules/Youtube.py
  6. 2
      pyircbot/pyircbot.py

@ -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))
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):
j = get("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc" % vid_id).json()
if j.get('error'):
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 not apidata['pageInfo']['totalResults']:
return
j = j['data']
out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % j['title']
if not j.get('duration'):
return out
video = apidata['items'][0]
snippet = video["snippet"]
duration = self.getISOdurationseconds(video["contentDetails"]["duration"])
out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % snippet["title"]
out += ' - length \x02'
length = j['duration']
if length / 3600: # > 1 hour
out += '%dh ' % (length / 3600)
if length / 60:
out += '%dm ' % (length / 60 % 60)
out += "%ds\x02" % (length % 60)
if 'rating' in j:
out += ' - rated \x02%.2f/5.0\x02 (%d)' % (j['rating'],
j['ratingCount'])
if 'viewCount' in j:
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'
if duration["hours"]!=None:
out += '%dh ' % int(duration["hours"])
if duration["minutes"]!=None:
out += '%dm ' % int(duration["minutes"])
out += "%ds\x02" % int(duration["seconds"])
totalvotes = float(video["statistics"]["dislikeCount"])+float(video["statistics"]["likeCount"])
rating = float(video["statistics"]["likeCount"]) / totalvotes
out += ' - rated \x02%.2f/5\x02' % round(rating*5,1)
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")
out += ' - by \x02%s\x02 on \x02%s\x02' % (snippet['channelTitle'], time.strftime("%Y.%m.%d", upload_time))
return out
def group_int_digits(self, number, delimiter=',', grouping=3):
base = str(number).strip()
builder = []

@ -10,12 +10,28 @@
from pyircbot.modulebase import ModuleBase,ModuleHook
from requests import get
import time
import re
class Youtube(ModuleBase):
def __init__(self, bot, moduleName):
ModuleBase.__init__(self, bot, moduleName);
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):
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)))
def get_video_description(self, vid_id):
j = get("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc" % vid_id).json()
if j.get('error'):
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 not apidata['pageInfo']['totalResults']:
return
j = j['data']
out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % j['title']
if not j.get('duration'):
return out
video = apidata['items'][0]
snippet = video["snippet"]
duration = self.getISOdurationseconds(video["contentDetails"]["duration"])
out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % snippet["title"]
out += ' - length \x02'
length = j['duration']
if length / 3600: # > 1 hour
out += '%dh ' % (length / 3600)
if length / 60:
out += '%dm ' % (length / 60 % 60)
out += "%ds\x02" % (length % 60)
if 'rating' in j:
out += ' - rated \x02%.2f/5.0\x02 (%d)' % (j['rating'],
j['ratingCount'])
if 'viewCount' in j:
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'
if duration["hours"]!=None:
out += '%dh ' % int(duration["hours"])
if duration["minutes"]!=None:
out += '%dm ' % int(duration["minutes"])
out += "%ds\x02" % int(duration["seconds"])
totalvotes = float(video["statistics"]["dislikeCount"])+float(video["statistics"]["likeCount"])
rating = float(video["statistics"]["likeCount"]) / totalvotes
out += ' - rated \x02%.2f/5\x02' % round(rating*5,1)
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")
out += ' - by \x02%s\x02 on \x02%s\x02' % (snippet['channelTitle'], time.strftime("%Y.%m.%d", upload_time))
return out
def group_int_digits(self, number, delimiter=',', grouping=3):
base = str(number).strip()
builder = []
@ -76,4 +84,4 @@ class Youtube(ModuleBase):
builder.append(base[-grouping:])
base = base[:-grouping]
builder.reverse()
return delimiter.join(builder)
return delimiter.join(builder)

@ -104,6 +104,7 @@ class PyIRCBot:
:type moduleName: str"""
" check if already exists "
if not name in self.modules:
self.log.debug("Importing %s" % name)
" attempt to load "
try:
moduleref = __import__(name)
@ -123,6 +124,7 @@ class PyIRCBot:
:param moduleName: Name of the module to import
:type moduleName: str"""
self.log.debug("Deporting %s" % name)
" unload if necessary "
if name in self.moduleInstances:
self.unloadmodule(name)

Loading…
Cancel
Save