From 860c444d3dd056444e8269b2c52120ca5b809699 Mon Sep 17 00:00:00 2001 From: Akamaru Date: Sun, 27 Mar 2016 16:04:03 +0200 Subject: [PATCH] =?UTF-8?q?Suche=20f=C3=BCr=20Homebrew.Cloud?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/thc_search.lua | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 plugins/thc_search.lua diff --git a/plugins/thc_search.lua b/plugins/thc_search.lua new file mode 100644 index 0000000..621d3e6 --- /dev/null +++ b/plugins/thc_search.lua @@ -0,0 +1,51 @@ +do + +local makeOurDate = function(dateString) + local pattern = "(%d+)%-(%d+)%-(%d+)" + local year, month, day = dateString:match(pattern) + return day..'.'..month..'.'..year +end + +local function search_thc(tag) + local url = 'http://homebrew.cloud/?json=get_search_results&search='..tag + local res,code = http.request(url) + local data = json:decode(res).posts[1] + if code ~= 200 then return "HTTP-Fehler" end + if not data then return "Nichts gefunden!" end + + local title = data.title + local from = data.author.name + local date = makeOurDate(data.date) + local url = data.url + if data.thumbnail then + image_url = data.thumbnail + end + + local text = unescape(title..' ('..from..' am '..date..')\n')..url + + if data.thumbnail then + return text, image_url + else + return text + end +end + +local function run(msg, matches) +local tag = matches[1] + local text, image_url = search_thc(tag) + local receiver = get_receiver(msg) + if image_url then + local file = download_to_file(image_url) + send_photo(receiver, file, ok_cb, false) + end + return text +end + +return { + description = "Suche für The Homebrew Cloud", + usage = "/thc [BEGRIFF]", + patterns = {"^/[Tt][Hh][Cc] (.*)$"}, + run = run +} + +end \ No newline at end of file