Created
November 7, 2011 00:44
-
-
Save mansilla/1343914 to your computer and use it in GitHub Desktop.
code for fetching a net of followers around a user...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usando tweepy para obetener la red de seguidores de nivel k de un sujeto en twitter. | |
# Created by Ricardo Mansilla at November 6, 2011. | |
#===================================================================================== | |
import sys | |
import tweepy | |
import webbrowser | |
import time | |
#======= SETUP =========== | |
# Nick de twitter del sujeto. | |
screen_name = 'jhonsmith' | |
#=========================== | |
# Niveles | |
nivel = 0 # no esta implementado el asunto de los niveles aun | |
#=========================== | |
output = open('circulo_de_'+screen_name+'.txt','w') | |
seg_file = open('red_seguidores_plain'+screen_name+'.txt','w') | |
key = 'XXXXXXXXX' | |
secret = 'YYYYYYYYYYYYY' | |
CONSUMER_KEY = 'ZZZZZZZZZZZ' | |
CONSUMER_SECRET = 'WWWWWWWWWWWWWWWW' | |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
auth.set_access_token(key, secret) | |
api = tweepy.API(auth) | |
# verifying the api | |
if (api.verify_credentials()): | |
print 'Instancia de la API existosa.' | |
print 'Obteniendo datos del sujeto .', | |
time.sleep(1) | |
sujeto = api.get_user(id=screen_name) | |
print '. . :)' | |
time.sleep(1) | |
print 'Sujeto en cuestion: '+str(sujeto.name) | |
print 'Twitter ID: '+ str(sujeto.id) | |
print 'Descripcion: '+sujeto.description | |
else: | |
print 'Construccion de la instancia de la API fallida. o_o? >>> :(' | |
#====================== | |
# Obteniendo el circulo | |
#====================== | |
print 'Obteniendo los datos el Circulo' | |
seg_seg = [] | |
nivel1 = tweepy.Cursor(api.followers_ids,id=screen_name) | |
cont1 = 0 | |
cont2 = 1 | |
for paglev1 in nivel1.pages(): | |
for it1 in paglev1: | |
cont1+=1 | |
cont2+=1 | |
output.write(str(it1)+' ') | |
thiseg = [it1] | |
print 'Oteniendo datos del seguidor '+str(cont1)+'...' | |
cursor = -1 | |
while True: | |
page = api.friends_ids(id=api.get_user(user_id=it1).screen_name,cursor=cursor) | |
if page[1][1]: | |
cursor = page[1][1] | |
for it2 in page: | |
output.write(str(it2)+' ') | |
thiseg.append(it2) | |
cont2+=1 | |
else: | |
for it2 in page: | |
output.write(str(it2)+' ') | |
thiseg.append(it2) | |
cont2+=1 | |
break | |
seg_seg.append(thiseg) | |
output.write('\n') | |
seg_file.write(str(thiseg)) | |
print 'Terminamos de obtener los datos,', | |
print 'la red consta de '+str(cont2)+'individuos.' | |
print '=======================================' | |
print '---------------------------------------' | |
print '=======================================\n' | |
print 'Construyendo la matriz de conexionese imprimiendo a archivo...' | |
for lista in seg_seg: | |
j=1 | |
while j < len(lista): | |
individuo = lista[j] | |
output.write(str(individuo)+' ') | |
# Ahora hay que ver si este individio esta en otra lista | |
for elem in seg_seg: | |
try: | |
temp = elem.index(individuo) | |
output.write(str(elem[1])+' ') | |
except ValueError: | |
pass | |
j+=1 | |
output.write('\n') | |
output.close() | |
seg_file.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$python getting_deeper.py | |
Instancia de la API existosa. | |
Obteniendo datos del sujeto . . . :) | |
Sujeto en cuestion: jhon smith | |
Twitter ID: xxxxx | |
Descripcion: | |
Oteniendo datos del seguidor 1... | |
Oteniendo datos del seguidor 2... | |
Oteniendo datos del seguidor 3... | |
Oteniendo datos del seguidor 4... | |
Oteniendo datos del seguidor 5... | |
Oteniendo datos del seguidor 6... | |
Oteniendo datos del seguidor 7... | |
Oteniendo datos del seguidor 8... | |
Oteniendo datos del seguidor 9... | |
Oteniendo datos del seguidor 10... | |
Oteniendo datos del seguidor 11... | |
Oteniendo datos del seguidor 12... | |
Traceback (most recent call last): | |
File "getting_deeper.py", line 63, in <module> | |
page = api.friends_ids(id=api.get_user(user_id=it1).screen_name,cursor=cursor) | |
File "build/bdist.macosx-10.6-intel/egg/tweepy/binder.py", line 185, in _call | |
File "build/bdist.macosx-10.6-intel/egg/tweepy/binder.py", line 168, in execute | |
tweepy.error.TweepError: Not authorized |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment