This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/plugins/youtube.lua

149 lines
4.2 KiB
Lua
Raw Normal View History

do
local BASE_URL = 'https://www.googleapis.com/youtube/v3'
2015-11-12 17:42:03 +01:00
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
local makeOurDate = function(dateString)
local pattern = "(%d+)%-(%d+)%-(%d+)T"
local year, month, day = dateString:match(pattern)
return day..'.'..month..'.'..year
end
function get_yt_data (yt_code)
local apikey = cred_data.google_apikey
2016-01-31 22:29:17 +01:00
local url = BASE_URL..'/videos?part=snippet,statistics,contentDetails&key='..apikey..'&id='..yt_code..'&fields=items(snippet(publishedAt,channelTitle,localized(title,description),thumbnails),statistics(viewCount,likeCount,dislikeCount,commentCount),contentDetails(duration,regionRestriction(blocked)))'
local res,code = https.request(url)
if code ~= 200 then return "HTTP-FEHLER" end
local data = json:decode(res).items[1]
return data
end
2015-06-09 16:23:27 +02:00
local function convertISO8601Time(duration)
local a = {}
for part in string.gmatch(duration, "%d+") do
table.insert(a, part)
end
if duration:find('M') and not (duration:find('H') or duration:find('S')) then
a = {0, a[1], 0}
end
if duration:find('H') and not duration:find('M') then
a = {a[1], 0, a[2]}
end
if duration:find('H') and not (duration:find('M') or duration:find('S')) then
a = {a[1], 0, 0}
end
duration = 0
if #a == 3 then
duration = duration + tonumber(a[1]) * 3600
duration = duration + tonumber(a[2]) * 60
duration = duration + tonumber(a[3])
end
if #a == 2 then
duration = duration + tonumber(a[1]) * 60
duration = duration + tonumber(a[2])
end
if #a == 1 then
duration = duration + tonumber(a[1])
end
return duration
end
2015-11-12 17:42:03 +01:00
function send_youtube_data(data, receiver, link, sendpic)
local title = data.snippet.localized.title
2016-01-31 22:29:17 +01:00
-- local description = data.snippet.localized.description
local uploader = data.snippet.channelTitle
2016-01-31 22:29:17 +01:00
local upload_date = makeOurDate(data.snippet.publishedAt)
2015-11-12 17:42:03 +01:00
local viewCount = comma_value(data.statistics.viewCount)
if data.statistics.likeCount then
2016-01-31 22:29:17 +01:00
likeCount = ', '..comma_value(data.statistics.likeCount)..' Likes und '
dislikeCount = comma_value(data.statistics.dislikeCount)..' Dislikes'
2015-11-12 17:42:03 +01:00
else
likeCount = ''
dislikeCount = ''
end
2016-01-31 22:29:17 +01:00
if data.statistics.commentCount then
commentCount = ', '..comma_value(data.statistics.commentCount)..' Kommentare'
else
commentCount = ''
end
2015-06-09 16:23:27 +02:00
local totalseconds = convertISO8601Time(data.contentDetails.duration)
2016-01-31 22:29:17 +01:00
local duration = makeHumanTime(totalseconds)
2015-11-12 17:42:03 +01:00
if data.contentDetails.regionRestriction then
blocked = data.contentDetails.regionRestriction.blocked
blocked = table.contains(blocked, "DE")
else
blocked = false
end
2016-01-31 22:29:17 +01:00
text = title..'\n('..uploader..' am '..upload_date..', '..viewCount..'x angesehen, Länge: '..duration..likeCount..dislikeCount..commentCount..')\n'
2015-11-12 17:42:03 +01:00
if link then
text = link..'\n'..text
end
2015-06-09 16:23:27 +02:00
2015-11-12 17:42:03 +01:00
if blocked then
text = text..'\nACHTUNG, Video ist in Deutschland geblockt!'
end
if sendpic then
if data.snippet.thumbnails.maxres then
image_url = data.snippet.thumbnails.maxres.url
elseif data.snippet.thumbnails.high then
image_url = data.snippet.thumbnails.high.url
elseif data.snippet.thumbnails.medium then
image_url = data.snippet.thumbnails.medium.url
elseif data.snippet.thumbnails.standard then
image_url = data.snippet.thumbnails.standard.url
else
image_url = data.snippet.thumbnails.default.url
end
local cb_extra = {
receiver = receiver,
url = image_url
}
send_msg(receiver, text, send_photo_from_url_callback, cb_extra)
else
send_msg(receiver, text, ok_cb, false)
end
end
function run(msg, matches)
local yt_code = matches[1]
local data = get_yt_data(yt_code)
local receiver = get_receiver(msg)
2016-07-07 21:24:02 +02:00
local thumb = download_to_file('http://i.ytimg.com/vi_webp/'..yt_code..'/hqdefault.webp')
send_document(receiver, thumb, ok_cb, false)
send_youtube_data(data, receiver)
end
return {
description = "Sendet YouTube-Info.",
2015-11-12 17:42:03 +01:00
usage = "URL zu YouTube-Video",
patterns = {
"youtu.be/([A-Za-z0-9-_-]+)",
2015-12-12 23:26:50 +01:00
"youtube.com/watch%?v=([A-Za-z0-9-_-]+)",
"youtube.com/embed/([A-Za-z0-9-_-]+)",
2015-12-12 23:26:50 +01:00
"c3nt.de/lor/([A-Za-z0-9-_-]+)"
},
run = run
}
2015-06-09 16:23:27 +02:00
end