2014-07-25 00:02:29 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# ZyklonB YouTube plugin, displaying info about YouTube links
|
|
|
|
#
|
2015-06-22 01:40:30 +02:00
|
|
|
# Copyright 2014 - 2015, Přemysl Janouch <p.janouch@gmail.com>
|
2014-07-25 00:02:29 +02:00
|
|
|
# See the file LICENSE for licensing information.
|
|
|
|
#
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import io
|
|
|
|
import re
|
|
|
|
import json
|
|
|
|
import urllib.request
|
|
|
|
|
2015-06-22 01:40:30 +02:00
|
|
|
class Plugin:
|
|
|
|
re_msg = re.compile ('(?::([^! ]*)(?:!([^@]*)@([^ ]*))? +)?'
|
|
|
|
'([^ ]+)(?: +(.*))?\r\n$')
|
|
|
|
re_args = re.compile (':?((?<=:).*|[^ ]+) *')
|
|
|
|
|
|
|
|
def parse (self, line):
|
|
|
|
m = self.re_msg.match (line)
|
|
|
|
if m is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
(nick, user, host, command, args) = m.groups ()
|
|
|
|
args = [] if args is None else self.re_args.findall (args)
|
|
|
|
return (nick, user, host, command, args)
|
|
|
|
|
|
|
|
def get_config (self, key):
|
|
|
|
print ("ZYKLONB get_config :%s" % key)
|
|
|
|
(_, _, _, _, args) = self.parse (sys.stdin.readline ())
|
|
|
|
return args[0]
|
|
|
|
|
|
|
|
def bot_print (self, what):
|
|
|
|
print ('ZYKLONB print :%s' % what)
|
|
|
|
|
|
|
|
class YouTube (Plugin):
|
2015-06-22 19:29:11 +02:00
|
|
|
re_videos = [re.compile (x) for x in [
|
2015-06-22 01:40:30 +02:00
|
|
|
r'youtube\.[a-z]+/[^ ]*[&?]v=([-\w]+)',
|
|
|
|
r'youtube\.[a-z]+/v/([-\w]+)',
|
|
|
|
r'youtu\.be/([-\w]+)'
|
2015-06-22 19:29:11 +02:00
|
|
|
]]
|
|
|
|
re_playlists = [re.compile (x) for x in [
|
2015-06-22 01:40:30 +02:00
|
|
|
r'youtube\.[a-z]+/playlist[&?][^ ]*(?<=&|\?)list=([-\w]+)',
|
2015-06-22 19:29:11 +02:00
|
|
|
]]
|
2015-06-22 01:40:30 +02:00
|
|
|
|
|
|
|
def print_info (self, channel, url, cb):
|
|
|
|
try:
|
|
|
|
data = json.loads (urllib.request.urlopen
|
|
|
|
(url, None, 30).read ().decode ('utf-8'))
|
|
|
|
|
|
|
|
for line in map (lambda x: "YouTube: " + cb (x), data['items']):
|
|
|
|
print ("PRIVMSG %s :%s" % (channel,
|
|
|
|
line.encode ('utf-8').decode ('iso8859-1')))
|
|
|
|
|
|
|
|
except Exception as err:
|
|
|
|
self.bot_print ('youtube: %s' % (err))
|
|
|
|
|
|
|
|
def print_video_info (self, channel, video_id):
|
|
|
|
url = 'https://www.googleapis.com/youtube/v3/' \
|
|
|
|
+ 'videos?id=%s&key=%s&part=snippet,contentDetails,statistics' \
|
|
|
|
% (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
|
|
|
|
if command != 'PRIVMSG' or len (args) < 2:
|
|
|
|
return
|
|
|
|
|
|
|
|
ctx = args[0]
|
|
|
|
if not ctx.startswith (('#', '+', '&', '!')):
|
|
|
|
ctx = nick
|
|
|
|
|
|
|
|
for regex in self.re_videos:
|
|
|
|
for i in regex.findall (args[1]):
|
|
|
|
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)
|
|
|
|
|
2014-07-25 00:02:29 +02:00
|
|
|
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)
|
|
|
|
|
2015-06-22 01:40:30 +02:00
|
|
|
YouTube ().run ()
|