2015-05-30 13:17:46 +00:00
|
|
|
from urllib.parse import urlparse
|
2015-06-11 05:06:44 +00:00
|
|
|
import re, json, subprocess
|
2015-05-30 13:17:46 +00:00
|
|
|
|
2015-10-30 20:57:45 +00:00
|
|
|
from nemubot.exception import IMException
|
2015-05-29 11:54:00 +00:00
|
|
|
from nemubot.hooks import hook
|
2015-10-13 22:17:02 +00:00
|
|
|
from nemubot.tools.web import _getNormalizedURL, getURLContent
|
2015-05-29 11:54:00 +00:00
|
|
|
from more import Response
|
|
|
|
|
2015-05-30 13:17:46 +00:00
|
|
|
"""Get information of youtube videos"""
|
2015-05-29 11:54:00 +00:00
|
|
|
|
2015-05-30 13:17:46 +00:00
|
|
|
nemubotversion = 3.4
|
2015-05-29 11:54:00 +00:00
|
|
|
|
|
|
|
def help_full():
|
2015-07-10 21:09:54 +00:00
|
|
|
return "!yt [<url>]: with an argument, get information about the given link; without arguments, use the latest link seen on the current channel."
|
2015-05-30 13:17:46 +00:00
|
|
|
|
2015-06-11 05:06:44 +00:00
|
|
|
def _get_ytdl(links):
|
|
|
|
cmd = 'youtube-dl -j --'.split()
|
|
|
|
cmd.extend(links)
|
|
|
|
res = []
|
|
|
|
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as p:
|
|
|
|
if p.wait() > 0:
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("Error while retrieving video information.")
|
2015-06-11 05:06:44 +00:00
|
|
|
for line in p.stdout.read().split(b"\n"):
|
|
|
|
localres = ''
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
info = json.loads(line.decode('utf-8'))
|
|
|
|
if info.get('fulltitle'):
|
|
|
|
localres += info['fulltitle']
|
|
|
|
elif info.get('title'):
|
|
|
|
localres += info['title']
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
if info.get('duration'):
|
|
|
|
d = info['duration']
|
|
|
|
localres += ' [{0}:{1:06.3f}]'.format(int(d/60), d%60)
|
|
|
|
if info.get('age_limit'):
|
|
|
|
localres += ' [-{}]'.format(info['age_limit'])
|
|
|
|
if info.get('uploader'):
|
|
|
|
localres += ' by {}'.format(info['uploader'])
|
|
|
|
if info.get('upload_date'):
|
|
|
|
localres += ' on {}'.format(info['upload_date'])
|
|
|
|
if info.get('description'):
|
|
|
|
localres += ': ' + info['description']
|
|
|
|
if info.get('webpage_url'):
|
|
|
|
localres += ' | ' + info['webpage_url']
|
|
|
|
res.append(localres)
|
|
|
|
if not res:
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("No video information to retrieve about this. Sorry!")
|
2015-06-11 05:06:44 +00:00
|
|
|
return res
|
|
|
|
|
2015-05-30 13:17:46 +00:00
|
|
|
LAST_URLS = dict()
|
2015-05-29 11:54:00 +00:00
|
|
|
|
2015-06-11 05:06:44 +00:00
|
|
|
|
2015-11-01 12:54:59 +00:00
|
|
|
@hook.command("yt")
|
2015-05-29 11:54:00 +00:00
|
|
|
def get_info_yt(msg):
|
2015-05-30 13:17:46 +00:00
|
|
|
links = list()
|
|
|
|
|
2015-05-29 11:54:00 +00:00
|
|
|
if len(msg.args) <= 0:
|
2015-05-30 13:17:46 +00:00
|
|
|
global LAST_URLS
|
|
|
|
if msg.channel in LAST_URLS and len(LAST_URLS[msg.channel]) > 0:
|
|
|
|
links.append(LAST_URLS[msg.channel].pop())
|
|
|
|
else:
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("I don't have any youtube URL for now, please provide me one to get information!")
|
2015-05-30 13:17:46 +00:00
|
|
|
else:
|
|
|
|
for url in msg.args:
|
|
|
|
links.append(url)
|
|
|
|
|
2015-06-11 05:06:44 +00:00
|
|
|
data = _get_ytdl(links)
|
2015-05-30 13:17:46 +00:00
|
|
|
res = Response(channel=msg.channel)
|
2015-06-11 05:06:44 +00:00
|
|
|
for msg in data:
|
|
|
|
res.append_message(msg)
|
2015-05-30 13:17:46 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
|
2015-11-01 12:54:59 +00:00
|
|
|
@hook.message()
|
2015-05-30 13:17:46 +00:00
|
|
|
def parselisten(msg):
|
|
|
|
parseresponse(msg)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2015-11-01 12:54:59 +00:00
|
|
|
@hook.post()
|
2015-05-30 13:17:46 +00:00
|
|
|
def parseresponse(msg):
|
|
|
|
global LAST_URLS
|
2015-10-13 05:49:53 +00:00
|
|
|
if hasattr(msg, "text") and msg.text:
|
2015-10-03 13:32:36 +00:00
|
|
|
urls = re.findall("([a-zA-Z0-9+.-]+:(?://)?[^ :]+)", msg.text)
|
2015-07-10 21:09:54 +00:00
|
|
|
for url in urls:
|
2015-10-13 22:17:02 +00:00
|
|
|
o = urlparse(_getNormalizedURL(url))
|
2015-07-10 21:09:54 +00:00
|
|
|
if o.scheme != "":
|
|
|
|
if o.netloc == "" and len(o.path) < 10:
|
|
|
|
continue
|
2015-11-01 12:54:59 +00:00
|
|
|
for recv in msg.to:
|
2015-09-21 00:23:15 +00:00
|
|
|
if recv not in LAST_URLS:
|
|
|
|
LAST_URLS[recv] = list()
|
|
|
|
LAST_URLS[recv].append(url)
|
2015-05-30 13:17:46 +00:00
|
|
|
return msg
|