youtube: also print playlist info
The plugin has been rewritten to be a little bit cleaner.
This commit is contained in:
parent
e276fe0022
commit
d7dd296b61
115
plugins/youtube
115
plugins/youtube
|
@ -2,7 +2,7 @@
|
||||||
#
|
#
|
||||||
# ZyklonB YouTube plugin, displaying info about YouTube links
|
# ZyklonB YouTube plugin, displaying info about YouTube links
|
||||||
#
|
#
|
||||||
# Copyright 2014 Přemysl Janouch
|
# Copyright 2014 - 2015, Přemysl Janouch <p.janouch@gmail.com>
|
||||||
# See the file LICENSE for licensing information.
|
# See the file LICENSE for licensing information.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
@ -12,85 +12,100 @@ import re
|
||||||
import json
|
import json
|
||||||
import urllib.request
|
import urllib.request
|
||||||
|
|
||||||
sys.stdin = io.TextIOWrapper (sys.__stdin__.buffer,
|
class Plugin:
|
||||||
encoding = 'iso8859-1', newline = '\r\n', line_buffering = True)
|
|
||||||
sys.stdout = io.TextIOWrapper (sys.__stdout__.buffer,
|
|
||||||
encoding = 'iso8859-1', newline = '\r\n', line_buffering = True)
|
|
||||||
|
|
||||||
re_msg = re.compile ('(?::([^! ]*)(?:!([^@]*)@([^ ]*))? +)?'
|
re_msg = re.compile ('(?::([^! ]*)(?:!([^@]*)@([^ ]*))? +)?'
|
||||||
'([^ ]+)(?: +(.*))?\r\n$')
|
'([^ ]+)(?: +(.*))?\r\n$')
|
||||||
re_args = re.compile (':?((?<=:).*|[^ ]+) *')
|
re_args = re.compile (':?((?<=:).*|[^ ]+) *')
|
||||||
|
|
||||||
def parse (line):
|
def parse (self, line):
|
||||||
global re_msg
|
m = self.re_msg.match (line)
|
||||||
global re_args
|
if m is None:
|
||||||
|
|
||||||
m = re_msg.match (line)
|
|
||||||
if m == None:
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
(nick, user, host, command, args) = m.groups ()
|
(nick, user, host, command, args) = m.groups ()
|
||||||
if args == None:
|
args = [] if args is None else self.re_args.findall (args)
|
||||||
args = []
|
|
||||||
else:
|
|
||||||
args = re_args.findall (args)
|
|
||||||
|
|
||||||
return (nick, user, host, command, args)
|
return (nick, user, host, command, args)
|
||||||
|
|
||||||
def get_config (key):
|
def get_config (self, key):
|
||||||
print ("ZYKLONB get_config :%s" % key)
|
print ("ZYKLONB get_config :%s" % key)
|
||||||
(_, _, _, _, args) = parse (sys.stdin.readline ())
|
(_, _, _, _, args) = self.parse (sys.stdin.readline ())
|
||||||
return args[0]
|
return args[0]
|
||||||
|
|
||||||
def bot_print (what):
|
def bot_print (self, what):
|
||||||
print ('ZYKLONB print :%s' % what)
|
print ('ZYKLONB print :%s' % what)
|
||||||
|
|
||||||
youtube_api_key = get_config ('youtube_api_key')
|
class YouTube (Plugin):
|
||||||
if youtube_api_key == "":
|
re_videos = list (map (lambda x: re.compile (x), [
|
||||||
bot_print ("youtube: missing `youtube_api_key'")
|
r'youtube\.[a-z]+/[^ ]*[&?]v=([-\w]+)',
|
||||||
|
r'youtube\.[a-z]+/v/([-\w]+)',
|
||||||
print ("ZYKLONB register")
|
r'youtu\.be/([-\w]+)'
|
||||||
|
]))
|
||||||
regexes = list (map (lambda x: re.compile (x), [
|
re_playlists = list (map (lambda x: re.compile (x), [
|
||||||
'youtube\\.[a-z]+/[^ ]*[&?]v=([-_A-Za-z0-9]+)',
|
r'youtube\.[a-z]+/playlist[&?][^ ]*(?<=&|\?)list=([-\w]+)',
|
||||||
'youtube\\.[a-z]+/v/([-_A-Za-z0-9]+)',
|
|
||||||
'youtu\\.be/([-_A-Za-z0-9]+)'
|
|
||||||
]))
|
]))
|
||||||
|
|
||||||
def print_video_info (channel, video_id):
|
def print_info (self, channel, url, cb):
|
||||||
global youtube_api_key
|
|
||||||
url = 'https://www.googleapis.com/youtube/v3/' \
|
|
||||||
+ 'videos?id=%s&key=%s&part=snippet,contentDetails,statistics' \
|
|
||||||
% (video_id, youtube_api_key)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = json.loads (urllib.request.urlopen
|
data = json.loads (urllib.request.urlopen
|
||||||
(url, None, 30).read ().decode ('utf-8'))
|
(url, None, 30).read ().decode ('utf-8'))
|
||||||
|
|
||||||
for x in data['items']:
|
for line in map (lambda x: "YouTube: " + cb (x), data['items']):
|
||||||
desc = "YouTube: "
|
|
||||||
desc += x['snippet']['title'] + ' | '
|
|
||||||
desc += x['contentDetails']['duration'][2:].lower () + ' | '
|
|
||||||
desc += x['statistics']['viewCount'] + 'x'
|
|
||||||
print ("PRIVMSG %s :%s" % (channel,
|
print ("PRIVMSG %s :%s" % (channel,
|
||||||
desc.encode ('utf-8').decode ('iso8859-1')))
|
line.encode ('utf-8').decode ('iso8859-1')))
|
||||||
|
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
bot_print ('youtube: %s' % (err))
|
self.bot_print ('youtube: %s' % (err))
|
||||||
|
|
||||||
for line in sys.stdin:
|
def print_video_info (self, channel, video_id):
|
||||||
msg = parse (line)
|
url = 'https://www.googleapis.com/youtube/v3/' \
|
||||||
if msg == None:
|
+ 'videos?id=%s&key=%s&part=snippet,contentDetails,statistics' \
|
||||||
continue
|
% (video_id, self.youtube_api_key)
|
||||||
|
self.print_info (channel, url, lambda x: "%s | %s | %sx" % (
|
||||||
|
x['snippet']['title'],
|
||||||
|
x['contentDetails']['duration'][2:].lower (),
|
||||||
|
x['statistics']['viewCount']))
|
||||||
|
|
||||||
|
def print_playlist_info (self, channel, playlist_id):
|
||||||
|
url = 'https://www.googleapis.com/youtube/v3/' \
|
||||||
|
+ 'playlists?id=%s&key=%s&part=snippet,contentDetails' \
|
||||||
|
% (playlist_id, self.youtube_api_key)
|
||||||
|
self.print_info (channel, url, lambda x: "%s | %d videos" % (
|
||||||
|
x['snippet']['title'],
|
||||||
|
x['contentDetails']['itemCount']))
|
||||||
|
|
||||||
|
def process_line (self, line):
|
||||||
|
msg = self.parse (line)
|
||||||
|
if msg is None:
|
||||||
|
return
|
||||||
|
|
||||||
(nick, user, host, command, args) = msg
|
(nick, user, host, command, args) = msg
|
||||||
if command != 'PRIVMSG' or len (args) < 2:
|
if command != 'PRIVMSG' or len (args) < 2:
|
||||||
continue
|
return
|
||||||
|
|
||||||
ctx = args[0]
|
ctx = args[0]
|
||||||
if not ctx.startswith (('#', '+', '&', '!')):
|
if not ctx.startswith (('#', '+', '&', '!')):
|
||||||
ctx = nick
|
ctx = nick
|
||||||
|
|
||||||
for regex in regexes:
|
for regex in self.re_videos:
|
||||||
for i in regex.findall (args[1]):
|
for i in regex.findall (args[1]):
|
||||||
print_video_info (ctx, i)
|
self.print_video_info (ctx, i)
|
||||||
|
for regex in self.re_playlists:
|
||||||
|
for i in regex.findall (args[1]):
|
||||||
|
self.print_playlist_info (ctx, i)
|
||||||
|
|
||||||
|
def run (self):
|
||||||
|
self.youtube_api_key = self.get_config ('youtube_api_key')
|
||||||
|
if self.youtube_api_key == "":
|
||||||
|
self.bot_print ("youtube: missing `youtube_api_key'")
|
||||||
|
|
||||||
|
print ("ZYKLONB register")
|
||||||
|
|
||||||
|
for line in sys.stdin:
|
||||||
|
self.process_line (line)
|
||||||
|
|
||||||
|
sys.stdin = io.TextIOWrapper (sys.__stdin__.buffer,
|
||||||
|
encoding = 'iso8859-1', newline = '\r\n', line_buffering = True)
|
||||||
|
sys.stdout = io.TextIOWrapper (sys.__stdout__.buffer,
|
||||||
|
encoding = 'iso8859-1', newline = '\r\n', line_buffering = True)
|
||||||
|
|
||||||
|
YouTube ().run ()
|
||||||
|
|
Loading…
Reference in New Issue