-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathuser.py
81 lines (66 loc) · 2.53 KB
/
user.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
from .workers import app
from db.dao import SeedidsOper
from page_get import (get_fans_or_followers_ids, get_profile, get_user_profile,
get_newcard_by_name)
from logger import crawler
from celery.exceptions import SoftTimeLimitExceeded
@app.task(ignore_result=True)
def crawl_follower_fans(uid):
seed = SeedidsOper.get_seed_by_id(uid)
if seed.other_crawled == 0:
rs = get_fans_or_followers_ids(uid, 1, 1)
rs.extend(get_fans_or_followers_ids(uid, 2, 1))
datas = set(rs)
# If data already exits, just skip it
if datas:
SeedidsOper.insert_seeds(datas)
SeedidsOper.set_seed_other_crawled(uid)
@app.task(ignore_result=True)
def crawl_person_infos(uid):
"""
Crawl user info and their fans and followers
For the limit of weibo's backend, we can only crawl 5 pages of the fans and followers.
We also have no permissions to view enterprise's followers and fans info
:param uid: current user id
:return: None
"""
if not uid:
return
try:
user, is_crawled = get_profile(uid)
# If it's enterprise user, just skip it
if user and user.verify_type == 2:
SeedidsOper.set_seed_other_crawled(uid)
return
# Crawl fans and followers
if not is_crawled:
app.send_task('tasks.user.crawl_follower_fans', args=(uid,), queue='fans_followers',
routing_key='for_fans_followers')
# By adding '--soft-time-limit secs' when you start celery, this will resend task to broker
# e.g. celery -A tasks.workers -Q user_crawler worker -l info -c 1 --soft-time-limit 10
except SoftTimeLimitExceeded:
crawler.error("user SoftTimeLimitExceeded uid={uid}".format(uid=uid))
app.send_task('tasks.user.crawl_person_infos', args=(uid, ), queue='user_crawler',
routing_key='for_user_info')
@app.task(ignore_result=True)
def crawl_person_infos_not_in_seed_ids(uid):
"""
Crawl user info not in seed_ids
"""
if not uid:
return
get_user_profile(uid)
def crawl_person_infos_by_name(name):
"""
Crawl user info by name
"""
if not name:
return False
user, is_crawled = get_newcard_by_name(name)
@app.task(ignore_result=True)
def execute_user_task():
seeds = SeedidsOper.get_seed_ids()
if seeds:
for seed in seeds:
app.send_task('tasks.user.crawl_person_infos', args=(seed.uid,), queue='user_crawler',
routing_key='for_user_info')