Completed
Push — master ( 668022...7bbb8a )
by W
01:31
created

TwitterStreamHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A on_success() 0 3 2
A on_error() 0 2 1
A __init__() 0 9 1
1
from twython import TwythonStreamer
2
3
from st2reactor.sensor.base import Sensor
4
5
__all__ = [
6
    'TwitterStreamSensor'
7
]
8
9
BASE_URL = 'https://twitter.com'
10
11
12
class TwitterStreamHandler(TwythonStreamer):
13
    def __init__(self, consumer_key, consumer_secret, token, token_secret, callback):
14
        super(TwitterStreamHandler, self).__init__(
15
            consumer_key,
16
            consumer_secret,
17
            token,
18
            token_secret
19
        )
20
21
        self._callback = callback
22
23
    def on_success(self, data):
24
        if 'text' in data:
25
            self._callback(tweet=data)
26
27
    def on_error(self, status_code, data):
28
        print status_code
29
30
31
class TwitterStreamSensor(Sensor):
32
    def __init__(self, sensor_service, config=None):
33
        super(TwitterStreamSensor, self).__init__(
34
            sensor_service=sensor_service,
35
            config=config
36
        )
37
        self._trigger_ref = 'twitter.stream_matched_tweet'
38
        self._logger = self._sensor_service.get_logger(__name__)
39
40
    def setup(self):
41
        pass
42
43
    def run(self):
44
        twitter = TwitterStreamHandler(
45
            self._config['consumer_key'],
46
            self._config['consumer_secret'],
47
            self._config['access_token'],
48
            self._config['access_token_secret'],
49
            self._dispatch_trigger_for_tweet
50
        )
51
        twitter.user()
52
53
    def cleanup(self):
54
        pass
55
56
    def add_trigger(self, trigger):
57
        pass
58
59
    def update_trigger(self, trigger):
60
        pass
61
62
    def remove_trigger(self, trigger):
63
        pass
64
65
    def _dispatch_trigger_for_tweet(self, tweet):
66
        trigger = self._trigger_ref
67
68
        url = '%s/%s/status/%s' % (BASE_URL, tweet['user']['screen_name'], tweet['id'])
69
        payload = {
70
            'id': tweet['id'],
71
            'created_at': tweet['created_at'],
72
            'lang': tweet['lang'],
73
            'place': tweet['place'],
74
            'retweet_count': tweet['retweet_count'],
75
            'favorite_count': tweet['favorite_count'],
76
            'user': {
77
                'screen_name': tweet['user']['screen_name'],
78
                'name': tweet['user']['name'],
79
                'location': tweet['user']['location'],
80
                'description': tweet['user']['description'],
81
            },
82
            'text': tweet['text'],
83
            'url': url
84
        }
85
        self._sensor_service.dispatch(trigger=trigger, payload=payload)
86