2016-06-18 16:41:21 +02:00
|
|
|
local mal = {}
|
|
|
|
|
|
|
|
local xml = require("xml")
|
|
|
|
|
|
|
|
mal.command = 'anime <Anime>, /manga <Manga>'
|
|
|
|
|
|
|
|
function mal:init(config)
|
|
|
|
if not cred_data.mal_user then
|
|
|
|
print('Missing config value: mal_user.')
|
|
|
|
print('myanimelist.lua will not be enabled.')
|
|
|
|
return
|
|
|
|
elseif not cred_data.mal_pw then
|
|
|
|
print('Missing config value: mal_pw.')
|
|
|
|
print('myanimelist.lua will not be enabled.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
mal.triggers = {
|
|
|
|
"^/(anime) (.+)$",
|
2016-07-13 01:33:27 +02:00
|
|
|
"^/(manga) (.+)$"
|
2016-06-18 16:41:21 +02:00
|
|
|
}
|
|
|
|
mal.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[anime*_ <Anime>_: Sendet Infos zum Anime
|
|
|
|
*]]..config.cmd_pat..[[manga*_ <Manga>_: Sendet Infos zum Manga
|
|
|
|
]]
|
|
|
|
end
|
|
|
|
|
|
|
|
local user = cred_data.mal_user
|
|
|
|
local password = cred_data.mal_pw
|
|
|
|
|
2016-08-27 16:06:21 +02:00
|
|
|
local BASE_URL = 'https://'..user..':'..password..'@myanimelist.net/api'
|
2016-06-18 16:41:21 +02:00
|
|
|
|
|
|
|
function mal:delete_tags(str)
|
|
|
|
str = string.gsub( str, '<br />', '')
|
|
|
|
str = string.gsub( str, '%[i%]', '')
|
|
|
|
str = string.gsub( str, '%[/i%]', '')
|
|
|
|
str = string.gsub( str, '—', ' — ')
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
|
|
|
local makeOurDate = function(dateString)
|
|
|
|
local pattern = "(%d+)%-(%d+)%-(%d+)"
|
|
|
|
local year, month, day = dateString:match(pattern)
|
|
|
|
return day..'.'..month..'.'..year
|
|
|
|
end
|
|
|
|
|
|
|
|
function mal:get_mal_info(query, typ)
|
|
|
|
if typ == 'anime' then
|
|
|
|
url = BASE_URL..'/anime/search.xml?q='..query
|
|
|
|
elseif typ == 'manga' then
|
|
|
|
url = BASE_URL..'/manga/search.xml?q='..query
|
|
|
|
end
|
2016-08-27 16:06:21 +02:00
|
|
|
local res,code = https.request(url)
|
2016-06-18 16:41:21 +02:00
|
|
|
if code ~= 200 then return "HTTP-Fehler" end
|
|
|
|
local result = xml.load(res)
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function mal:send_anime_data(result, receiver)
|
|
|
|
local title = xml.find(result, 'title')[1]
|
|
|
|
local id = xml.find(result, 'id')[1]
|
2016-08-27 16:06:21 +02:00
|
|
|
local mal_url = 'https://myanimelist.net/anime/'..id
|
2016-06-18 16:41:21 +02:00
|
|
|
|
|
|
|
if xml.find(result, 'synonyms')[1] then
|
|
|
|
alt_name = '\noder: '..unescape(mal:delete_tags(xml.find(result, 'synonyms')[1]))
|
|
|
|
else
|
|
|
|
alt_name = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'synopsis')[1] then
|
|
|
|
desc = '\n'..unescape(mal:delete_tags(string.sub(xml.find(result, 'synopsis')[1], 1, 200))) .. '...'
|
|
|
|
else
|
|
|
|
desc = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'episodes')[1] then
|
|
|
|
episodes = '\nEpisoden: '..xml.find(result, 'episodes')[1]
|
|
|
|
else
|
|
|
|
episodes = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
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: '..string.gsub(xml.find(result, 'score')[1], "%.", ",")
|
|
|
|
else
|
|
|
|
score = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'type')[1] then
|
|
|
|
typ = '\nTyp: '..xml.find(result, 'type')[1]
|
|
|
|
else
|
|
|
|
typ = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'start_date')[1] ~= "0000-00-00" then
|
|
|
|
startdate = '\nVeröffentlichungszeitraum: '..makeOurDate(xml.find(result, 'start_date')[1])
|
|
|
|
else
|
|
|
|
startdate = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'end_date')[1] ~= "0000-00-00" then
|
|
|
|
enddate = ' - '..makeOurDate(xml.find(result, 'end_date')[1])
|
|
|
|
else
|
|
|
|
enddate = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
local text = '*'..title..'*'..alt_name..typ..episodes..status..score..startdate..enddate..'_'..desc..'_\n[Auf MyAnimeList ansehen]('..mal_url..')'
|
|
|
|
if xml.find(result, 'image') then
|
|
|
|
local image_url = xml.find(result, 'image')[1]
|
|
|
|
return text, image_url
|
|
|
|
else
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function mal:send_manga_data(result)
|
|
|
|
local title = xml.find(result, 'title')[1]
|
|
|
|
local id = xml.find(result, 'id')[1]
|
2016-08-27 16:06:21 +02:00
|
|
|
local mal_url = 'https://myanimelist.net/manga/'..id
|
2016-06-18 16:41:21 +02:00
|
|
|
|
|
|
|
if xml.find(result, 'type')[1] then
|
|
|
|
typ = ' ('..xml.find(result, 'type')[1]..')'
|
|
|
|
else
|
|
|
|
typ = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'synonyms')[1] then
|
|
|
|
alt_name = '\noder: '..unescape(mal:delete_tags(xml.find(result, 'synonyms')[1]))
|
|
|
|
else
|
|
|
|
alt_name = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'chapters')[1] then
|
|
|
|
chapters = '\nKapitel: '..xml.find(result, 'chapters')[1]
|
|
|
|
else
|
|
|
|
chapters = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'status')[1] then
|
|
|
|
status = ' ('..xml.find(result, 'status')[1]..')'
|
|
|
|
else
|
|
|
|
status = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'volumes')[1] then
|
|
|
|
volumes = '\nBände '..xml.find(result, 'volumes')[1]
|
|
|
|
else
|
|
|
|
volumes = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'score')[1] ~= "0.00" then
|
|
|
|
score = '\nScore: '..xml.find(result, 'score')[1]
|
|
|
|
else
|
|
|
|
score = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'start_date')[1] ~= "0000-00-00" then
|
|
|
|
startdate = '\nVeröffentlichungszeitraum: '..makeOurDate(xml.find(result, 'start_date')[1])
|
|
|
|
else
|
|
|
|
startdate = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'end_date')[1] ~= "0000-00-00" then
|
|
|
|
enddate = ' - '..makeOurDate(xml.find(result, 'end_date')[1])
|
|
|
|
else
|
|
|
|
enddate = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if xml.find(result, 'synopsis')[1] then
|
|
|
|
desc = '\n'..unescape(mal:delete_tags(string.sub(xml.find(result, 'synopsis')[1], 1, 200))) .. '...'
|
|
|
|
else
|
|
|
|
desc = ''
|
|
|
|
end
|
|
|
|
|
|
|
|
local text = '*'..title..'*'..alt_name..typ..chapters..status..volumes..score..startdate..enddate..'_'..desc..'_\n[Auf MyAnimeList ansehen]('..mal_url..')'
|
|
|
|
if xml.find(result, 'image') then
|
|
|
|
local image_url = xml.find(result, 'image')[1]
|
|
|
|
return text, image_url
|
|
|
|
else
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-13 01:33:27 +02:00
|
|
|
function mal:action(msg, config, matches)
|
2016-06-18 16:41:21 +02:00
|
|
|
local query = URL.escape(matches[2])
|
|
|
|
if matches[1] == 'anime' then
|
|
|
|
local anime_info = mal:get_mal_info(query, 'anime')
|
|
|
|
if anime_info == "HTTP-Fehler" then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Anime nicht gefunden!')
|
2016-06-18 16:41:21 +02:00
|
|
|
return
|
|
|
|
else
|
|
|
|
local text, image_url = mal:send_anime_data(anime_info)
|
|
|
|
if image_url then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2016-10-03 20:19:20 +02:00
|
|
|
utilities.send_photo(msg.chat.id, image_url, nil, msg.message_id)
|
2016-06-18 16:41:21 +02:00
|
|
|
end
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, text, true)
|
2016-06-18 16:41:21 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
elseif matches[1] == 'manga' then
|
|
|
|
local manga_info = mal:get_mal_info(query, 'manga')
|
|
|
|
if manga_info == "HTTP-Fehler" then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Manga nicht gefunden!')
|
2016-06-18 16:41:21 +02:00
|
|
|
return
|
|
|
|
else
|
|
|
|
local text, image_url = mal:send_manga_data(manga_info)
|
|
|
|
if image_url then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2016-10-03 20:19:20 +02:00
|
|
|
utilities.send_photo(msg.chat.id, image_url, nil, msg.message_id)
|
2016-06-18 16:41:21 +02:00
|
|
|
end
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, text, true)
|
2016-06-18 16:41:21 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return mal
|