2018-08-31 09:50:37 +00:00
|
|
|
# --require-config google-api-key
|
2016-03-30 18:31:23 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
import re
|
|
|
|
import Utils
|
|
|
|
|
|
|
|
REGEX_YOUTUBE = re.compile(
|
2016-03-31 12:36:26 +00:00
|
|
|
"https?://(?:www.)?(?:youtu.be/|youtube.com/watch\?[\S]*v=)([\w\-]{11})",
|
2016-03-29 11:56:58 +00:00
|
|
|
re.I)
|
|
|
|
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_YOUTUBESHORT = "https://youtu.be/%s"
|
|
|
|
|
|
|
|
ARROW_UP = "▲"
|
|
|
|
ARROW_DOWN = "▼"
|
|
|
|
|
2018-08-31 09:50:37 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
class Module(object):
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
bot.events.on("received").on("command").on("yt", "youtube"
|
2018-08-31 09:50:37 +00:00
|
|
|
).hook(self.yt,
|
|
|
|
help="Find a video on youtube",
|
|
|
|
usage="[query]")
|
2016-03-29 11:56:58 +00:00
|
|
|
bot.events.on("received").on("message").on("channel").hook(
|
|
|
|
self.channel_message)
|
|
|
|
|
2017-12-26 10:32:36 +00:00
|
|
|
bot.events.on("postboot").on("configure").on(
|
2018-08-28 13:55:08 +00:00
|
|
|
"channelset").assure_call(setting="auto-youtube",
|
2018-08-31 09:50:37 +00:00
|
|
|
help="Disable/Enable automatically getting info from youtube URLs",
|
|
|
|
validate=Utils.bool_or_none)
|
2017-12-26 10:32:36 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def get_video_page(self, video_id, part):
|
|
|
|
return Utils.get_url(URL_YOUTUBEVIDEO, get_params={"part": part,
|
2018-08-31 09:50:37 +00:00
|
|
|
"id": video_id,
|
|
|
|
"key":
|
|
|
|
self.bot.config[
|
|
|
|
"google-api-key"]},
|
|
|
|
json=True)
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def video_details(self, video_id):
|
|
|
|
snippet = self.get_video_page(video_id, "snippet")
|
|
|
|
if snippet["items"]:
|
|
|
|
snippet = snippet["items"][0]["snippet"]
|
|
|
|
statistics = self.get_video_page(video_id, "statistics")[
|
|
|
|
"items"][0]["statistics"]
|
|
|
|
content = self.get_video_page(video_id, "contentDetails")[
|
|
|
|
"items"][0]["contentDetails"]
|
|
|
|
video_uploader = snippet["channelTitle"]
|
|
|
|
video_title = snippet["title"]
|
|
|
|
video_views = statistics["viewCount"]
|
2016-04-04 17:40:39 +00:00
|
|
|
video_likes = statistics.get("likeCount")
|
|
|
|
video_dislikes = statistics.get("dislikeCount")
|
2016-03-29 11:56:58 +00:00
|
|
|
video_duration = content["duration"]
|
2016-04-04 17:40:39 +00:00
|
|
|
video_opinions = ""
|
|
|
|
if video_likes and video_dislikes:
|
|
|
|
video_opinions = " (%s%s%s%s)" % (video_likes, ARROW_UP,
|
2018-08-31 09:50:37 +00:00
|
|
|
ARROW_DOWN, video_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
|
2018-08-31 09:50:37 +00:00
|
|
|
) if match.group(
|
|
|
|
1) else ""
|
2016-03-29 11:56:58 +00:00
|
|
|
video_duration += "%s:" % match.group(2)[:-1].zfill(2
|
2018-08-31 09:50:37 +00:00
|
|
|
) if match.group(
|
|
|
|
2) else "00:"
|
2016-03-29 11:56:58 +00:00
|
|
|
video_duration += "%s" % match.group(3)[:-1].zfill(2
|
2018-08-31 09:50:37 +00:00
|
|
|
) if match.group(
|
|
|
|
3) else "00"
|
2016-04-04 17:40:39 +00: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-08-31 09:50:37 +00:00
|
|
|
int(video_views)), video_opinions,
|
|
|
|
URL_YOUTUBESHORT % video_id)
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def yt(self, event):
|
|
|
|
video_id = None
|
|
|
|
search = None
|
|
|
|
if event["args"]:
|
|
|
|
search = event["args"]
|
|
|
|
else:
|
2018-08-28 11:23:57 +00:00
|
|
|
last_youtube = event["buffer"].find(REGEX_YOUTUBE)
|
2016-03-29 11:56:58 +00:00
|
|
|
if last_youtube:
|
2018-08-31 09:50:37 +00:00
|
|
|
video_id = re.search(REGEX_YOUTUBE, last_youtube.message).group(
|
|
|
|
1)
|
2016-03-29 11:56:58 +00:00
|
|
|
if search or video_id:
|
|
|
|
if not video_id:
|
|
|
|
search_page = Utils.get_url(URL_YOUTUBESEARCH,
|
2018-08-31 09:50:37 +00:00
|
|
|
get_params={"q": search,
|
|
|
|
"part": "snippet",
|
|
|
|
"maxResults": "1",
|
|
|
|
"type": "video",
|
|
|
|
"key": self.bot.config[
|
|
|
|
"google-api-key"]},
|
|
|
|
json=True)
|
2016-03-29 11:56:58 +00:00
|
|
|
if search_page:
|
|
|
|
if search_page["pageInfo"]["totalResults"] > 0:
|
|
|
|
video_id = search_page["items"][0]["id"]["videoId"]
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No videos found")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Failed to load results")
|
|
|
|
if video_id:
|
|
|
|
event["stdout"].write(self.video_details(video_id))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No search phrase provided")
|
|
|
|
else:
|
2018-08-31 09:50:37 +00:00
|
|
|
event["stderr"].write("No search phrase provided")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def channel_message(self, event):
|
|
|
|
match = re.search(REGEX_YOUTUBE, event["message"])
|
2016-05-01 21:47:59 +00:00
|
|
|
if match and event["channel"].get_setting("auto-youtube", False):
|
2016-03-29 11:56:58 +00:00
|
|
|
youtube_id = match.group(1)
|
|
|
|
video_details = self.video_details(youtube_id)
|
|
|
|
if video_details:
|
|
|
|
self.bot.events.on("send").on("stdout").call(target=event[
|
|
|
|
"channel"], message=video_details, module_name="Youtube",
|
2018-08-31 09:50:37 +00:00
|
|
|
server=event[
|
|
|
|
"server"])
|