85 lines
3.1 KiB
Python
85 lines
3.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
import os
|
|
import re
|
|
|
|
import irc3
|
|
import requests
|
|
from docopt import Dict
|
|
from irc3.plugins.command import command
|
|
from irc3.utils import IrcString
|
|
|
|
from . import Plugin
|
|
from .utils import date_from_iso
|
|
|
|
|
|
class YouTube(Plugin):
|
|
URL = 'https://www.googleapis.com/youtube/v3'
|
|
API = '{}/videos?part=snippet,statistics,contentDetails'.format(URL)
|
|
SEARCH = '{}/search?type=video&maxResults=1&part=id'.format(URL)
|
|
RETURN_YOUTUBE_DISLIKE_API = 'https://returnyoutubedislikeapi.com/votes'
|
|
|
|
def get_video_data(self, video_id: str):
|
|
"""Requests the infos for a video id and formats them."""
|
|
data = self._api(self.API, id=video_id)
|
|
|
|
if not data.get('items'):
|
|
return
|
|
|
|
return_youtube_dislike_response = requests.get(self.RETURN_YOUTUBE_DISLIKE_API, params={'videoId': video_id}).json()
|
|
|
|
item = data['items'][0]
|
|
date = date_from_iso(item['snippet']['publishedAt'])
|
|
length = re.findall(r'(\d+[DHMS])', item['contentDetails']['duration'])
|
|
|
|
views = int(item['statistics'].get('viewCount', 0))
|
|
likes = int(item['statistics'].get('likeCount', 0))
|
|
dislikes = int(return_youtube_dislike_response.get('dislikes', 0))
|
|
|
|
try:
|
|
score = 100 * float(likes) / (likes + dislikes)
|
|
except ZeroDivisionError:
|
|
score = 0
|
|
|
|
return '\x02[YouTube]\x02 ' \
|
|
'{title} - length {length} -\x033 {likes:,}\x03 /' \
|
|
'\x034 {dislikes:,}\x03 ({score:,.1f}%) - {views:,} ' \
|
|
'views - {channel} on {date}' \
|
|
.format(title=item['snippet']['title'],
|
|
channel=item['snippet']['channelTitle'],
|
|
length=' '.join([l.lower() for l in length]),
|
|
likes=likes,
|
|
dislikes=dislikes,
|
|
score=score,
|
|
views=views,
|
|
date=date.strftime('%d.%m.%Y %H:%M:%S UTC'))
|
|
|
|
@irc3.event(r'(?i)^:\S+ PRIVMSG (?P<target>\S+) :.*(?:youtube.*?(?:v=|/v/)'
|
|
r'|youtu\.be/)(?P<video_id>[-_a-zA-Z0-9]+).*')
|
|
def youtube_parser(self, target: str, video_id: str):
|
|
data = self.get_video_data(video_id)
|
|
if data:
|
|
self.bot.privmsg(target, data)
|
|
|
|
@command
|
|
def yt(self, mask: IrcString, target: IrcString, args: Dict):
|
|
"""Searches for query on YouTube and returns first result.
|
|
|
|
%%yt <query>...
|
|
"""
|
|
data = self._api(self.SEARCH, q=' '.join(args['<query>']))
|
|
|
|
if 'error' in data:
|
|
return '\x02[YouTube]\x02 Error performing search'
|
|
elif data['pageInfo']['totalResults'] == 0:
|
|
return '\x02[YouTube]\x02 No results found'
|
|
else:
|
|
video_id = data['items'][0]['id']['videoId']
|
|
data = self.get_video_data(video_id)
|
|
return '{} - https://youtu.be/{}'.format(data, video_id)
|
|
|
|
# noinspection PyMethodMayBeStatic
|
|
def _api(self, url: str, **kwargs):
|
|
"""Wrapper around requests.get which adds the Google API key."""
|
|
kwargs['key'] = os.environ['GOOGLE_API_KEY']
|
|
return requests.get(url, params=kwargs).json()
|