2017-05-18 00:01:44 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2017-06-30 12:09:46 +00:00
|
|
|
import irc3
|
|
|
|
import requests
|
2017-08-22 13:57:57 +00:00
|
|
|
from docopt import Dict
|
2017-05-29 12:43:04 +00:00
|
|
|
from irc3.plugins.command import command
|
|
|
|
from irc3.utils import IrcString
|
2017-05-18 00:01:44 +00:00
|
|
|
|
|
|
|
from . import Plugin
|
2017-08-22 15:43:48 +00:00
|
|
|
from .utils import date_from_iso
|
2017-05-18 00:01:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class YouTube(Plugin):
|
|
|
|
URL = 'https://www.googleapis.com/youtube/v3'
|
|
|
|
API = '{}/videos?part=snippet,statistics,contentDetails'.format(URL)
|
2024-06-02 15:31:24 +00:00
|
|
|
SEARCH = '{}/search?type=video&maxResults=1&part=id'.format(URL)
|
2021-12-14 20:45:08 +00:00
|
|
|
RETURN_YOUTUBE_DISLIKE_API = 'https://returnyoutubedislikeapi.com/votes'
|
2017-05-18 00:01:44 +00:00
|
|
|
|
2017-05-30 12:26:47 +00:00
|
|
|
def get_video_data(self, video_id: str):
|
2017-06-01 12:50:35 +00:00
|
|
|
"""Requests the infos for a video id and formats them."""
|
2017-05-30 12:26:47 +00:00
|
|
|
data = self._api(self.API, id=video_id)
|
2017-05-18 00:01:44 +00:00
|
|
|
|
2017-07-31 13:29:59 +00:00
|
|
|
if not data.get('items'):
|
2017-05-30 12:26:47 +00:00
|
|
|
return
|
|
|
|
|
2021-12-14 20:45:08 +00:00
|
|
|
return_youtube_dislike_response = requests.get(self.RETURN_YOUTUBE_DISLIKE_API, params={'videoId': video_id}).json()
|
|
|
|
|
2017-05-30 12:26:47 +00:00
|
|
|
item = data['items'][0]
|
2017-05-29 12:43:04 +00:00
|
|
|
date = date_from_iso(item['snippet']['publishedAt'])
|
2024-06-02 15:30:20 +00:00
|
|
|
length = re.findall(r'(\d+[DHMS])', item['contentDetails']['duration'])
|
2017-05-29 12:43:04 +00:00
|
|
|
|
2017-05-30 12:26:47 +00:00
|
|
|
views = int(item['statistics'].get('viewCount', 0))
|
|
|
|
likes = int(item['statistics'].get('likeCount', 0))
|
2021-12-14 20:45:08 +00:00
|
|
|
dislikes = int(return_youtube_dislike_response.get('dislikes', 0))
|
2017-05-29 12:43:04 +00:00
|
|
|
|
|
|
|
try:
|
2017-05-30 12:26:47 +00:00
|
|
|
score = 100 * float(likes) / (likes + dislikes)
|
|
|
|
except ZeroDivisionError:
|
|
|
|
score = 0
|
|
|
|
|
2017-08-22 14:15:52 +00:00
|
|
|
return '\x02[YouTube]\x02 ' \
|
2017-07-31 13:29:59 +00:00
|
|
|
'{title} - length {length} -\x033 {likes:,}\x03 /' \
|
2017-06-01 15:38:34 +00:00
|
|
|
'\x034 {dislikes:,}\x03 ({score:,.1f}%) - {views:,} ' \
|
|
|
|
'views - {channel} on {date}' \
|
2017-05-30 12:26:47 +00:00
|
|
|
.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,
|
2020-05-18 20:48:20 +00:00
|
|
|
date=date.strftime('%d.%m.%Y %H:%M:%S UTC'))
|
2017-05-29 12:43:04 +00:00
|
|
|
|
2017-07-31 13:29:59 +00:00
|
|
|
@irc3.event(r'(?i)^:\S+ PRIVMSG (?P<target>\S+) :.*(?:youtube.*?(?:v=|/v/)'
|
2017-06-01 12:50:35 +00:00
|
|
|
r'|youtu\.be/)(?P<video_id>[-_a-zA-Z0-9]+).*')
|
2017-05-18 00:01:44 +00:00
|
|
|
def youtube_parser(self, target: str, video_id: str):
|
2017-05-30 12:26:47 +00:00
|
|
|
data = self.get_video_data(video_id)
|
|
|
|
if data:
|
|
|
|
self.bot.privmsg(target, data)
|
2017-05-18 00:01:44 +00:00
|
|
|
|
2017-05-29 12:43:04 +00:00
|
|
|
@command
|
2017-08-22 13:57:57 +00:00
|
|
|
def yt(self, mask: IrcString, target: IrcString, args: Dict):
|
2017-05-29 12:43:04 +00:00
|
|
|
"""Searches for query on YouTube and returns first result.
|
2017-05-18 00:01:44 +00:00
|
|
|
|
2017-05-29 12:43:04 +00:00
|
|
|
%%yt <query>...
|
|
|
|
"""
|
2017-05-30 12:26:47 +00:00
|
|
|
data = self._api(self.SEARCH, q=' '.join(args['<query>']))
|
2017-05-29 12:43:04 +00:00
|
|
|
|
|
|
|
if 'error' in data:
|
2017-08-22 09:33:03 +00:00
|
|
|
return '\x02[YouTube]\x02 Error performing search'
|
2022-09-17 10:44:00 +00:00
|
|
|
elif data['pageInfo']['totalResults'] == 0:
|
2017-08-22 09:33:03 +00:00
|
|
|
return '\x02[YouTube]\x02 No results found'
|
2017-05-29 12:43:04 +00:00
|
|
|
else:
|
2017-05-30 12:26:47 +00:00
|
|
|
video_id = data['items'][0]['id']['videoId']
|
2017-07-07 00:11:20 +00:00
|
|
|
data = self.get_video_data(video_id)
|
|
|
|
return '{} - https://youtu.be/{}'.format(data, video_id)
|
2017-05-30 12:26:47 +00:00
|
|
|
|
2017-08-21 14:59:27 +00:00
|
|
|
# noinspection PyMethodMayBeStatic
|
|
|
|
def _api(self, url: str, **kwargs):
|
2017-06-01 12:50:35 +00:00
|
|
|
"""Wrapper around requests.get which adds the Google API key."""
|
2017-05-30 12:26:47 +00:00
|
|
|
kwargs['key'] = os.environ['GOOGLE_API_KEY']
|
|
|
|
return requests.get(url, params=kwargs).json()
|