bitbot-3.11-fork/modules/youtube.py

164 lines
6.8 KiB
Python
Raw Normal View History

#--depends-on commands
#--depends-on config
#--require-config google-api-key
import re, urllib.parse
from src import EventManager, ModuleManager, utils
2016-03-29 11:56:58 +00:00
REGEX_YOUTUBE = re.compile("https?://(?:www.)?(?:youtu.be/|youtube.com/)\\S+", re.I)
2016-03-29 11:56:58 +00:00
REGEX_ISO8601 = re.compile("PT(\d+H)?(\d+M)?(\d+S)?", re.I)
URL_YOUTUBESEARCH = "https://www.googleapis.com/youtube/v3/search"
URL_YOUTUBEVIDEO = "https://www.googleapis.com/youtube/v3/videos"
URL_YOUTUBEPLAYLIST = "https://www.googleapis.com/youtube/v3/playlists"
2016-03-29 11:56:58 +00:00
URL_YOUTUBESHORT = "https://youtu.be/%s"
URL_VIDEO = "https://www.youtube.com/watch?v=%s"
URL_PLAYLIST = "https://www.youtube.com/playlist?list=%s"
2016-03-29 11:56:58 +00:00
ARROW_UP = ""
ARROW_DOWN = ""
2016-03-29 11:56:58 +00:00
@utils.export("channelset", utils.BoolSetting("auto-youtube",
"Disable/Enable automatically getting info from youtube URLs"))
@utils.export("channelset", utils.BoolSetting("youtube-safesearch",
"Turn safe search off/on"))
class Module(ModuleManager.BaseModule):
def on_load(self):
self.exports.add("search-youtube", self._search_youtube)
2016-03-29 11:56:58 +00:00
def get_video_page(self, video_id, part):
return utils.http.request(URL_YOUTUBEVIDEO, get_params={"part": part,
2016-03-29 11:56:58 +00:00
"id": video_id, "key": self.bot.config["google-api-key"]},
json=True)
def video_details(self, video_id):
snippet = self.get_video_page(video_id, "snippet")
if snippet.data["items"]:
snippet = snippet.data["items"][0]["snippet"]
statistics = self.get_video_page(video_id, "statistics").data[
2016-03-29 11:56:58 +00:00
"items"][0]["statistics"]
content = self.get_video_page(video_id, "contentDetails").data[
2016-03-29 11:56:58 +00:00
"items"][0]["contentDetails"]
video_uploader = snippet["channelTitle"]
video_title = snippet["title"]
video_views = statistics["viewCount"]
video_likes = statistics.get("likeCount")
video_dislikes = statistics.get("dislikeCount")
2016-03-29 11:56:58 +00:00
video_duration = content["duration"]
video_opinions = ""
if video_likes and video_dislikes:
2019-05-19 20:40:48 +00:00
likes = utils.irc.color("%s%s" % (video_likes, ARROW_UP),
utils.consts.GREEN)
2019-05-19 20:40:48 +00:00
dislikes = utils.irc.color("%s%s" %
(ARROW_DOWN, video_dislikes), utils.consts.RED)
video_opinions = " (%s%s)" % (likes, dislikes)
2016-03-29 11:56:58 +00:00
match = re.match(REGEX_ISO8601, video_duration)
video_duration = ""
video_duration += "%s:" % match.group(1)[:-1].zfill(2
) if match.group(1) else ""
video_duration += "%s:" % match.group(2)[:-1].zfill(2
) if match.group(2) else "00:"
2016-03-29 11:56:58 +00:00
video_duration += "%s" % match.group(3)[:-1].zfill(2
) if match.group(3) else "00"
return "%s (%s) uploaded by %s, %s views%s %s" % (
2016-03-29 11:56:58 +00:00
video_title, video_duration, video_uploader, "{:,}".format(
2018-05-02 06:20:52 +00:00
int(video_views)), video_opinions, URL_YOUTUBESHORT % video_id)
2016-03-29 11:56:58 +00:00
def get_playlist_page(self, playlist_id, part):
return utils.http.request(URL_YOUTUBEPLAYLIST, get_params={
"part": part, "id": playlist_id,
"key": self.bot.config["google-api-key"]}, json=True)
def playlist_details(self, playlist_id):
snippet = self.get_playlist_page(playlist_id, "snippet")
if snippet.data["items"]:
snippet = snippet.data["items"][0]["snippet"]
content = self.get_playlist_page(playlist_id, "contentDetails")
count = content.data["items"][0]["contentDetails"]["itemCount"]
return "%s - %s (%d %s) %s" % (snippet["channelTitle"],
snippet["title"], count, "video" if count == 1 else "videos",
URL_PLAYLIST % playlist_id)
def _from_url(self, url):
parsed = urllib.parse.urlparse(url)
query = urllib.parse.parse_qs(parsed.query)
2019-07-31 10:59:29 +00:00
if parsed.hostname == "youtu.be" and parsed.path:
2019-07-31 10:58:08 +00:00
return self.video_details(parsed.path[1:])
elif parsed.path == "/watch" and "v" in query:
return self.video_details(query["v"][0])
elif parsed.path == "/playlist" and "list" in query:
return self.playlist_details(query["list"][0])
def _search_youtube(self, query):
video_id = ""
search_page = utils.http.request(URL_YOUTUBESEARCH,
get_params={"q": query, "part": "snippet",
"maxResults": "1", "type": "video",
"key": self.bot.config["google-api-key"]},
json=True)
if search_page:
if search_page.data["pageInfo"]["totalResults"] > 0:
video_id = search_page.data["items"][0]["id"]["videoId"]
return "https://youtu.be/%s" % video_id
@utils.hook("received.command.yt", alias_of="youtube")
@utils.hook("received.command.youtube")
2016-03-29 11:56:58 +00:00
def yt(self, event):
"""
:help: Find a video on youtube
:usage: [query/URL]
"""
url = None
2016-03-29 11:56:58 +00:00
search = None
if event["args"]:
search = event["args"]
url_match = re.match(REGEX_YOUTUBE, event["args"])
if url_match:
url = event["args"]
else:
search = event["args"]
else:
url = event["target"].buffer.find(REGEX_YOUTUBE)
if not url:
safe_setting = event["target"].get_setting("youtube-safesearch", True)
safe = "moderate" if safe_setting else "none"
search_page = utils.http.request(URL_YOUTUBESEARCH,
get_params={"q": search, "part": "snippet", "maxResults": "1",
"type": "video", "key": self.bot.config["google-api-key"],
"safeSearch": safe}, json=True)
if search_page:
if search_page.data["pageInfo"]["totalResults"] > 0:
url = URL_VIDEO % search_page.data[
"items"][0]["id"]["videoId"]
2016-03-29 11:56:58 +00:00
else:
raise utils.EventError("No videos found")
else:
raise utils.EventsResultsError()
if url:
out = self._from_url(url)
if not out == None:
event["stdout"].write(out)
2016-03-29 11:56:58 +00:00
else:
raise utils.EventsResultsError()
2016-03-29 11:56:58 +00:00
else:
event["stderr"].write("No search phrase provided")
2016-03-29 11:56:58 +00:00
@utils.hook("command.regex")
@utils.kwarg("priority", EventManager.PRIORITY_LOW)
@utils.kwarg("ignore_action", False)
@utils.kwarg("command", "youtube")
@utils.kwarg("pattern", REGEX_YOUTUBE)
2016-03-29 11:56:58 +00:00
def channel_message(self, event):
if event["target"].get_setting("auto-youtube", False):
out = self._from_url(event["match"].group(0))
if not out == None:
event.eat()
event["stdout"].write(out)