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/akasblog_search.lua

94 lines
3.0 KiB
Lua
Raw Normal View History

2016-01-31 19:09:09 +01:00
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_post(tag)
local url = 'http://akamaru.de/?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
-- Character encoding
title = string.gsub(title, "´", "´")
title = string.gsub(title, "&", "&")
title = string.gsub(title, ">", ">")
title = string.gsub(title, """, '"')
title = string.gsub(title, "&lt;", "<")
title = string.gsub(title, "&mdash;", "")
title = string.gsub(title, "&nabla;", "")
title = string.gsub(title, "&ndash;", "")
title = string.gsub(title, "&Psi;", "ψ")
title = string.gsub(title, "&psi;", "ψ")
title = string.gsub(title, "&raquo;", "»")
title = string.gsub(title, "&szlig;", "ß")
title = string.gsub(title, "&trade;", "")
title = string.gsub(title, "&#038;", "&")
title = string.gsub(title, "&#039;", "'")
title = string.gsub(title, "&#39;", "'")
title = string.gsub(title, "&#124;", "|")
title = string.gsub(title, "&#160;", " ")
title = string.gsub(title, "&#187;", "»")
title = string.gsub(title, "&#223;", "ß")
title = string.gsub(title, "&#8211;", "")
title = string.gsub(title, "&#8217;", "'")
title = string.gsub(title, "&#8220;", "")
title = string.gsub(title, "&#8221;", "")
title = string.gsub(title, "&#8222;", "")
title = string.gsub(title, "&#8249;", "")
title = string.gsub(title, "&#8364;", "")
-- Ä Ö Ü
title = string.gsub(title, "&auml;", "ä")
title = string.gsub(title, "&Auml;", "Ä")
title = string.gsub(title, "&#228;", "ä")
title = string.gsub(title, "&#196;", "Ä")
title = string.gsub(title, "&ouml;", "ö")
title = string.gsub(title, "&Ouml;", "Ö")
title = string.gsub(title, "&#246;", "ö")
title = string.gsub(title, "&#214;", "Ö")
title = string.gsub(title, "&uuml;", "ü")
title = string.gsub(title, "&Uuml;", "Ü")
title = string.gsub(title, "&#252;", "ü")
title = string.gsub(title, "&#220;", "Ü")
local from = data.author.name
local date = makeOurDate(data.date)
2016-02-07 14:12:09 +01:00
local content = string.match(data.excerpt, '<p>(.*)<span')
2016-01-31 19:09:09 +01:00
local url = data.url
if data.thumbnail then
image_url = data.thumbnail
end
local text = title..' ('..from..' am '..date..')\n\n'..content..'...\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_post(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 Aka's Blog",
usage = "/aka [BEGRIFF]",
patterns = {"^/[Aa][Kk][Aa] (.*)$"},
run = run
}
end