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/pluginsold/tumblr_post.lua

65 lines
1.6 KiB
Lua

do
local api_key = cred_data.tumblr_api_key
local makeOurDate = function(dateString)
local pattern = "(%d+)%-(%d+)%-(%d+)"
local year, month, day = dateString:match(pattern)
if month == "00" then
return year
elseif day == "00" then
return month..'.'..year
else
return day..'.'..month..'.'..year
end
end
local function get_tumblr_post(blog, id)
local url = 'https://api.tumblr.com/v2/blog/'..blog..'.tumblr.com/posts?id='..id..'&api_key='..api_key
print(url)
local res,code = https.request(url)
local data = json:decode(res).response
if code ~= 200 then return "HTTP-Fehler" end
if not data then return "HTTP-Fehler" end
local blog_name = data.blog.title
local date = makeOurDate(data.posts[1].date)
local sum = data.posts[1].summary
local short_url = data.posts[1].short_url
if data.posts[1].photos then
pic = data.posts[1].photos[1].original_size.url
end
local text = blog_name..' am '..date..':\n'..sum..'\n'..short_url
if data.posts[1].photos then
return text, pic
else
return text
end
end
local function run(msg, matches)
local blog = matches[1]
local id = matches[2]
local text, pic = get_tumblr_post(blog, id)
local receiver = get_receiver(msg)
local file = download_to_file(pic)
if string.ends(pic, '.gif') then
send_document(receiver, file, ok_cb, false)
else
send_photo(receiver, file, ok_cb, false)
end
return text
end
return {
description = "Sendet Infos zu einem tumblr-Post",
usage = "Link zu tumblr Post",
patterns = {
"^https?://(.*).tumblr.com/post/(%d+)"
},
run = run
}
end