diff options
Diffstat (limited to 'htt.py')
-rwxr-xr-x | htt.py | 148 |
1 files changed, 15 insertions, 133 deletions
@@ -2,134 +2,13 @@ # -*- coding: utf-8 -*- +import argparse import tweepy -from tweepy import OAuthHandler -from tweepy import Stream -from tweepy.streaming import StreamListener from auth import * -from config import hashtags, white_kwds -from db import LikeMemory +from db import open_db +from live import listen_live +from tweepy import OAuthHandler from users import listen_to_users -import json -import sys - - -class StdOutListener(StreamListener): - """A listener handles tweets are the received from the stream.""" - - def __init__(self, api, memory, tempo): - """Build the Python object.""" - - super().__init__() - - self._api = api - self._memory = memory - - self._tempo = tempo - self._previous = None - - self._white = [ s.lower() for s in white_kwds.split(' ') ] - - - def get_status_info(self, data): - """Parse status data to get information about its author and content.""" - - # Do not rely on https://dev.twitter.com/overview/api/tweets - # as the specs seem outdated... - - sid = data['id'] - username = data['user']['screen_name'] - - if 'extended_tweet' in data: - content = data['extended_tweet']['full_text'] - else: - content = data['text'] - - content = content.replace('\n', '') - - return sid, username, content - - - def on_data(self, data): - """Receive Tweets matching the given hashtags.""" - - decoded = json.loads(data) - - if 'retweeted_status' in decoded: - - if not ('id' in decoded['retweeted_status']): - print(decoded) - - sid, username, content = self.get_status_info(decoded['retweeted_status']) - - else: - - if not ('id' in decoded): - print(decoded) - - sid, username, content = self.get_status_info(decoded) - - like = False - - words = content.split(' ') - - for kwd in self._white: - - for w in words: - if w.lower() == kwd: - like = True - break - - if like: - break - - if like: - - if self._memory.is_original_content(content): - - try: - - if self._tempo: - - if self._previous != None: - - self._api.create_favorite(self._previous) - - self._previous = sid - - else: - - self._api.create_favorite(sid) - - # Save even pending statuses to remember them when looking for original content - self._memory.save_liked_status(sid, username, content) - - print('@%s: "%s" (id=%d)' % (username, content, sid)) - print(' -> https://twitter.com/%s/status/%d' % (username, sid)) - - except tweepy.error.TweepError: - - pass - - else: - - print('Already seen "%s"' % content) - - else: - - print('Reject "%s"' % content) - - 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 if __name__ == '__main__': @@ -139,17 +18,20 @@ if __name__ == '__main__': auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) - #memory = LikeMemory(api) - if len(sys.argv) > 1 and sys.argv[1] == '--purge': + open_db() - memory.purge_old_status() + parser = argparse.ArgumentParser() + parser.add_argument('-p', '--purge', help='Delete old liked Tweets', action='store_true') + parser.add_argument('-d', '--daily', help='Analyse contents from selected followers', action='store_true') - else: + args = parser.parse_args() - listen_to_users(auth, api) + if args.purge: + memory.purge_old_status() - #listener = StdOutListener(api, memory, True) + elif args.daily: + listen_to_users(auth, api) - #stream = Stream(auth, listener) - #stream.filter(track=hashtags.split(' ')) + else: + listen_live(auth, api) |