SpiffyTitles: add Twitter handler

This commit is contained in:
oddluck 2020-06-06 12:54:46 -04:00
parent 7cad84a682
commit ea47c3cea2
3 changed files with 60 additions and 1 deletions

View File

@ -37,7 +37,7 @@ import supybot.world as world
# Use this for the version of this plugin. You may wish to put a CVS keyword
# in here if you're keeping the plugin in CVS or some similar system.
__version__ = "2020.05.21+git"
__version__ = "2020.06.06+git"
# XXX Replace this with an appropriate author or supybot.Author instance.
__author__ = supybot.Author("butterscotchstallion", "butterscotchstallion", "")

View File

@ -652,3 +652,25 @@ conf.registerChannelValue(
_("""Uses Coub API to get additional information about coub.com links"""),
),
)
# twitter configs
conf.registerGroup(SpiffyTitles, "twitter")
# twitter enabler
conf.registerChannelValue(
SpiffyTitles.coub,
"enabled",
registry.Boolean(
True, _("""Whether to add additional information about Twitter links""")
),
)
# twitter template
conf.registerChannelValue(
SpiffyTitles.coub,
"template",
registry.String(
"^ {{text}}",
_("""Uses Twitter API to get additional information about twitter.com links"""),
),
)

View File

@ -78,6 +78,11 @@ class SpiffyTitles(callbacks.Plugin):
self.add_wikipedia_handlers()
self.add_reddit_handlers()
self.add_twitch_handlers()
self.add_twitter_handlers()
def add_twitter_handlers(self):
self.handlers["twitter.com"] = self.handler_twitter
self.handlers["www.twitter.com"] = self.handler_twitter
def add_dailymotion_handlers(self):
self.handlers["www.dailymotion.com"] = self.handler_dailymotion
@ -1649,6 +1654,38 @@ class SpiffyTitles(callbacks.Plugin):
else:
return self.handler_default(url, channel)
def handler_twitter(self, url, info, channel):
if not self.registryValue("twitter.enabled", channel):
return self.handler_default(url, channel)
api_url = "https://publish.twitter.com/oembed?url={0}&omit_script=True".format(
url
)
try:
request = requests.get(api_url, timeout=self.timeout)
request.raise_for_status()
except (
requests.exceptions.RequestException,
requests.exceptions.HTTPError,
) as e:
log.error("SpiffyTitles: Twitter Error: {0}".format(e))
return self.handler_default(url, channel)
response = None
try:
response = json.loads(request.content.decode())
except:
log.error("SpiffyTitles: Error reading Twitter JSON response")
return self.handler_default(url, channel)
results = {}
results["name"] = response["author_name"]
soup = BeautifulSoup(response["html"])
results["text"] = soup.text.replace("", " - ").strip()
template = Template(self.registryValue("twitter.template", channel))
title = template.render(results).strip()
if title:
return title
else:
return self.handler_default(url, channel)
def t(self, irc, msg, args, query):
"""
Retrieves title for a URL on demand