diff --git a/SpiffyTitles/__init__.py b/SpiffyTitles/__init__.py index 9af88e3..23947d7 100644 --- a/SpiffyTitles/__init__.py +++ b/SpiffyTitles/__init__.py @@ -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", "") diff --git a/SpiffyTitles/config.py b/SpiffyTitles/config.py index 2581ed5..1e7b35e 100644 --- a/SpiffyTitles/config.py +++ b/SpiffyTitles/config.py @@ -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"""), + ), +) diff --git a/SpiffyTitles/plugin.py b/SpiffyTitles/plugin.py index 9a42a04..74d9c71 100644 --- a/SpiffyTitles/plugin.py +++ b/SpiffyTitles/plugin.py @@ -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