forked from s045pd/DarkNet_ChineseTrading
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
341 lines (316 loc) Β· 12 KB
/
parser.py
File metadata and controls
341 lines (316 loc) Β· 12 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import platform
import re
from io import BytesIO
from urllib.parse import urljoin, urlparse
import moment
import pytesseract
from bs4 import BeautifulSoup as bs_4
from imgcat import imgcat
from PIL import Image
from common import fix_nums, float_format
from log import debug, error, info, success
from conf import Config
class Parser:
@staticmethod
def get_next_target(resp):
try:
next_target = re.findall(
'<meta http-equiv="refresh" content="3;url=(.*?)">', resp.text
)
if next_target:
info(f"find next target: {next_target[0]}")
return next_target[0]
except Exception as e:
error(f"[Parser->get_next_target]: {e}")
@staticmethod
def get_login_and_reg_payload(resp):
try:
if "500 Internal Privoxy Error" in resp.text:
error("check your proxies")
exit()
bs_data = bs_4(resp.text, "lxml")
autim = bs_data.select_one('input[name="autim"]').attrs["value"]
sid = bs_data.select_one('input[name="sid"]').attrs["value"]
form_token = bs_data.select_one('input[name="form_token"]').attrs["value"]
creation_time = bs_data.select_one('input[name="creation_time"]').attrs[
"value"
]
login = {
"autim": autim,
"creation_time": creation_time,
"form_token": form_token,
"login": "η»ε½",
"password": "",
"redirect": [
item.attrs["value"]
for item in bs_data.select('input[name="redirect"]')
],
"sid": sid,
"username": "",
}
debug(f"login payload: {login}")
login_url = urljoin(resp.url, bs_data.select_one("#login").attrs["action"])
debug(f"login url: {login_url}")
reg_url = urljoin(resp.url, bs_data.select_one("a.button2").attrs["href"])
debug(f"register url: {reg_url}")
return autim, sid, login, login_url, reg_url
except Exception as e:
error(f"[Parser->get_login_and_reg_payload]: {e}")
raise e
@staticmethod
def get_sid(resp, default):
try:
sid = re.findall('sid=(.*?)"', resp.text)
if sid:
info(f"sid: {sid[0]}")
return sid[0]
else:
return default
except Exception as e:
error(f"[Parser->get_sid]: {e}")
return default
@staticmethod
def get_token_and_creation_time(resp):
try:
bs_data = bs_4(resp.text, "lxml")
token = bs_data.select_one('input[name="form_token"]').attrs["value"]
info(f"token: {token}")
creation_time = bs_data.select_one('input[name="creation_time"]').attrs[
"value"
]
info(f"creation_time: {creation_time}")
return token, creation_time
except Exception as e:
error(f"[Parser->get_token_and_creation_time]: {e}")
return (None, None)
@staticmethod
def get_qa_answer_and_id(resp):
try:
qa_answer = re.findall("θ―·ε¨ε³θΎΉζ‘δΈθΎε
₯οΌ (.*?)οΌ</label>", resp.text)[0]
info(f"qa_answer: {qa_answer}")
bs_data = bs_4(resp.text, "lxml")
qa_confirm_id = bs_data.select_one("#qa_confirm_id").attrs["value"]
info(f"qa_confirm_id: {qa_confirm_id}")
return qa_answer, qa_confirm_id
except Exception as e:
error(f"[Parser->get_qa_answer_and_id]: {e}")
return (None, None)
@staticmethod
def get_captcha(func, resp):
try:
bs_data = bs_4(resp.text, "lxml")
path = bs_data.select_one(".captcha>img").attrs["src"]
confirm_id = re.findall("confirm_id=(.*?)&", path)[0]
img_url = urljoin(resp.url, path)
img_raw = func(img_url)
if platform.system().upper() == "DARWIN":
imgcat(img_raw)
code = pytesseract.image_to_string(
Image.open(BytesIO(img_raw)), lang="snum"
).replace(" ", "")
info(f"captcha_code: {code}, confirm_id:{confirm_id}")
return code, confirm_id
# return input("code:"), confirm_id
except Exception as e:
error(f"[Parser->get_captcha]: {e}")
return "TRBGR", "7c3601cd570d2650a89fd33b3b5238d1"
@staticmethod
def get_current_type(resp):
try:
types = {
item.select_one(".index_list_title")
.attrs["href"]
.split("=")[1]
.split("&")[0]: item.select_one("tr:nth-child(1) > td")
.text.split()[0]
.replace("ζ₯ηζ΄ε€", "")
for item in bs_4(resp.text, "lxml").select(".ad_table_b")
}
info(f"all types: {types}")
types = dict(
filter(lambda item: item[1] in Config.filterArea, types.items())
)
success(f"types filter result: {types}")
return types
except Exception as e:
error(f"[Parser->get_current_type]: {e}")
return {}
@staticmethod
def get_uid_and_sid(bs_data):
try:
uid = fix_nums(
bs_data.select_one(
".v_table_2 > tr:nth-child(5) > td:nth-child(2)"
).text
)
debug(f"uid: {uid}")
sid = fix_nums(bs_data.select_one("tr:nth-child(3) > td:nth-child(2)").text)
debug(f"sid: {sid}")
return uid, sid
except Exception as e:
error(f"[Parser->get_uid_and_sid]: {e}")
return (None, None)
@staticmethod
def get_person_data(bs_data):
try:
personDatas = {
"salenums": fix_nums(
bs_data.select_one(
".v_table_2 tr:nth-child(3) > td:nth-child(4)"
).text
),
"totalsales": float_format(
bs_data.select_one(
".v_table_2 tr:nth-child(5) > td:nth-child(4)"
).text
),
"totalbuys": float_format(
bs_data.select_one(
".v_table_2 tr:nth-child(7) > td:nth-child(4)"
).text
),
}
username = bs_data.select_one(
".v_table_2 tr:nth-child(3) > td:nth-child(2)"
).text
debug(f"personDatas: {personDatas}")
debug(f"username: {username}")
return personDatas, username
except Exception as e:
error(f"[Parser->get_person_data]: {e}")
return {"salenums": 0, "totalsales": 0, "totalbuys": 0}, ""
@staticmethod
def get_reg_date(bs_data, default):
try:
reg_date_str = bs_data.select_one(
".v_table_2 tr:nth-child(7) > td:nth-child(2)"
).text
debug(f"reg_date_str:{reg_date_str}")
return moment.date(reg_date_str).format("YYYY-MM-DD")
except Exception as e:
error(f"[Parser->get_reg_date]: {e}")
return default
@staticmethod
def get_detail_content(bs_data):
try:
content = " ".join(bs_data.select_one(".postbody .content").text.split())
debug(f"content: {content}")
return content
except Exception as e:
error(f"[Parser->get_detail_content]: {e}")
return ""
@staticmethod
def get_img_urls(bs_data):
try:
urls = [_.attrs["src"] for _ in bs_data.select(".postbody img")]
debug(f"img url:{urls}")
return urls
except Exception as e:
error(f"[Parser->get_img_urls]: {e}")
return []
@staticmethod
def get_up_time(bs_data, current_year):
try:
to_current_year_datetime = moment.date(
f"{current_year} "
+ bs_data.select_one("tr:nth-child(3) > td:nth-child(6)").text
)
real_up_time_tree = bs_data.select_one(".author")
real_up_time_tree.a.extract()
real_up_time_tree.span.extract()
real_up_time = moment.date(
real_up_time_tree.text.replace("εΉ΄", "")
.replace("ζ", "")
.replace("ζ₯", "")
)
real_up_time = (
real_up_time if real_up_time._date else to_current_year_datetime
)
return debug(real_up_time)
except Exception as e:
error(f"[Parser->get_up_time]: {e}")
return moment.now()
@staticmethod
def get_details(bs_data, current_year, real_up_time, muti):
try:
priceUSDT = float_format(
bs_data.select_one("tr:nth-child(3) > td:nth-child(4) > span").text
)
priceBTC = float_format(
bs_data.select_one("tr:nth-child(5) > td:nth-child(4)").text.split()[0]
)
if priceBTC > priceUSDT:
priceUSDT, priceBTC = priceBTC, priceUSDT
return debug(
{
"lasttime": moment.date(
f"{current_year} "
+ bs_data.select_one("tr:nth-child(7) > td:nth-child(6)").text
).format("YYYY-MM-DD HH:mm:ss"),
"priceUSDT": priceUSDT,
"priceBTC": priceBTC,
"lines": muti["lines"],
"uptime": real_up_time.format("YYYY-MM-DD HH:mm:ss"),
"hot": muti["hot"],
"types": bs_data.select_one(
"tr:nth-child(5) > td:nth-child(2)"
).text,
"status": bs_data.select_one(
"tr:nth-child(7) > td:nth-child(2)"
).text,
"oversell": fix_nums(
bs_data.select_one("tr:nth-child(9) > td:nth-child(2)").text,
to=99999,
),
"sold": fix_nums(
bs_data.select_one("tr:nth-child(7) > td:nth-child(4)").text,
to=99999,
),
}
)
except Exception as e:
error(f"[Parser->get_details]: {e}")
raise e
@staticmethod
def get_types(resp):
try:
for item in bs_4(resp.text, "lxml").select("table.m_area_a tr"):
detail_path = item.select_one("div.length_400>a.text_p_link")
if detail_path:
yield debug((item, urljoin(resp.url, detail_path.attrs["href"])))
except Exception as e:
error(f"[Parser->get_types]: {e}")
return []
@staticmethod
def get_type_datas(item):
try:
return debug(
{
"lines": fix_nums(
item.select_one("td:nth-child(7)").text.replace("倩", "")
),
"hot": fix_nums(item.select_one("td:nth-child(8)").text),
"title": item.select_one("td:nth-child(5)").text,
"area": item.select_one("td:nth-child(3)").text,
}
)
except Exception as e:
error(f"[Parser->get_type_datas]: {e}")
raise e
@staticmethod
def get_index(bs_item, default="*"):
try:
return bs_item.select("td")[0].text
except Exception as e:
error(f"[Parser->get_index]: {e}")
return default
@staticmethod
def get_max_page(resp, just_update):
try:
info("Parsing Max Page")
max_page = 30 if not just_update else 1
info(f"MaxPage: {max_page}")
return max_page
except Exception as e:
error(f"[Parser->get_max_page]: {e}")
return 1