From 0e197425422640fc33335fdd8b9ce0f5fdd9aaaa Mon Sep 17 00:00:00 2001 From: Akamaru Date: Wed, 3 Feb 2016 21:15:16 +0100 Subject: [PATCH] tumblr Plugin Fix by @Brawl --- .gitignore | 2 - plugins/pluginsold/tumblr_blog.lua | 46 +++++++++++ plugins/pluginsold/tumblr_post.lua | 65 +++++++++++++++ plugins/tumblr.lua | 124 +++++++++++++++++++++++++++++ 4 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 plugins/pluginsold/tumblr_blog.lua create mode 100644 plugins/pluginsold/tumblr_post.lua create mode 100644 plugins/tumblr.lua diff --git a/.gitignore b/.gitignore index 0ea0ac1..54f7f09 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,4 @@ plugins/pegelf_img.lua plugins/plex.lua plugins/chantalle.lua plugins/plex_xml.lua -plugins/tumblr_blog.lua -plugins/tumblr_post.lua plugins/wiiu.lua \ No newline at end of file diff --git a/plugins/pluginsold/tumblr_blog.lua b/plugins/pluginsold/tumblr_blog.lua new file mode 100644 index 0000000..8b433e2 --- /dev/null +++ b/plugins/pluginsold/tumblr_blog.lua @@ -0,0 +1,46 @@ +do + +local api_key = cred_data.tumblr_api_key + +local function get_blog_info(id) + local url = 'https://api.tumblr.com/v2/blog/'..id..'.tumblr.com/info?api_key='..api_key + local res,code = https.request(url) + local data = json:decode(res).response.blog + if code ~= 200 then return "HTTP-Fehler" end + if not data then return "HTTP-Fehler" end + + local title = data.title + local desc = data.description + if data.posts == 1 then + posts = data.posts..' Post' + else + posts = data.posts..' Posts' + end + if data.likes then + likes = ', '..data.likes..' Likes' + else + likes = '' + end + + local text = title..'\n'..desc..'\n'..posts..likes..'\n' + + return text +end + +local function run(msg, matches) + local id = matches[1] + local text = get_blog_info(id) + local receiver = get_receiver(msg) + return text +end + +return { + description = "Sendet Infos zu einem tumblr-Blog", + usage = "Link zu tumblr Blog", + patterns = { + "^https?://(.*).tumblr.com/?$" + }, + run = run +} + +end \ No newline at end of file diff --git a/plugins/pluginsold/tumblr_post.lua b/plugins/pluginsold/tumblr_post.lua new file mode 100644 index 0000000..aebee74 --- /dev/null +++ b/plugins/pluginsold/tumblr_post.lua @@ -0,0 +1,65 @@ +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 \ No newline at end of file diff --git a/plugins/tumblr.lua b/plugins/tumblr.lua new file mode 100644 index 0000000..74f4989 --- /dev/null +++ b/plugins/tumblr.lua @@ -0,0 +1,124 @@ +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_text(data) + local title = data.posts[1].title + local post = unescape(data.posts[1].body) + return title, post +end + +local function get_tumblr_photo(data) + local caption = data.posts[1].summary + local image_url = data.posts[1].photos[1].original_size.url + return caption, image_url +end + +local function get_tumblr_video(data) + local caption = data.posts[1].caption + local video_url = data.posts[1].permalink_url + return caption, video_url +end + +local function get_tumblr_info(blog, post, receiver) + local url = 'https://api.tumblr.com/v2/blog/'..blog..'.tumblr.com/posts?id='..post..'&api_key='..api_key..'&filter=text' + local res,code = https.request(url) + local data = json:decode(res).response + if code ~= 200 then return "HTTP-Fehler" end + if not data.posts then return "HTTP-Fehler" end + + -- General blog info, present on every type + local blog_name = data.blog.title + local created_at = makeOurDate(data.posts[1].date) + local short_url = data.posts[1].short_url + local typ = data.posts[1].type + local text = blog_name..' am '..created_at..':\n' + + -- type-specific + if typ == 'text' then + local title, post = get_tumblr_text(data) + text = text..title..'\n'..post..'\n'..short_url + elseif typ == 'photo' then + local caption, image_url = get_tumblr_photo(data) + text = text..caption..'\n'..short_url + local file = download_to_file(image_url) + local cb_extra = {file_path=file} + if string.ends(image_url, '.gif') then + send_document(receiver, file, rmtmp_cb, cb_extra) + else + send_photo(receiver, file, rmtmp_cb, cb_extra) + end + elseif typ == 'video' then + local caption, video_url = get_tumblr_video(data) + text = text..caption..'\n'..video_url..'\n'..short_url + else + text = text..'Konnte Format nicht bestimmen (ist: '..typ..')' + end + + return text +end + +local function get_tumblr_blog(blog, receiver) + local url = 'https://api.tumblr.com/v2/blog/'..blog..'.tumblr.com/info?api_key='..api_key + local res,code = https.request(url) + local data = json:decode(res).response.blog + if code ~= 200 then return "HTTP-Fehler" end + if not data then return "HTTP-Fehler" end + + local title = data.title + if data.description == "" then + description = '' + else + description = '\n'..data.description + end + + if data.posts == 1 then + posts = data.posts..' Post' + else + posts = data.posts..' Posts' + end + + if data.likes then + likes = ', '..data.likes..' Likes' + else + likes = '' + end + + local text = title..description..'\n\n'..posts..likes..'\n' + return text +end + +local function run(msg, matches) + local blog = matches[1] + local post = matches[2] + local receiver = get_receiver(msg) + if not post then + return get_tumblr_blog(blog, receiver) + else + return get_tumblr_info(blog, post, receiver) + end +end + +return { + description = "Sendet Infos zu einem tumblr-Post oder -Blog", + usage = "Link zu tumblr-Post oder -Blog", + patterns = { + "^https?://(.*).tumblr.com/post/(%d+)", + "^https?://(.*).tumblr.com/?$" + }, + run = run +} + +end \ No newline at end of file