-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
57 lines (44 loc) · 1.25 KB
/
run.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
import socket
from time import sleep
from redis import StrictRedis
from main.comment import get_rv
key = 'song_comment'
key_count = 'key_count'
redis_store = StrictRedis(db=1)
def get_song_id_from_redis():
song_id = redis_store.get(key_count)
redis_store.incr(key_count)
return song_id
def store(song_id, comment_num):
if comment_num > 10000:
url = 'http://music.163.com/#/song?id=@'.replace('@', str(song_id))
redis_store.zadd(key, comment_num, url)
def show_rv():
rv = redis_store.zrevrange(key, 0, 10)
for url in rv:
print url
def run():
error_counter = 0
while True:
if error_counter == 10:
error_counter = 0
sleep(10)
song_id = get_song_id_from_redis()
print 'song id: %s' % song_id
if int(song_id) < 5000000:
try:
comment_num = get_rv(song_id)
if comment_num:
store(song_id, comment_num)
sleep(4)
except socket.timeout as e:
error_counter += 1
print e
except Exception as e:
error_counter += 1
print e
else:
break
show_rv()
if __name__ == '__main__':
run()