-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_analytics.py
262 lines (203 loc) · 7.85 KB
/
twitter_analytics.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# - *- coding: utf- 8 - *-
import twitter
import flickr_api
import random
import networkx as nx
from flickr_api.api import flickr
import sys, requests, json
# Llaves de API Flickr
FLICKR_API_KEY = "76827ad9eaad1bd3a99714a4d0bddb58"
FLICKR_API_SECRET = "cf97b6510ae41c68"
flickr_api.set_keys(api_key = FLICKR_API_KEY, api_secret = FLICKR_API_SECRET)
# Cantidad de tweets para traer del timeline (max. 200 por api)
tweetCount = 100
userCheck = False
#################################
### Connection to Twitter API ###
#################################
TWITTER_CONSUMER_KEY = "vaIY3GCpoLs2KNZjU0O1OwbKe"
TWITTER_CONSUMER_SECRET = "zMeF2HN5FZukSD2BrlhTiwasvAXw3uG6wssan0RXcEoeApXwoA"
TWITTER_ACCESS_TOKEN_KEY = "34757935-HGoJ1rJjoqyZbpefIoF5XbqJfNgnIUpIJhmeP4c8m"
TWITTER_ACCESS_TOKEN_SECRET = "pgJ8OSOQ3SDZsSL41OiAy79i7qGbxmnB8yHBlNCIzNKwX"
try:
api = twitter.Api(consumer_key = TWITTER_CONSUMER_KEY,
consumer_secret = TWITTER_CONSUMER_SECRET,
access_token_key = TWITTER_ACCESS_TOKEN_KEY,
access_token_secret = TWITTER_ACCESS_TOKEN_SECRET)
except:
print "No se pudo conectar a la API de Twitter, lamentamos las molestias."
sys.exit()
############################
### Check if user exists ###
############################
while not userCheck:
# Si se deja en blanco el input, usa el usuario loggeado (@tommehthecat)
print "Bienvenido a Twitter Analytics!"
print "Escribe 'exit' si quieres salir."
print "Que usuario quieres buscar?"
user = raw_input()
if user == "exit":
print "Hasta luego!"
sys.exit()
if user and not user.startswith('@'):
user = '@' + user
if not user:
try:
foundUser.id = ''
break
except NameError:
print 'Usuario Inválido.'
sys.exit()
try:
foundUser = api.GetUser(screen_name = user)
userCheck = True
print '\n'
except:
print "El usuario {} no fue encontrado, vuelve a intentar.".format(user)
def mostUsedWords( tweets ):
most_used_words = []
for tweet in tweets:
encoded_tweet = tweet.text.encode('utf-8')
tweet_words = str(encoded_tweet).split(' ')
for tweet_word in tweet_words:
used = False
for used_word in most_used_words:
if( tweet_word == used_word['text']):
used_word['repeated'] = used_word['repeated'] + 1
used = True
if not( used ):
new_word = {'text': tweet_word, 'repeated': 1}
if not(new_word['text'].startswith('@')):
most_used_words.append(new_word)
for j in range(1, len(most_used_words)):
key = most_used_words[j]
i = j - 1
while( i >= 0 and most_used_words[i]['repeated'] < key['repeated']):
most_used_words[i + 1] = most_used_words[i]
i = i - 1
most_used_words[i + 1] = key
print 'Las palabras más usadas por {} son:'.format(user)
for i in range(10):
print ('{}.- {}, {} veces.'
.format(i + 1, most_used_words[i]['text'], most_used_words[i]['repeated']))
def findWordIn( word, tweets ):
count = 0
for tweet in tweets:
encoded_tweet = tweet.text.encode('utf-8')
tweet_words = str(encoded_tweet).split(' ')
for tweet_word in tweet_words:
if( tweet_word == word ):
count = count + 1
print 'La palabra {} fue utilizada por {}, {} veces'.format(word, user, count)
def getTrendingTopics():
print 'Mostrando trending topics globales:\n'
trending_topics = api.GetTrendsCurrent()
for topic in trending_topics:
print topic.name
def getLocalTrendingTopics():
send_url = 'http://freegeoip.net/json'
r = requests.get(send_url)
j = json.loads(r.text)
location = flickr.places.findByLatLon(
lat = j['latitude'],
lon = j['longitude'],
format = "json",
nojsoncallback = 1)
n = json.loads(location)
woeid = n['places']['place'][0]['woeid']
place_name = n['places']['place'][0]['name']
print 'Mostrando trending topics cerca de {}:\n'.format(place_name)
trending_topics = api.GetTrendsWoeid(woeid)
for topic in trending_topics:
print topic.name
def getShortestNetwork():
tw_file = file('twitter_combined.txt')
id_found = False
users = set()
for line in tw_file:
line_array = line.split()
for Id in line_array:
users.add(Id)
if str(foundUser.id) in line:
id_found = True
if not id_found:
print "El id de usuario no fue encontrado en la muestra. Elige otro."
return
users = list(users)
query = True
while query:
random_id = random.choice(users)
user = api.GetUser(user_id = random_id).screen_name
print "Elige otro usuario al cual quieres llegar (sugerencia: @{})".format(user)
target_user = raw_input()
if target_user and not target_user.startswith('@'):
target_user = '@' + target_user
try:
secondUser = api.GetUser(screen_name = target_user)
print '\n'
except:
print "El usuario {} no fue encontrado, vuelve a intentar.".format(target_user)
if str(secondUser.id) in users:
query = False
else:
print "El usuario {} no esta en nuestra base de datos, vuelve a intentar.".format(target_user)
print "Calculando camino mas corto..."
G = nx.read_edgelist("twitter_combined.txt", nodetype = int)
nodes = G.nodes()
shortest_path = nx.shortest_path(
G,
source=nodes[nodes.index(foundUser.id)],
target=nodes[nodes.index(secondUser.id)])
print "Consiguiendo el nombre de los usuarios..."
names_shortest_path = list()
for Id in shortest_path:
user = api.GetUser(user_id = Id).screen_name
names_shortest_path.append(user)
print "El camino mas corto del usuario @{} a @{} es: ".format(foundUser.screen_name, secondUser.screen_name)
print '\n'
for name in names_shortest_path:
print "@{}".format(name)
##############################
#### Get timeline from user ###
###############################
timeline = api.GetUserTimeline(user_id = foundUser.id, count = tweetCount)
while True:
print '################################'
print 'Seleccione una de las opciones'
print '1.- Ver los últimos tweets'
print '2.- Ver las palabras mas usadas'
print '3.- Busqueda por palabra'
print '4.- Ver los trending topics globales'
print '5.- Ver los trending topics cercanos'
print '6.- NUEVO! Ver red mas cercana entre un usuario y otro'
print '0.- Salir'
while True:
try:
option = int(input())
break
except (ValueError, NameError):
print 'Oops, intentalo otra vez'
if(option == 1):
print 'Estos son los tweets de {}'.format(user)
for i in range(10):
print '{} .- {}'.format(i+1, timeline[i].text.encode('utf-8'))
print '\n'
elif( option == 2 ):
mostUsedWords(timeline)
elif(option == 3):
print 'Ingrese la palabra para la busqueda.'
word = str(raw_input())
findWordIn(word, timeline)
elif( option == 4):
getTrendingTopics()
elif( option == 5):
getLocalTrendingTopics()
elif(option == 6):
getShortestNetwork()
elif( option == 0 ):
print 'Hasta luego!'
sys.exit()
else:
print 'Opción invalida, adios'
sys.exit()
raw_input("Presiona una tecla para continuar...")