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

136 lines
3.4 KiB
Lua
Raw Permalink Normal View History

2015-11-12 17:42:03 +01:00
-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION
do
local xml = require("xml")
local user = cred_data.mal_username
local password = cred_data.mal_pw
local BASE_URL = 'http://'..user..':'..password..'@myanimelist.net/api/anime'
local function delete_tags(str)
str = string.gsub( str, '<br />', '')
str = string.gsub( str, '%[i%]', '')
str = string.gsub( str, '%[/i%]', '')
2015-11-15 20:23:44 +01:00
str = string.gsub( str, '&mdash;', '')
2015-11-12 17:42:03 +01:00
return str
end
local makeOurDate = function(dateString)
local pattern = "(%d+)%-(%d+)%-(%d+)"
local year, month, day = dateString:match(pattern)
2016-01-11 19:43:59 +01:00
if month == "00" then
return year
2016-01-11 19:43:59 +01:00
elseif day == "00" then
return month..'.'..year
else
2015-11-12 17:42:03 +01:00
return day..'.'..month..'.'..year
end
2015-11-12 17:42:03 +01:00
end
local function get_anime_info(anime)
local url = BASE_URL..'/search.xml?q='..anime
local res,code = http.request(url)
if code ~= 200 then return "HTTP-Fehler" end
local result = xml.load(res)
return result
end
local function send_anime_data(result, receiver)
local title = xml.find(result, 'title')[1]
local id = xml.find(result, 'id')[1]
local mal_url = 'http://myanimelist.net/anime/'..id
2015-04-29 21:57:38 +02:00
if xml.find(result, 'type')[1] then
typ = ' ('..xml.find(result, 'type')[1]..')'
2015-11-12 17:42:03 +01:00
else
typ = ''
end
if xml.find(result, 'english')[1] then
eng = '\nEnglisch: '..unescape(delete_tags(xml.find(result, 'english')[1]))
else
eng = ''
end
2016-01-11 19:43:59 +01:00
if xml.find(result, 'synonyms')[1] then
syno = '\nAlternativ: '..unescape(delete_tags(xml.find(result, 'synonyms')[1]))
else
syno = ''
end
2015-04-29 21:57:38 +02:00
2015-11-12 17:42:03 +01:00
if xml.find(result, 'episodes')[1] then
episodes = '\nEpisoden: '..xml.find(result, 'episodes')[1]
else
episodes = ''
2015-04-29 21:57:38 +02:00
end
2015-11-12 17:42:03 +01:00
if xml.find(result, 'status')[1] then
status = ' ('..xml.find(result, 'status')[1]..')'
else
status = ''
end
if xml.find(result, 'score')[1] ~= "0.00" then
score = '\nScore: '..xml.find(result, 'score')[1]
2015-11-12 17:42:03 +01:00
else
score = ''
end
2015-11-12 17:42:03 +01:00
if xml.find(result, 'start_date')[1] ~= "0000-00-00" then
startdate = '\nAusstrahlung: '..makeOurDate(xml.find(result, 'start_date')[1])
2015-11-12 17:42:03 +01:00
else
startdate = ''
end
if xml.find(result, 'end_date')[1] ~= "0000-00-00" then
enddate = ' - '..makeOurDate(xml.find(result, 'end_date')[1])
2015-11-12 17:42:03 +01:00
else
enddate = ''
end
if xml.find(result, 'synopsis')[1] then
2015-11-15 20:23:44 +01:00
desc = '\n'..unescape(delete_tags(string.sub(xml.find(result, 'synopsis')[1], 1, 200)))..'...'
else
desc = ''
end
2015-11-12 17:42:03 +01:00
2016-01-11 19:43:59 +01:00
local text = title..typ..eng..syno..episodes..status..score..startdate..enddate..'\n'..desc..'\n'..mal_url
2015-12-17 17:54:04 +01:00
2015-11-12 17:42:03 +01:00
if xml.find(result, 'image') then
local image_url = xml.find(result, 'image')[1]
local cb_extra = {
receiver=receiver,
url=image_url
}
send_msg(receiver, text, send_photo_from_url_callback, cb_extra)
else
send_msg(receiver, text, ok_cb, false)
end
2015-04-29 21:57:38 +02:00
end
2015-11-12 17:42:03 +01:00
local function run(msg, matches)
local anime = URL.escape(matches[1])
local receiver = get_receiver(msg)
local anime_info = get_anime_info(anime)
if anime_info == "HTTP-Fehler" then
return "Anime nicht gefunden!"
else
send_anime_data(anime_info, receiver)
end
2015-04-29 21:57:38 +02:00
end
return {
2015-11-12 17:42:03 +01:00
description = "Sendet Infos zu einem Anime.",
usage = "#anime [Anime]: Sendet Infos zum Anime",
patterns = {"^#anime (.+)$",
"^#mal (.*)$",
"^#myanimelist (.*)$)",
2015-11-12 17:42:03 +01:00
"^https?://myanimelist.net/anime/[0-9]+/(.*)$"
},
run = run
}
end