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/url_title.lua
2015-06-12 15:38:19 +02:00

63 lines
1.7 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

do
function getTitle(page)
local s = page
-- Remove optional spaces from the tags.
s = string.gsub(s, "\n", " ")
s = string.gsub(s, " *< *", "<")
s = string.gsub(s, " *> *", ">")
s = string.gsub(s, "&#124;", "|")
s = string.gsub(s, "&#8249;", "")
s = string.gsub(s, "&lt;", "<")
s = string.gsub(s, "&gt;", ">")
s = string.gsub(s, "&amp;", "&")
s = string.gsub(s, "&#39;", "'")
-- Put all the tags in lowercase.
s = string.gsub(s, "(<[^ >]+)", string.lower)
local i, f, t = string.find(s, "<title>(.+)</title>")
return t or ""
end
function string.ends(str, fin)
return fin=='' or string.sub(str,-string.len(fin)) == fin
end
function run(msg, matches)
local url = matches[1]
if string.ends(url, ".jpg") or string.ends(url, ".gif") or string.ends(url, ".png") then
return
end
local result = http.request(url)
local title = getTitle(result)
--Ignoring 301, 302, 404 and more
if title == "301 Moved Permanently" or
title == "" or
title == "404 Not Found" or
title == "302 Found" or
title == "302 Moved" or
title == "Moved Permanently" or
title == "Redirection" or
title == "Object moved" or
string.match(title, "on Steam") or
string.match(title, "521: Web server is down") or
string.match(title, "eBay</title>") or
string.match(msg.text, "twitch.tv") or
string.match(msg.text, "steamcommunity.com/app/") or
string.match(msg.text, "deviantart.com") then
print('Invalide, da "'..title..'"')
else
return title
end
end
return {
description = "Postet URL-Titel",
usage = {"Irgendein Link"},
patterns = {"^(https?://[%w-_%.%?%.:,/%+=&]+)$",},
run = run
}
end