From 0c7336229238ab29bde000d9c8049e16948efde9 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Tue, 16 Aug 2016 00:53:23 +0200 Subject: [PATCH] =?UTF-8?q?-=20Akasblog-Plugin=20hinzugef=C3=BCgt=20-=20Un?= =?UTF-8?q?escape=20in=20Utilites?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miku/plugins/akasblog.lua | 61 +++++++++++++++++++++++++++++++++++++++ miku/utilities.lua | 18 ++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 miku/plugins/akasblog.lua diff --git a/miku/plugins/akasblog.lua b/miku/plugins/akasblog.lua new file mode 100644 index 0000000..3eaedea --- /dev/null +++ b/miku/plugins/akasblog.lua @@ -0,0 +1,61 @@ +local wordpress_recent_post = {} + +function wordpress_recent_post:init(config) + wordpress_recent_post.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('aka', true).table +end + +local makeOurDate = function(dateString) + local pattern = "(%d+)%-(%d+)%-(%d+)" + local year, month, day = dateString:match(pattern) + return day..'.'..month..'.'..year +end + +function wordpress_recent_post:get_full_url() + local url = 'http://akamaru.de/?json=get_recent_posts' + local res,code = http.request(url) + if code ~= 200 then return nil end + local data = json.decode(res).posts[1] + if not data then return nil end + + local title = unescape(data.title) + local from = unescape(data.author.name) + local posted_at = makeOurDate(data.date) + local content = data.excerpt:match('

(.*)(.*)

') + end + local content = unescape(content) + local url = data.url + if data.thumbnail then + image_url = data.thumbnail + else + image_url = nil + end + + local text = ''..title..'\n'..from..' am '..posted_at..'\n'..content..'...\nArtikel aufrufen' + + if image_url then + local text = title..' ('..from..' am '..posted_at..')\n\n'..content..'...\n'..url -- captions don't supported markdowns + return text, image_url + else + return text + end +end + +function wordpress_recent_post:action(msg, config) + local text, image_url = wordpress_recent_post:get_full_url() + if not text then + utilities.send_reply(self, msg, config.errors.connection) + return + end + + if image_url then + utilities.send_typing(self, msg.chat.id, 'upload_photo') + local file = download_to_file(image_url) + utilities.send_photo(self, msg.chat.id, file, text, msg.message_id) + else + utilities.send_reply(self, msg, text, 'HTML') + end +end + +return wordpress_recent_post \ No newline at end of file diff --git a/miku/utilities.lua b/miku/utilities.lua index eef2d34..217b84d 100644 --- a/miku/utilities.lua +++ b/miku/utilities.lua @@ -962,6 +962,24 @@ function convertNumbers(str) return str end +function unescape(str) + str = string.gsub( str, '<', '<' ) + str = string.gsub( str, '>', '>' ) + str = string.gsub( str, '"', '"' ) + str = string.gsub( str, ''', "'" ) + str = string.gsub( str, "Ä", "Ä") + str = string.gsub( str, "ä", "ä") + str = string.gsub( str, "Ö", "Ö") + str = string.gsub( str, "ö", "ö") + str = string.gsub( str, "Uuml;", "Ü") + str = string.gsub( str, "ü", "ü") + str = string.gsub( str, "ß", "ß") + str = string.gsub( str, '&#(%d+);', function(n) return string.char(n) end ) + str = string.gsub( str, '&#x(%d+);', function(n) return string.char(tonumber(n,16)) end ) + str = string.gsub( str, '&', '&' ) -- Be sure to do this after all others + return str +end + function url_encode(str) if (str) then str = string.gsub (str, "\n", "\r\n")