public
Description: Yahoo Hack Day Project
Homepage:
Clone URL: git://github.com/Fabs/eventomeeter.git
eventomeeter / twitter.py
100644 106 lines (82 sloc) 3.168 kb
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
from urllib import urlopen
import datetime
import pickle
import time
from flickr_api import getFlickers
import webbrowser
 
serie = 0
while (True):
    n = 0
    next_url = 'http://search.twitter.com/search.json?q=brhackday08'
    contador = 0
    posts_na_ultima_hora = 0
    twits = []
    while n<3:
        data = urlopen(next_url).read()
        data = eval(data.replace('null', 'None'))
        next_url = "http://search.twitter.com/search.json"+data['next_page']
        #print "Querying twitter (%s)" % (next_url)
        for post in data['results']:
            #print data['results'][0]['created_at']
            dia = int(post['created_at'].split()[1])
            hora, min, seg = map(int, post['created_at'].split()[4].split(':'))
            now = datetime.datetime.now()
            horario_post = datetime.datetime(now.year, now.month, dia, hora, min, seg) - datetime.timedelta(hours=2)
 
            #print horario_post
            tempo_passado = now - horario_post
            #print tempo_passado
            if (tempo_passado < datetime.timedelta(hours=1)):
                posts_na_ultima_hora +=1
                twits.append((post['from_user'], post['text']))
                print "* %s (%s)" % (tempo_passado, post['from_user'])
            #print "%s (%s)" % (tempo_passado, post['from_user'])
            #print data
            #if (tempo_passado <
            contador += 1
        n+=1
    
    photos = getFlickers()
 
    photos_tratadas = []
    contador = 0
    print "len(photos) = " + str(len(photos))
    for photo in photos:
        #import pdb
        #pdb.set_trace()
        if (int(photo.lastupdate) - time.time()+ 7200 < 3600):
            photos_tratadas.append({'type': 'flickr', 'url': photo.url, 'user': photo.username, 'tags': photo.tags})
            contador += 1
            if contador < 30:
                print "%d" % (int(photo.lastupdate) - time.time())
            
            
    print "contador flickr recentes=%d" % contador
    
    print "posts_na_ultima_hora = %d" % posts_na_ultima_hora
    if (posts_na_ultima_hora >= 0):
        saida_arduino = 1
 
    if (posts_na_ultima_hora > 8):
        saida_arduino = 2
 
    if (posts_na_ultima_hora > 23):
        saida_arduino = 3
 
    if (posts_na_ultima_hora > 32):
        saida_arduino = 4
 
    if (posts_na_ultima_hora > 42):
        saida_arduino = 5
 
    print "Antes dos ifs: contador = %d" % contador
    
    if (contador <= 10):
        taxa_flickr = 200
 
    if (contador > 10):
        taxa_flickr = 150
 
    if (contador > 19):
        taxa_flickr = 100
 
    if (contador > 40):
        taxa_flickr = 50
 
    if (contador >= 90):
        taxa_flickr = 5
 
    print saida_arduino
 
    twits_tratados = []
 
    for twit in twits:
        twits_tratados.append({'type': 'twitter', 'text': twit[1], 'user': twit[0]})
 
    to_pickle = { 'taxa_twitter' : saida_arduino,
                    'taxa_flickr' : taxa_flickr,
                    'items': twits_tratados + photos_tratadas,
                                            }
 
    pickle.dump(to_pickle, file(str(serie) + ".pickle", 'w'))
    serie += 1
    time.sleep(30)