Add module for grabbing YouTube info
Diff
config.json.example | 1 +
requirements.txt | 3 ++-
dave/modules/urbandictionary.py | 2 +-
dave/modules/youtube.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 58 insertions(+), 2 deletions(-)
@@ -7,6 +7,7 @@
"api_keys": {
"google_maps": "",
"forecast.io": "",
"youtube": ""
"timezonedb": "",
"mashape": "",
"wolfram": ""
@@ -14,4 +14,5 @@
pysocks==1.6.7
wolframalpha==3.0
babel==2.4.0
humanize==0.5.1
humanize==0.5.1
isodate==0.5.4
@@ -43,7 +43,7 @@
if len(definition) > 200:
definition = definition[:197] + "..."
definition = assembleFormattedText(A.normal[A.bold[str(res["word"])], ": {} [by {} +{}/-{}] [more at {}]".format(
definition = assembleFormattedText(A.normal[A.bold[str(res["word"])], ": {} [by {}, 👍 {} 👎 {}] [more at {}]".format(
definition,
res["author"],
res["thumbs_up"],
@@ -1,0 +1,54 @@
import pickle
import dave.module
import dave.config
from twisted.words.protocols.irc import assembleFormattedText, attributes as A
from requests import get
from humanize import naturaltime, intcomma
from datetime import datetime, timezone
import isodate
BASE_URL = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet," \
"statistics&key={}".format(dave.config.config["api_keys"]["youtube"])
@dave.module.match(r'http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?[\w\?=]*)?')
@dave.module.dont_always_run_if_run()
def youtubevideo(bot, args, sender, source):
"""Ran whenever a YouTube video is sent"""
if not dave.config.redis.exists("youtube:{}".format(args[0])):
req = get("{}&id={}".format(BASE_URL, args[0]),
headers={'user-agent': 'irc bot (https://github.com/w4)'})
if req.status_code != 200:
bot.msg(source, "Bad response from YouTube API: {}".format(req.status_code))
return
req = req.json()
if not req["pageInfo"]["totalResults"]:
bot.msg(source, "That video doesn't exist.")
return
dave.config.redis.setex("youtube:{}".format(args[0]), 400,
pickle.dumps(req))
else:
req = pickle.loads(dave.config.redis.get("youtube:{}".format(args[0])))
resp = req["items"][0]
bot.msg(source, assembleFormattedText(
A.normal[
A.bold[resp["snippet"]["title"]],
" ({}) by {} uploaded {}. {} views, 👍 {} 👎 {}.".format(
str(isodate.parse_duration(resp["contentDetails"]["duration"])),
resp["snippet"]["channelTitle"],
naturaltime(
datetime.now(timezone.utc)
- isodate.parse_datetime(resp["snippet"]["publishedAt"])
),
intcomma(resp["statistics"]["viewCount"]),
intcomma(resp["statistics"]["likeCount"]),
intcomma(resp["statistics"]["dislikeCount"])
)
]
))