InlineQuery für YouTube
This commit is contained in:
parent
3e4c1abfcc
commit
464651cba1
@ -2,6 +2,7 @@ local youtube = {}
|
|||||||
|
|
||||||
local utilities = require('otouto.utilities')
|
local utilities = require('otouto.utilities')
|
||||||
local https = require('ssl.https')
|
local https = require('ssl.https')
|
||||||
|
local URL = require('socket.url')
|
||||||
local JSON = require('dkjson')
|
local JSON = require('dkjson')
|
||||||
local bindings = require('otouto.bindings')
|
local bindings = require('otouto.bindings')
|
||||||
|
|
||||||
@ -17,6 +18,9 @@ function youtube:init(config)
|
|||||||
'youtube.com/embed/([A-Za-z0-9-_-]+)',
|
'youtube.com/embed/([A-Za-z0-9-_-]+)',
|
||||||
'youtube.com/watch%?v=([A-Za-z0-9-_-]+)'
|
'youtube.com/watch%?v=([A-Za-z0-9-_-]+)'
|
||||||
}
|
}
|
||||||
|
youtube.inline_triggers = {
|
||||||
|
"^yt (.+)"
|
||||||
|
}
|
||||||
youtube.doc = [[*YouTube-Link*: Postet Infos zu Video]]
|
youtube.doc = [[*YouTube-Link*: Postet Infos zu Video]]
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -78,6 +82,21 @@ function convertISO8601Time(duration)
|
|||||||
return duration
|
return duration
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
function send_youtube_data(data, msg, self, link, sendpic)
|
function send_youtube_data(data, msg, self, link, sendpic)
|
||||||
local title = data.snippet.localized.title
|
local title = data.snippet.localized.title
|
||||||
local title = title:gsub('%*', '\\*')
|
local title = title:gsub('%*', '\\*')
|
||||||
@ -119,17 +138,7 @@ function send_youtube_data(data, msg, self, link, sendpic)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if sendpic then
|
if sendpic then
|
||||||
if data.snippet.thumbnails.maxres then
|
local image_url = get_yt_thumbnail(data)
|
||||||
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
|
|
||||||
-- need to change text, because Telegram captions can only be 200 characters long and don't support Markdown
|
-- 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..')'
|
local text = link..'\n'..title..'\n('..uploader..' am '..upload_date..', '..viewCount..'x angesehen, Länge: '..duration..')'
|
||||||
if blocked then
|
if blocked then
|
||||||
@ -142,6 +151,66 @@ function send_youtube_data(data, msg, self, link, sendpic)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
-- ...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 description = 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)
|
function youtube:action(msg, config, matches)
|
||||||
local yt_code = matches[1]
|
local yt_code = matches[1]
|
||||||
local data = get_yt_data(yt_code)
|
local data = get_yt_data(yt_code)
|
||||||
|
Reference in New Issue
Block a user