summaryrefslogtreecommitdiff
path: root/live.py
diff options
context:
space:
mode:
Diffstat (limited to 'live.py')
-rw-r--r--live.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/live.py b/live.py
new file mode 100644
index 0000000..9f6ad4c
--- /dev/null
+++ b/live.py
@@ -0,0 +1,98 @@
+#!/usr/bin/python3
+# -*- coding: utf-8 -*-
+
+
+from config import hashtags, underlined
+from db import LikeMemory
+from taste import analyse
+from tweepy import Stream
+from tweepy.streaming import StreamListener
+
+
+CACHE_SIZE = 1000 #10000
+
+
+class StdOutListener(StreamListener):
+ """A listener handles tweets are the received from the stream."""
+
+ def __init__(self, api):
+ """Build the Python object."""
+
+ super().__init__()
+
+ self._api = api
+ self._memory = LikeMemory(api)
+
+ self._cache = []
+
+ self._tweets_reviewed = 0
+ self._tweets_liked = 0
+
+
+ def on_status(self, status):
+ """Receive Tweets matching the given filter."""
+
+ sid = status.id
+ username = status.author.screen_name
+ displayed = status.author.name
+
+ if hasattr(status, 'lang'):
+ lang = status.lang
+ else:
+ lang = 'unknown'
+
+ while hasattr(status, 'retweeted_status'):
+ status = status.retweeted_status
+
+ cached = [ sid, username, displayed, lang, status.text ]
+ self._cache.insert(0, cached)
+
+ csize = len(self._cache)
+
+ if csize <= CACHE_SIZE:
+
+ if csize % 50 == 0:
+ print('[*] Cache size: %u...' % csize)
+
+ else:
+
+ sid, username, displayed, lang, text = self._cache.pop()
+
+ liked = analyse(sid, username, displayed, lang, text, self._api, self._memory)
+
+ self._tweets_reviewed += 1
+
+ if liked:
+ self._tweets_liked += 1
+
+ if self._tweets_reviewed % 50 == 0:
+ print('[*] Seen and analyzed %u tweet%s, liked %u tweet%s...' \
+ % (self._tweets_reviewed, 's' if self._tweets_reviewed > 1 else '', \
+ self._tweets_liked, 's' if self._tweets_liked > 1 else ''))
+
+ return True
+
+
+ def on_error(self, code):
+ """Handle errors."""
+
+ print('Error:', code)
+
+ if code == 420:
+ #returning False in on_data disconnects the stream
+ return False
+
+
+def listen_live(auth, api):
+ """Track all tweets written by users."""
+
+ lst = hashtags.split(' ') + underlined.split(' ') + [ '#re' ]
+
+ targets = [ s.lower().replace('_', ' ') for s in lst ]
+
+ listener = StdOutListener(api)
+
+ print('targets = ', targets)
+
+ stream = Stream(auth, listener)
+ stream.filter(track=targets)