minetestbot-modules/title.py

51 lines
1.1 KiB
Python
Raw Normal View History

2013-05-09 12:52:55 -07:00
#!/usr/bin/env python
"""
title.py - Phenny URL Title Module
2014-07-20 07:13:59 -07:00
Copyright 2014, sfan5
2013-05-09 12:52:55 -07:00
"""
2014-07-20 07:13:59 -07:00
import re
import web
import html
2014-07-20 07:13:59 -07:00
r_title = re.compile(r'(?ims)<\s*title[^>]*>(.*?)<\s*/\s*title\s*>')
def title(phenny, input):
uri = input.group(2)
if uri:
uri = uri.strip()
if not uri.startswith("http://") and not uri.startswith("https://"):
return phenny.say("That's not a valid URL")
elif hasattr(phenny.bot, 'last_seen_uri'):
uri = phenny.bot.last_seen_uri
else:
return phenny.reply("Give me a link.")
data, sc = web.get(uri, 16384)
if sc != 200:
return phenny.say("HTTP error %d" % sc)
2017-08-23 10:56:15 -07:00
data = str(data, 'utf-8', 'ignore')
m = re.search(r_title, data)
if not m:
return phenny.say("No title found.")
title = m.group(1)
2017-08-23 10:56:15 -07:00
title = html.unescape(title).strip()
if len(title) > 150:
title = title[:150] + "[...]"
phenny.reply(title)
2014-07-20 07:13:59 -07:00
title.commands = ['title']
2013-05-09 12:52:55 -07:00
def noteuri(phenny, input):
uri = input.group(1)
setattr(phenny.bot, 'last_seen_uri', uri)
2013-05-09 12:52:55 -07:00
noteuri.rule = r'.*(https?://[^<> "\x01]+).*'
2013-05-09 12:52:55 -07:00
noteuri.priority = 'low'
2014-07-19 12:43:47 -07:00
noteuri.nohook = True
noteuri.thread = False
2013-05-09 12:52:55 -07:00
if __name__ == '__main__':
print(__doc__.strip())