-
Notifications
You must be signed in to change notification settings - Fork 0
/
twstreamer.py
executable file
·136 lines (100 loc) · 3.16 KB
/
twstreamer.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#! /usr/bin/env python2.7
# coding: utf-8
import sys, os
import tweepy
from datetime import datetime
import re
CONSUMER_KEY = 'dvcNxv04JRtsTpXdqsT4YNwoQ'
CONSUMER_SECRET = 'vZheuS46AYjue0sYPq9f02jCEEpBD6xbOLZteZv3hrp0q3kX3N'
ACCESS_FILEPATH = "/tmp/.access"
class StdOutListener(tweepy.StreamListener):
""" A listener handles tweets are the received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def __init__(self, formater):
tweepy.StreamListener.__init__(self)
self.formater = formater
def on_status(self, status):
print self.formater(status)
print ""
return True
def on_error(self, status):
pass
def save_key_secret(key, secret):
acc = open(ACCESS_FILEPATH, "w+")
acc.write("access_key=%s\n" % key)
acc.write("access_secret=%s" % secret)
acc.close()
def retrieve_access():
acc = open(ACCESS_FILEPATH, "r+")
access_key = acc.readline().split("=")[1]
access_secret = acc.readline().split("=")[1]
acc.close()
return access_key.strip(), access_secret.strip()
def has_grant():
if not os.path.isfile(ACCESS_FILEPATH):
return False
key, secret = retrieve_access()
if not key or not secret:
return False
return True
def request_user_for_pin(url):
print 'From your browser, please click AUTHORIZE APP and then copy the unique PIN: '
print url
verifier = raw_input('PIN: ').strip()
auth.get_access_token(verifier)
return auth.access_token.key, auth.access_token.secret
def start_listening(auth):
l = StdOutListener(tt_formatter)
stream = tweepy.Stream(auth, l)
stream.userstream()
def tt_formatter(status):
time = status.created_at.strftime("%H:%M")
user = status.author.name.encode("utf-8")
tt = status.text.strip("\n \t").encode("utf-8")
tt = mark_tt_simbols(tt)
out_string = template_string(spacer="\n").encode("utf-8")
return out_string.format(user=user, time=time, text=tt)
def template_string(c_user="\033[1;37;44m", c_text="\033[0m", c_time="\033[2;37m", spacer=" "):
return "{c_user}@{user}\033[0m{spacer}{c_text}{text}\033[0m{spacer}{c_time}{time}\033[0m".format(
c_user=c_user,
c_text=c_text,
c_time=c_time,
spacer=spacer,
user="{user}",
text="{text}",
time="{time}"
)
def mark_tt_simbols(text):
formated_text = ""
simbols = text.split()
for simbol in simbols:
if is_hash(simbol):
simbol = "\033[1;35m%s" % simbol
elif is_url(simbol):
simbol = "\033[1;32m%s" % simbol
elif is_usr(simbol):
simbol = "\033[1;34m%s" % simbol
simbol += "\033[0m"
formated_text += " %s" % simbol
formated_text = formated_text.strip()
return formated_text
def is_hash(simb):
reg = re.compile("\#\w+")
return bool(reg.search(simb))
def is_url(simb):
reg = re.compile("(http:\/\/)?([\w_-]+\.)+[\w]{2,3}(\/[^ \t]+)?")
return bool(reg.search(simb))
def is_usr(simb):
reg = re.compile("@[\w_]+")
return bool(reg.search(simb))
if __name__ == "__main__":
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
if not has_grant():
auth_url = auth.get_authorization_url()
key, secret = request_user_for_pin(auth_url)
save_key_secret(key, secret)
else:
key, secret = retrieve_access()
auth.set_access_token(key, secret)
start_listening(auth)