This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.py
141 lines (116 loc) · 4.3 KB
/
youtube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# coding=utf8
from __future__ import unicode_literals, division
from sopel.module import rule, commands, example
from sopel.config.types import StaticSection, ValidatedAttribute, NO_DEFAULT
from sopel.formatting import color, colors
from sopel import tools
import datetime
import sys
import re
import googleapiclient.discovery
if sys.version_info.major < 3:
int = long
ISO8601_PERIOD_REGEX = re.compile(
r"^(?P<sign>[+-])?"
r"P(?!\b)"
r"(?P<y>[0-9]+([,.][0-9]+)?(?:Y))?"
r"(?P<mo>[0-9]+([,.][0-9]+)?M)?"
r"(?P<w>[0-9]+([,.][0-9]+)?W)?"
r"(?P<d>[0-9]+([,.][0-9]+)?D)?"
r"((?:T)(?P<h>[0-9]+([,.][0-9]+)?H)?"
r"(?P<m>[0-9]+([,.][0-9]+)?M)?"
r"(?P<s>[0-9]+([,.][0-9]+)?S)?)?$")
regex = re.compile('(youtube.com/watch\S*v=|youtu.be/)([\w-]+)')
API = None
class YoutubeSection(StaticSection):
api_key = ValidatedAttribute('api_key', default=NO_DEFAULT)
"""The Google API key to auth to the endpoint"""
def configure(config):
config.define_section('youtube', YoutubeSection, validate=False)
config.youtube.configure_setting(
'api_key',
'Enter your Google API key.',
)
def setup(bot):
bot.config.define_section('youtube', YoutubeSection)
if not bot.memory.contains('url_callbacks'):
bot.memory['url_callbacks'] = tools.SopelMemory()
bot.memory['url_callbacks'][regex] = get_info
global API
API = googleapiclient.discovery.build("youtube", "v3",
developerKey=bot.config.youtube.api_key)
def shutdown(bot):
del bot.memory['url_callbacks'][regex]
@commands('yt', 'youtube')
@example('.yt h3h3productions rekt together')
def search(bot, trigger):
"""Search YouTube"""
if not trigger.group(2):
return
results = API.search().list(
q=trigger.group(2),
type='video',
part='id,snippet',
maxResults=1,
).execute()
results = results.get('items')
if not results:
bot.say("📺 I couldn't find any YouTube videos for your query.")
return
_say_result(bot, trigger, results[0]['id']['videoId'])
@rule('.*(youtube.com/watch\S*v=|youtu.be/)([\w-]+).*')
def get_info(bot, trigger, found_match=None):
"""
Get information about the latest video uploaded by the channel provided.
"""
match = found_match or trigger
_say_result(bot, trigger, match.group(2), include_link=False)
def _say_result(bot, trigger, id_, include_link=True):
result = API.videos().list(
id=id_,
part='snippet,contentDetails,statistics',
).execute().get('items')
if not result:
return
result = result[0]
message = (
'📺 '
'{title} | Uploader: {uploader} | '
'Length: {length} | Views: {views:,}'
)
snippet = result['snippet']
details = result['contentDetails']
statistics = result['statistics']
duration = _parse_duration(details['duration'])
uploaded = _parse_published_at(bot, trigger, snippet['publishedAt'])
comments = statistics.get('commentCount', '-')
if comments != '-':
comments = '{:,}'.format(int(comments))
message = message.format(
title=snippet['title'],
uploader=snippet['channelTitle'],
length=duration,
uploaded=uploaded,
views=int(statistics['viewCount']),
comments=comments,
)
if 'likeCount' in statistics:
likes = int(statistics['likeCount'])
message += ' | ' + color('{:,}+'.format(likes), colors.GREEN)
if 'dislikeCount' in statistics:
dislikes = int(statistics['dislikeCount'])
message += ' | ' + color('{:,}-'.format(dislikes), colors.RED)
if include_link:
message = message + ' | Link: https://youtu.be/' + id_
bot.say(message)
def _parse_duration(duration):
splitdur = ISO8601_PERIOD_REGEX.match(duration)
dur = []
for k, v in splitdur.groupdict().items():
if v is not None:
dur.append(v.lower())
return ' '.join(dur)
def _parse_published_at(bot, trigger, published):
pubdate = datetime.datetime.strptime(published, '%Y-%m-%dT%H:%M:%S.%fZ')
return tools.time.format_time(bot.db, bot.config, nick=trigger.nick,
channel=trigger.sender, time=pubdate)