-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsl.py
267 lines (229 loc) · 9.42 KB
/
jsl.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# !/usr/bin/env python
# -*- coding: utf-8 -*-
from pyquery import PyQuery as pq
from requests import Session, adapters
import re, hashlib, time
from urllib.parse import unquote
from model import Users, Posts, Replies, Topics, TopicUser, Provs, Industry
from logbook import Logger, StreamHandler, RotatingFileHandler
import sys
log_format = '[{record.time:%Y-%m-%d %H:%M:%S}] {record.level_name}::{record.channel}[{record.module}]:{record.lineno} - {record.message}'
StreamHandler(sys.stdout, format_string=log_format, level='DEBUG').push_application()
RotatingFileHandler('log.log', format_string=log_format, max_size=1024*1024*64, backup_count=5, bubble=True).push_application()
log = Logger(__file__)
HEADERS = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36'
}
def gen_id(some):
return int(hashlib.sha1(some.encode()).hexdigest(), 16) % (10 ** 8)
def retry(times):
def decorator(func):
def wrapper(*args, **kwargs):
attempt = 0
while attempt < times:
try:
return func(*args, **kwargs)
except Exception as err:
log.error('@@@ Error %s @@@ RETRY %s TIMES ' % (err, attempt))
attempt += 1
if attempt == times:
raise err
time.sleep(2 * times)
return wrapper
return decorator
class FetchPost(object):
def __init__(self):
self.__session = Session()
self.__session.mount('https://', adapters.HTTPAdapter(max_retries=3))
def __extract_users(self, dollar):
users = {}
for u in dollar('a.aw-user-name'):
user = {
pq(u).attr('data-id'): {
'id': pq(u).attr('data-id'),
'name': pq(u).text(),
'linkname': unquote(pq(u).text().split('/')[-1])
}
}
users.update(user)
return users
def __extract_posts(self, pid, dollar):
last_actived_at, views, focus = None, None, None
if dollar('.aw-side-bar-mod-body li span'):
last_actived_at, views, focus = [pq(i).text() for i in dollar('.aw-side-bar-mod-body li span')]
else:
log.warn('closed Post - pid: %s' % pid)
post = {
'id': pid,
'user': dollar('a.aw-user-name').attr('data-id'),
'title': dollar('.aw-mod-head h1').text(),
'content': dollar('.aw-mod-body .aw-question-detail-txt').remove('div').text().strip(),
'updated_at': dollar('.aw-question-detail-meta span.pull-left').text(),
'views': views,
'focus': focus,
'last_actived_at': last_actived_at
}
return post
def __extract_replies(self, pid, dollar):
replies = {}
ele = dollar('.aw-mod-body.aw-dynamic-topic .aw-item')
if not ele:
log.warn('no replies - pid: %s' % pid)
return
for r in ele:
rid = pq(r).attr('id').split('_')[-1]
reply = {
rid: {
'id': rid,
'post': pid,
'content': pq(r)('.markitup-box').text(),
'updated_at': pq(r)('.aw-dynamic-topic-meta span.pull-left').text(),
'user': pq(r)('.aw-user-name').attr('data-id')
}
}
replies.update(reply)
return replies
@retry(3)
def single(self, pid):
log.info('start fetching post - pid: %s' % pid)
resp = self.__session.get('https://www.jisilu.cn/question/%s' % pid, headers=HEADERS)
dollar = pq(resp.content)
if('问题不存在或已被删除'.encode()) in resp.content:
log.warn('deleted post - pid: %s' % pid)
return
users = self.__extract_users(dollar)
if users:
Users.insert_many(users.values()).on_conflict('IGNORE').execute()
else:
log.error('NO USER!!! - pid: %s' % pid)
post = self.__extract_posts(pid, dollar)
Posts.insert(post).on_conflict('IGNORE').execute()
replies = self.__extract_replies(pid, dollar)
if replies:
Replies.insert_many(replies.values()).on_conflict('IGNORE').execute()
return post
def multi(self, op=1, ed=260052):
for pid in range(op, ed):
self.single(pid)
class FetchUser(object):
def __init__(self):
self.__session = Session()
self.__session.mount('https://', adapters.HTTPAdapter(max_retries=3))
def __parse_last_signin(self, dollar):
for elem in dollar('.aw-user-center-details dl'):
if '最后活跃' in pq(elem)('dt span').text():
return pq(elem)('dd').text()
def __extract_user_details(self, dollar):
prov = dollar('i.i-user-locate + a').text().strip() or None
prov_id = None
if prov:
prov_id = gen_id(prov)
Provs.insert({
'id': prov_id,
'prov': prov
}).on_conflict('IGNORE').execute()
industry = dollar('i.i-user-post').parent().text().strip() or None
industry_id = None
if industry:
industry_id = gen_id(industry)
Industry.insert({
'id': industry_id,
'industry': industry
}).on_conflict('IGNORE').execute()
lvs = {
'VIP': 0,
'活跃用户': 1,
'普通用户': 2,
'已注销': -1,
# '非信任用户': 1
# '封禁用户': 0
}
lvk = dollar('.aw-mod-body .aw-user-center-follow-meta > span a em')
visits_ele = re.findall('\d+', (dollar('i.i-user-visits').parent().text()))
if not visits_ele:
return None
details = {
'name': dollar('.aw-mod-body .aw-user-title +h1').remove('img').text(),
'signature': dollar('.aw-mod-body .aw-user-title + h1 + span').text().strip(),
'prestige': dollar('.aw-mod-body .aw-user-center-follow-meta .i-user-prestige + em').text(),
'approve': dollar('.aw-mod-body .aw-user-center-follow-meta .i-user-approve + em').text(),
'thank': dollar('.aw-mod-body .aw-user-center-follow-meta .i-user-thank + em').text(),
'coins': dollar('.aw-mod-body .aw-user-center-follow-meta i[style] + em').text().strip('+'),
'industry': industry_id,
'prov': prov_id,
'visits': visits_ele[0],
'locate': dollar('i.i-user-locate + a + a').text().strip() or None,
'last_signin_at': self.__parse_last_signin(dollar),
'level': lvs.get(lvk.text().strip(' »'))
}
return details
def __extract_topics(self, uid, dollar):
topics, topicusers = [], []
elem = dollar('dd.aw-user-center-details-good-topic > div')
for e in elem:
tid = pq(e)('a').attr('data-id')
topic = unquote(pq(e)('a').attr('href').split('/')[-1])
approve, thank = pq(e).remove('a')('span').text().split(' ')
topics.append({
'id': tid,
'topic': topic
})
topicusers.append({
'user': uid,
'topic': tid,
'approve': approve,
'thank': thank
})
return topics, topicusers
@retry(3)
def single(self, uid=None, linkname=None, save=False):
log.info('start fetching user - uid: %s | linkname: %s' % (uid, linkname))
url = 'https://www.jisilu.cn/people/%s' % (uid or linkname)
resp = self.__session.get(url, headers=HEADERS)
dollar = pq(resp.content)
if '用户不存在'.encode() in resp.content:
log.warn('deleted user - uid: %s | linkname: %s' % (uid, linkname))
return
if uid:
topics, topicusers = self.__extract_topics(uid, dollar)
if topics:
Topics.insert_many(topics).on_conflict('IGNORE').execute()
TopicUser.insert_many(topicusers).on_conflict('IGNORE').execute()
details = self.__extract_user_details(dollar)
if not details:
log.error('linkname error uid: %s' % uid)
return None
details.update({
'id': uid,
'linkname': unquote(resp.url.split('/')[-1]),
})
if save:
Users.insert(details).on_conflict('REPLACE').execute()
return details
def multi2(self, op=1, ed=270000, step=100):
rcds = []
for uid in range(op, ed):
rcd = self.single(uid)
if not rcd:
continue
rcds.append(rcd)
if len(rcds) % step == 0:
log.info('updating %s records' % step)
Users.insert_many(rcds).on_conflict('REPLACE').execute()
Users.insert_many(rcds).on_conflict('REPLACE').execute()
def multi(self, op=1):
users = (Users.select(Users.id)
.where((Users.id >= op) & (Users.signature >> None))
.order_by(Users.id))
for u in users:
rcd = self.single(uid=u.id)
if not rcd:
continue
Users.insert(rcd).on_conflict('REPLACE').execute()
class Pipeline(object):
def __init__(self):
self.post = FetchPost()
self.user = FetchUser()
if __name__ == '__main__':
import fire
fire.Fire(Pipeline)