def youtube_info(message):
if not hasattr(config, "youtube_api_key"):
return
link_re = re.compile(r"""(?:https?://)(?:www\.)?(?:(?:youtube\.com(?:/embed/|/watch/?\?(?:.*)v=))|youtu\.be/)(?P<id>[A-Za-z0-9-_]+)""")
match = link_re.search(message.text)
if not match:
return
params = {
"id": match.group("id"),
"part": "contentDetails,statistics,snippet",
"key": config.youtube_api_key
}
async with aiohttp.get("https://www.googleapis.com/youtube/v3/videos", params=params) as resp:
if resp.status != 200:
return
info = await resp.json()
things = dict()
things.update(info["items"][0]["snippet"])
things.update(info["items"][0]["statistics"])
reply = "YouTube: {title} by {channelTitle} ({viewCount} views)".format(**things)
await message.reply(reply)
评论列表
文章目录