summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsand <daniel@spatof.org>2013-05-13 17:20:47 (GMT)
committer sand <daniel@spatof.org>2013-05-13 17:20:47 (GMT)
commit46fedbedb8b3978a067b3b58d1adfa8a63fb9364 (patch)
treee9934de834c7cdfbde7270645c4ecf299fab2453
parent3d629450c44f0910409ce5cdb07b593e6b0cdb58 (diff)
plugin twitter
-rw-r--r--pinolo/plugins/twitter.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/pinolo/plugins/twitter.py b/pinolo/plugins/twitter.py
new file mode 100644
index 0000000..fb56fb1
--- /dev/null
+++ b/pinolo/plugins/twitter.py
@@ -0,0 +1,61 @@
+# -*- coding: utf-8 -*-
+"""
+ pinolo.plugins.twitter
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ Twitter utilities
+
+ :copyright: (c) 2013 Daniel Kertesz
+ :license: BSD, see LICENSE for more details.
+"""
+import re
+import requests
+import logging
+from pinolo.plugins import Plugin
+from pinolo.tasks import Task
+
+
+log = logging.getLogger()
+
+# re to match a twitter status URL
+r_twitter_status = re.compile(r"https?://twitter.com/(?P<user>[^/]+)/status/(?P<id>\d+)")
+
+# API to retrieve a single tweet
+json_status = "https://api.twitter.com/1/statuses/show.json?id={0}"
+
+
+class TwitterTask(Task):
+ def __init__(self, status_id, event, *args, **kwargs):
+ self.status_id = status_id
+ super(TwitterTask, self).__init__(event, *args, **kwargs)
+
+ def run(self):
+ log.debug("Fetching tweet %s" % self.status_id)
+ response = requests.get(json_status.format(self.status_id))
+ data = response.json()
+
+ try:
+ text = data["text"]
+ username = data["user"]["screen_name"]
+ name = data["user"]["name"]
+ except KeyError:
+ return
+
+ reply = u'"%s" @%s: %s' % (name, username, text)
+ self.put_results(self.event.reply, reply)
+
+
+class TwitterPlugin(Plugin):
+ def on_PRIVMSG(self, event):
+ if event.user.nickname == event.client.current_nickname:
+ return
+
+ match = r_twitter_status.search(event.text)
+ if match is None:
+ return
+ status_url = match.group()
+ username = match.groupdict()["user"]
+ status_id = match.groupdict()["id"]
+
+ t = TwitterTask(status_id, event)
+ t.start()