2016-04-11 06:04:47 +02:00
|
|
|
local youtube = {}
|
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
local utilities = require('miku.utilities')
|
2016-06-12 19:59:23 +02:00
|
|
|
local https = require('ssl.https')
|
2016-07-17 13:22:27 +02:00
|
|
|
local URL = require('socket.url')
|
2016-06-12 19:59:23 +02:00
|
|
|
local JSON = require('dkjson')
|
2016-07-17 13:22:27 +02:00
|
|
|
local bindings = require('miku.bindings')
|
2016-04-11 06:04:47 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function youtube:init(config)
|
2016-06-12 19:59:23 +02:00
|
|
|
if not cred_data.google_apikey then
|
|
|
|
print('Missing config value: google_apikey.')
|
2016-04-11 06:04:47 +02:00
|
|
|
print('youtube.lua will not be enabled.')
|
|
|
|
return
|
|
|
|
end
|
2016-06-12 19:59:23 +02:00
|
|
|
|
|
|
|
youtube.triggers = {
|
|
|
|
'youtu.be/([A-Za-z0-9-_-]+)',
|
2016-07-17 13:22:27 +02:00
|
|
|
'youtube.com/embed/([A-Za-z0-9-_-]+)',
|
2016-06-12 19:59:23 +02:00
|
|
|
'youtube.com/watch%?v=([A-Za-z0-9-_-]+)'
|
|
|
|
}
|
2016-07-17 13:22:27 +02:00
|
|
|
youtube.inline_triggers = {
|
|
|
|
"^yt (.+)"
|
|
|
|
}
|
2016-06-12 19:59:23 +02:00
|
|
|
youtube.doc = [[*YouTube-Link*: Postet Infos zu Video]]
|
|
|
|
end
|
|
|
|
|
|
|
|
local apikey = cred_data.google_apikey
|
|
|
|
|
|
|
|
local BASE_URL = 'https://www.googleapis.com/youtube/v3'
|
2016-04-12 05:55:46 +02:00
|
|
|
|
2016-06-12 19:59:23 +02:00
|
|
|
local makeOurDate = function(dateString)
|
|
|
|
local pattern = "(%d+)%-(%d+)%-(%d+)T"
|
|
|
|
local year, month, day = dateString:match(pattern)
|
|
|
|
return day..'.'..month..'.'..year
|
|
|
|
end
|
|
|
|
|
2016-07-18 18:37:29 +02:00
|
|
|
function markdown_escape_simple(text)
|
|
|
|
text = text:gsub('_', '\\_')
|
|
|
|
text = text:gsub('%*', '\\*')
|
|
|
|
text = text:gsub('`', '\\`')
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
2016-06-12 19:59:23 +02:00
|
|
|
function get_yt_data (yt_code)
|
|
|
|
local apikey = cred_data.google_apikey
|
|
|
|
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-09-23 20:12:06 +02:00
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
function convertISO8601Time(duration)
|
2016-06-12 19:59:23 +02:00
|
|
|
local a = {}
|
2015-11-25 03:22:04 +01:00
|
|
|
|
2016-06-12 19:59:23 +02:00
|
|
|
for part in string.gmatch(duration, "%d+") do
|
|
|
|
table.insert(a, part)
|
2015-11-25 03:22:04 +01:00
|
|
|
end
|
|
|
|
|
2016-06-12 19:59:23 +02:00
|
|
|
if duration:find('M') and not (duration:find('H') or duration:find('S')) then
|
|
|
|
a = {0, a[1], 0}
|
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
|
2016-06-12 19:59:23 +02:00
|
|
|
if duration:find('H') and not duration:find('M') then
|
|
|
|
a = {a[1], 0, a[2]}
|
2015-11-25 03:22:04 +01:00
|
|
|
end
|
|
|
|
|
2016-06-12 19:59:23 +02:00
|
|
|
if duration:find('H') and not (duration:find('M') or duration:find('S')) then
|
|
|
|
a = {a[1], 0, 0}
|
2016-01-18 20:52:21 +01:00
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
|
2016-06-12 19:59:23 +02:00
|
|
|
duration = 0
|
2015-11-25 03:22:04 +01:00
|
|
|
|
2016-06-12 19:59:23 +02:00
|
|
|
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
|
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
function get_yt_thumbnail(data)
|
|
|
|
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
|
|
|
|
return image_url
|
|
|
|
end
|
|
|
|
|
2016-06-12 19:59:23 +02:00
|
|
|
function send_youtube_data(data, msg, self, link, sendpic)
|
2016-07-18 18:37:29 +02:00
|
|
|
local title = markdown_escape_simple(data.snippet.localized.title)
|
2016-06-12 19:59:23 +02:00
|
|
|
-- local description = data.snippet.localized.description
|
|
|
|
local uploader = data.snippet.channelTitle
|
|
|
|
local upload_date = makeOurDate(data.snippet.publishedAt)
|
|
|
|
local viewCount = comma_value(data.statistics.viewCount)
|
|
|
|
if data.statistics.likeCount then
|
|
|
|
likeCount = ', '..comma_value(data.statistics.likeCount)..' Likes und '
|
|
|
|
dislikeCount = comma_value(data.statistics.dislikeCount)..' Dislikes'
|
|
|
|
else
|
|
|
|
likeCount = ''
|
|
|
|
dislikeCount = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if data.statistics.commentCount then
|
|
|
|
commentCount = ', '..comma_value(data.statistics.commentCount)..' Kommentare'
|
|
|
|
else
|
|
|
|
commentCount = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
local totalseconds = convertISO8601Time(data.contentDetails.duration)
|
|
|
|
local duration = makeHumanTime(totalseconds)
|
|
|
|
if data.contentDetails.regionRestriction then
|
|
|
|
blocked = data.contentDetails.regionRestriction.blocked
|
|
|
|
blocked = table.contains(blocked, "DE")
|
|
|
|
else
|
|
|
|
blocked = false
|
|
|
|
end
|
|
|
|
|
|
|
|
text = '*'..title..'*\n_('..uploader..' am '..upload_date..', '..viewCount..'x angesehen, Länge: '..duration..likeCount..dislikeCount..commentCount..')_\n'
|
|
|
|
if link then
|
|
|
|
text = link..'\n'..text
|
|
|
|
end
|
|
|
|
|
|
|
|
if blocked then
|
|
|
|
text = text..'\n*ACHTUNG, Video ist in Deutschland gesperrt!*'
|
|
|
|
end
|
|
|
|
|
|
|
|
if sendpic then
|
2016-07-17 13:22:27 +02:00
|
|
|
local image_url = get_yt_thumbnail(data)
|
2016-06-12 19:59:23 +02:00
|
|
|
-- need to change text, because Telegram captions can only be 200 characters long and don't support Markdown
|
|
|
|
local text = link..'\n'..title..'\n('..uploader..' am '..upload_date..', '..viewCount..'x angesehen, Länge: '..duration..')'
|
|
|
|
if blocked then
|
|
|
|
text = text..'\nACHTUNG, In Deutschland gesperrt!'
|
|
|
|
end
|
|
|
|
local file = download_to_file(image_url)
|
2016-06-12 20:53:20 +02:00
|
|
|
utilities.send_photo(self, msg.chat.id, file, text, msg.message_id)
|
2016-06-12 19:59:23 +02:00
|
|
|
else
|
|
|
|
utilities.send_reply(self, msg, text, true)
|
|
|
|
end
|
|
|
|
end
|
2015-09-23 20:12:06 +02:00
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
function youtube:inline_callback(inline_query, config, matches)
|
|
|
|
local query = matches[1]
|
|
|
|
local url = BASE_URL..'/search?part=snippet&key='..apikey..'&maxResults=10&type=video&q='..URL.escape(query)..'&fields=items(id(videoId),snippet(publishedAt,title,thumbnails,channelTitle))'
|
|
|
|
local res,code = https.request(url)
|
|
|
|
if code ~= 200 then return end
|
|
|
|
|
|
|
|
local data = JSON.decode(res)
|
|
|
|
if not data.items[1] then return end
|
|
|
|
|
|
|
|
local video_ids = ""
|
|
|
|
-- We get all videoIds from search...
|
|
|
|
for num in pairs(data.items) do
|
|
|
|
video_ids = video_ids..data.items[num].id.videoId
|
|
|
|
if num < #data.items then
|
|
|
|
video_ids = video_ids..','
|
|
|
|
end
|
2016-06-12 19:59:23 +02:00
|
|
|
end
|
|
|
|
|
2016-07-17 13:22:27 +02:00
|
|
|
-- ...and do a second query to get all video infos
|
|
|
|
local url = BASE_URL..'/videos?part=snippet,statistics,contentDetails&key='..apikey..'&id='..video_ids..'&fields=items(id,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 end
|
|
|
|
|
|
|
|
local video_results = JSON.decode(res)
|
|
|
|
if not video_results.items[1] then return end
|
|
|
|
|
|
|
|
local results = '['
|
|
|
|
for num in pairs(video_results.items) do
|
|
|
|
local video_url = 'https://www.youtube.com/watch?v='..video_results.items[num].id
|
|
|
|
local thumb_url = get_yt_thumbnail(video_results.items[num])
|
|
|
|
local video_duration = convertISO8601Time(video_results.items[num].contentDetails.duration)
|
|
|
|
local video_title = video_results.items[num].snippet.localized.title:gsub('"', '\\"')
|
|
|
|
|
|
|
|
if video_results.items[num].statistics.likeCount then
|
|
|
|
likeCount = ', '..comma_value(video_results.items[num].statistics.likeCount)..' Likes, '
|
|
|
|
dislikeCount = comma_value(video_results.items[num].statistics.dislikeCount)..' Dislikes'
|
|
|
|
else
|
|
|
|
likeCount = ''
|
|
|
|
dislikeCount = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if video_results.items[num].statistics.commentCount then
|
|
|
|
commentCount = ', '..comma_value(video_results.items[num].statistics.commentCount)..' Kommentare'
|
|
|
|
else
|
|
|
|
commentCount = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
local readable_dur = makeHumanTime(video_duration)
|
|
|
|
local viewCount = comma_value(video_results.items[num].statistics.viewCount)
|
|
|
|
local uploader = video_results.items[num].snippet.channelTitle
|
|
|
|
local description = uploader..', '..viewCount..' Views, '..readable_dur..likeCount..dislikeCount..commentCount
|
|
|
|
|
|
|
|
results = results..'{"type":"video","id":"'..math.random(100000000000000000)..'","video_url":"'..video_url..'","mime_type":"text/html","thumb_url":"'..thumb_url..'","title":"'..video_title..'","description":"'..description..'","video_duration":'..video_duration..',"input_message_content":{"message_text":"'..video_url..'"}}'
|
|
|
|
if num < #video_results.items then
|
|
|
|
results = results..','
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local results = results..']'
|
|
|
|
utilities.answer_inline_query(self, inline_query, results, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
function youtube:action(msg, config, matches)
|
|
|
|
local yt_code = matches[1]
|
2016-06-12 19:59:23 +02:00
|
|
|
local data = get_yt_data(yt_code)
|
|
|
|
send_youtube_data(data, msg, self)
|
|
|
|
return
|
2015-09-23 20:12:06 +02:00
|
|
|
end
|
|
|
|
|
2016-06-12 19:59:23 +02:00
|
|
|
return youtube
|