Commits

pval...@pvalente-laptop  committed fb7c7be

Resolvido o bug #1

  • Participants
  • Parent commits 1dce317

Comments (0)

Files changed (3)

 import re
 import simplejson
 import time
+import cPickle
 from twitter import twitter
 
 twitter_username = 'fasassim'
 twitter_password = 'abc123'
 
+def get_friends_list(api, force = False):
+    try:
+        friends_list = cPickle.load(open('friends.pickle'))
+    except IOError:
+        force = True
+    if force:
+        friends = api.GetFriends()
+        friends_list = [f.screen_name for f in friends]
+        cPickle.dump(friends_list, open('friends.pickle','w'))
+    return friends_list
+
+def add_friend(api, screen_name):
+    new_friend = api.CreateFriendship(screen_name)
+    print "twitter.fasassim.following %s" % new_friend.name
+
+def get_full_feed():
+    feed = urllib.urlopen('http://search.twitter.com/search.json?q=comofas')
+    updates = simplejson.loads(feed.read())
+    cPickle.dump(updates, open('updates.pickle','w'))
+    return updates
+
 def get_last_feed():
-    feed = urllib.urlopen('http://search.twitter.com/search.json?q=comofas')
-    updates = feed.read()
-    updates = simplejson.loads(updates)
-
+    updates = get_full_feed()
     return updates['results'][0]['text'].lower(), updates['results'][0]['from_user']
 
 def clean_text(txt):
 
     api = twitter.Api(username=twitter_username, password=twitter_password)
     api.SetXTwitterHeaders('fasassim',"http://twitter.com/fasassim",'Ni!')
-
-    ultimos_usuarios={}
+    
+    friends_list = get_friends_list(api)   
+    print "Sigo %d amigos" % len(friends_list)
+    
+    
     ultima_pergunta=''
     while (True):
-        time.sleep(random.randint(15,30)*2)
+        intervalo = random.randint(20,40)*2
+        print "Tempo: %d segundos..." % intervalo
+        time.sleep(intervalo)
+        feed = get_full_feed()
+        
+        #checa se no feed tem pessoas novas
+        new_people = False
+        for update in feed['results']:
+            if update['from_user'] not in friends_list:
+                new_people = True
+                add_friend(api, update['from_user'])
+        if new_people:
+            friends_list = get_friends_list(api, force=True)
+        
+        #TODO Iterar do último item do feed para o primeiro, checando se já foi respondido.
+        #     Se não foi, responde e grava em algum lugar update_id e flag_respondida
+        #     Ignorar updates mais antigos do que 1 ou 2 horas
         f = get_last_feed()
         pergunta = f[0]
         usuario = f[1]
         follow_counter=0
 
-        ultimos_usuarios[usuario]=1
-
         if ultima_pergunta == pergunta:
             continue
         else:
                 #nao_entendi(usuario)
                 continue
 
-            follow_counter = follow_counter + 1
-
-            if follow_counter==5:
-                follow_counter = 0
-                added_friends = api.GetFriends()
-
-                for friend in added_friends:
-                    if not ultimos_usuarios.has_key(friend.name) and usuario != 'fasassim':
-                        api.CreateFriendship(friend)
-                        print "twitter.fasassim.following %s" % friend.name
-                        ultimos_usuarios.pop(friend.name)
-                ultimos_usuarios={}
 
             if questions:
                 for question in questions:
                         print '-' * 50
                         print "twitter.fasassim postUpdate: %s" % mensagem
                         status = api.PostUpdate(mensagem)
-                        ultimos_usuarios[usuario]=1
+
                     except UnicodeEncodeError:
                         continue
             else:

File friends.pickle

+(lp1
+Vwal_limoli
+p2
+aVthaismello
+p3
+aVAnanista
+p4
+aVlimareis
+p5
+aVkxxx
+p6
+aVsweethome
+p7
+aVmarianneabreu
+p8
+aVphonetrio
+p9
+aVmari_onete
+p10
+aVfelipelobo
+p11
+aVleeiri
+p12
+aVbrunocaimar
+p13
+aVmyris
+p14
+aVfelipesantini
+p15
+aVglacial
+p16
+aValegomes
+p17
+aVnatalianambara
+p18
+aVfranksands
+p19
+aVerikaluthor
+p20
+aVelimafra
+p21
+aVfelipc
+p22
+aVpathaddad
+p23
+aVgustavolemos
+p24
+aVlidifaria
+p25
+aVmob_igormaia
+p26
+aVandersonwyzard
+p27
+aVnavelar
+p28
+aVfracazo
+p29
+aVTamosauskas
+p30
+aVandressafabris
+p31
+aVfunxo
+p32
+aVjpferreira
+p33
+aVvinisko
+p34
+aVlenonm
+p35
+aVmeuretrato
+p36
+aVfugita
+p37
+aVnandop
+p38
+aVlpouzada
+p39
+aVJimmyAndrade
+p40
+aVnitzoca
+p41
+aVInFog9
+p42
+aVrpmarsolla
+p43
+aVmagalyprado
+p44
+aVguzzmartins
+p45
+aVmanoelbr
+p46
+aVcarrapatoso
+p47
+aVmauro_baraldi
+p48
+aVcmilfont
+p49
+aVjonnyken
+p50
+aVleopinheiro
+p51
+aVgcirne
+p52
+aVhebertr
+p53
+aVpellegrino
+p54
+aVdonalilian
+p55
+aVbrunocarvalho
+p56
+aVyogodoshi
+p57
+aVpac_man
+p58
+aVmserrano
+p59
+aVmichellets
+p60
+aVedsonlcj
+p61
+aVAmendoeira
+p62
+aVandyzeo
+p63
+aVgilbertojr
+p64
+aVklaudin
+p65
+aVlucieide
+p66
+aVsahelizabeth
+p67
+aVcahcarola
+p68
+aVfernandoluz
+p69
+aVrbp
+p70
+aVrbernardelli
+p71
+aVpergunta
+p72
+aVdivless
+p73
+aVtiedyepoa
+p74
+aValined
+p75
+aVsilviotanaka
+p76
+aVloveology_x
+p77
+aVLiliabdalla
+p78
+aVeshiota
+p79
+aValickel
+p80
+aVgunter77
+p81
+aVJuzix
+p82
+aVigordrey
+p83
+aVmarkun
+p84
+aVpedroturambar
+p85
+aVdanielcassiano
+p86
+aVsergiomaia
+p87
+aVlucmult
+p88
+aVboechat
+p89
+aVmeninaquejoga
+p90
+aVandressa_r
+p91
+aVpcalcado
+p92
+aViarana
+p93
+aVCronicaNet
+p94
+aVwillianmax
+p95
+aVmoskito
+p96
+aVparsifall
+p97
+aVpeleteiro
+p98
+aVmundosdalua
+p99
+aVvrsmn
+p100
+aVbiasouza
+p101
+a.

File updates.pickle

+(dp1
+Vnext_page
+p2
+V?page=2&max_id=1001446080&q=comofas
+p3
+sVrefresh_url
+p4
+V?since_id=1001446080&q=comofas
+p5
+sVresults
+p6
+(lp7
+(dp8
+Viso_language_code
+p9
+Vpt
+p10
+sVtext
+p11
+VConsigo n�o, universo. Comofas aqui com gal�xias de medos eternos? EIN? Porra, meu.
+p12
+sVcreated_at
+p13
+VWed, 12 Nov 2008 02:34:18 +0000
+p14
+sVprofile_image_url
+p15
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/59994655/grayside_normal.jpg
+p16
+sVfrom_user
+p17
+Vwal_limoli
+p18
+sVfrom_user_id
+p19
+I1609096
+sVto_user_id
+p20
+NsVid
+p21
+I1001446080
+sa(dp22
+Viso_language_code
+p23
+Vpt
+p24
+sVtext
+p25
+Vtop top mtv foi tudo, comofas ter pais mega influentes///
+p26
+sVcreated_at
+p27
+VWed, 12 Nov 2008 02:33:14 +0000
+p28
+sVprofile_image_url
+p29
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/56772187/praiafaixagrandecortada_normal.jpg
+p30
+sVfrom_user
+p31
+Vthaismello
+p32
+sVfrom_user_id
+p33
+I980877
+sVto_user_id
+p34
+NsVid
+p35
+I1001444533
+sa(dp36
+Viso_language_code
+p37
+Vpt
+p38
+sVtext
+p39
+VQuando eu morrer, quero deixar o meu corpo pra ci�ncia #comofas?
+p40
+sVcreated_at
+p41
+VWed, 12 Nov 2008 02:12:25 +0000
+p42
+sVprofile_image_url
+p43
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/58434963/Ananias_normal.jpg
+p44
+sVfrom_user
+p45
+VAnanista
+p46
+sVfrom_user_id
+p47
+I267892
+sVto_user_id
+p48
+NsVid
+p49
+I1001421324
+sa(dp50
+Viso_language_code
+p51
+Vpt
+p52
+sVtext
+p53
+VQuero aprender mandarim, #comofas?
+p54
+sVcreated_at
+p55
+VWed, 12 Nov 2008 01:32:20 +0000
+p56
+sVprofile_image_url
+p57
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/63884836/avatar2_normal.png
+p58
+sVfrom_user
+p59
+Vlenonm
+p60
+sVfrom_user_id
+p61
+I191412
+sVto_user_id
+p62
+NsVid
+p63
+I1001371857
+sa(dp64
+Viso_language_code
+p65
+Vpt
+p66
+sVtext
+p67
+Vcomofas pra ligar pro afeganist�o ?
+p68
+sVcreated_at
+p69
+VWed, 12 Nov 2008 01:26:18 +0000
+p70
+sVprofile_image_url
+p71
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/60297300/BRAZIL_by_Oster_normal.jpg
+p72
+sVfrom_user
+p73
+Vbrunocaimar
+p74
+sVfrom_user_id
+p75
+I609851
+sVto_user_id
+p76
+NsVid
+p77
+I1001364783
+sa(dp78
+Viso_language_code
+p79
+Vpt
+p80
+sVtext
+p81
+Vcomofas pra isso funcionar ?
+p82
+sVcreated_at
+p83
+VWed, 12 Nov 2008 01:23:59 +0000
+p84
+sVprofile_image_url
+p85
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/60297300/BRAZIL_by_Oster_normal.jpg
+p86
+sVfrom_user
+p87
+Vbrunocaimar
+p88
+sVfrom_user_id
+p89
+I609851
+sVto_user_id
+p90
+NsVid
+p91
+I1001361868
+sa(dp92
+Viso_language_code
+p93
+Vpt
+p94
+sVtext
+p95
+VO som no Debian t� muito baixo, j� reinstalei o Alsa e nada... #comofas?
+p96
+sVcreated_at
+p97
+VWed, 12 Nov 2008 01:16:16 +0000
+p98
+sVprofile_image_url
+p99
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/63884836/avatar2_normal.png
+p100
+sVfrom_user
+p101
+Vlenonm
+p102
+sVfrom_user_id
+p103
+I191412
+sVto_user_id
+p104
+NsVid
+p105
+I1001351859
+sa(dp106
+Viso_language_code
+p107
+Vpt
+p108
+sVtext
+p109
+V@luizaaltino comofas/ O__O'
+p110
+sVcreated_at
+p111
+VWed, 12 Nov 2008 01:07:39 +0000
+p112
+sVprofile_image_url
+p113
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/64068242/IMG_4095_normal.JPG
+p114
+sVto_user
+p115
+Vluizaaltino
+p116
+sVfrom_user
+p117
+Vleeiri
+p118
+sVfrom_user_id
+p119
+I1954396
+sVto_user_id
+p120
+I1674872
+sVid
+p121
+I1001338478
+sa(dp122
+Viso_language_code
+p123
+Vpt
+p124
+sVtext
+p125
+Vfudido de grana, mas ainda quero comprar livros. #comofas?
+p126
+sVcreated_at
+p127
+VWed, 12 Nov 2008 01:04:29 +0000
+p128
+sVprofile_image_url
+p129
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/57375017/eye_normal.jpg
+p130
+sVfrom_user
+p131
+Vfelipelobo
+p132
+sVfrom_user_id
+p133
+I136748
+sVto_user_id
+p134
+NsVid
+p135
+I1001333211
+sa(dp136
+Viso_language_code
+p137
+Ven
+p138
+sVtext
+p139
+VRadiohead no Chile e crise, comofas//
+p140
+sVcreated_at
+p141
+VWed, 12 Nov 2008 01:02:34 +0000
+p142
+sVprofile_image_url
+p143
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/59808984/eu_normal.jpg
+p144
+sVfrom_user
+p145
+Vmari_onete
+p146
+sVfrom_user_id
+p147
+I1561967
+sVto_user_id
+p148
+NsVid
+p149
+I1001329908
+sa(dp150
+Viso_language_code
+p151
+Vpt
+p152
+sVtext
+p153
+Vquero malhar, falar espanhol e morar fora, comofas?
+p154
+sVcreated_at
+p155
+VWed, 12 Nov 2008 01:00:14 +0000
+p156
+sVprofile_image_url
+p157
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/60771701/parque_normal.jpg
+p158
+sVfrom_user
+p159
+Vphonetrio
+p160
+sVfrom_user_id
+p161
+I2185576
+sVto_user_id
+p162
+NsVid
+p163
+I1001326064
+sa(dp164
+Viso_language_code
+p165
+Vpt
+p166
+sVtext
+p167
+V@alegomes Se te consola: meu mac caiu no ch�o. Arrumei com ajuda do http://www.ifixit.com/, mas perdi o drive de CD/DVD. #comofas now?
+p168
+sVcreated_at
+p169
+VWed, 12 Nov 2008 00:41:38 +0000
+p170
+sVprofile_image_url
+p171
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/61182697/orkut_normal.jpg
+p172
+sVto_user
+p173
+Valegomes
+p174
+sVfrom_user
+p175
+Vmarianneabreu
+p176
+sVfrom_user_id
+p177
+I508853
+sVto_user_id
+p178
+I65808
+sVid
+p179
+I1001296479
+sa(dp180
+Viso_language_code
+p181
+Vpt
+p182
+sVtext
+p183
+V#comofas?  =/
+p184
+sVcreated_at
+p185
+VWed, 12 Nov 2008 00:39:57 +0000
+p186
+sVprofile_image_url
+p187
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/39443812/DSC02790_normal.JPG
+p188
+sVfrom_user
+p189
+Vsweethome
+p190
+sVfrom_user_id
+p191
+I179059
+sVto_user_id
+p192
+NsVid
+p193
+I1001294198
+sa(dp194
+Viso_language_code
+p195
+Vpt
+p196
+sVtext
+p197
+Vcomofas quando eu nao entendo? sem girias de truts, pls. jaajosdkfd
+p198
+sVcreated_at
+p199
+VWed, 12 Nov 2008 00:27:38 +0000
+p200
+sVprofile_image_url
+p201
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/59365849/qqq_normal.JPG
+p202
+sVfrom_user
+p203
+Vkxxx
+p204
+sVfrom_user_id
+p205
+I1482969
+sVto_user_id
+p206
+NsVid
+p207
+I1001279713
+sa(dp208
+Viso_language_code
+p209
+Vpt
+p210
+sVtext
+p211
+VPrecisava urgeeeeente colocar mais mem�ria nesse PC, mas dia 30 anivers�rio da m�e... #comofas///1/'^~
+p212
+sVcreated_at
+p213
+VWed, 12 Nov 2008 00:25:37 +0000
+p214
+sVprofile_image_url
+p215
+Vhttp://s3.amazonaws.com/twitter_production/profile_images/58560816/yo_normal.jpg
+p216
+sVfrom_user
+p217
+Vlimareis
+p218
+sVfrom_user_id
+p219
+I11422
+sVto_user_id
+p220
+NsVid
+p221
+I1001277433
+sasVsince_id
+p222
+I0
+sVresults_per_page
+p223
+I15
+sVquery
+p224
+Vcomofas
+p225
+sVmax_id
+p226
+I1001446080
+sVpage
+p227
+I1
+s.