summaryrefslogtreecommitdiff
path: root/live.py
blob: 9f6ad4c0a62e4570b704380fd40da58cf187e652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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)