Skip to content

Commit 5641456

Browse files
authored
Weibo site check fix, activation mechanism added (#1938)
1 parent 29c1f56 commit 5641456

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

maigret/activation.py

+41
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,47 @@ def spotify(site, logger, cookies={}):
3939
bearer_token = r.json()["accessToken"]
4040
site.headers["authorization"] = f"Bearer {bearer_token}"
4141

42+
@staticmethod
43+
def weibo(site, logger):
44+
headers = dict(site.headers)
45+
import requests
46+
47+
session = requests.Session()
48+
# 1 stage: get the redirect URL
49+
r = session.get(
50+
"https://weibo.com/clairekuo",
51+
headers=headers,
52+
allow_redirects=False
53+
)
54+
logger.debug(
55+
f"1 stage: {'success' if r.status_code == 302 else 'no 302 redirect, fail!'}"
56+
)
57+
location = r.headers.get("Location")
58+
59+
# 2 stage: go to passport visitor page
60+
headers["Referer"] = location
61+
r = session.get(location, headers=headers)
62+
logger.debug(
63+
f"2 stage: {'success' if r.status_code == 200 else 'no 200 response, fail!'}"
64+
)
65+
66+
# 3 stage: gen visitor token
67+
headers["Referer"] = location
68+
r = session.post(
69+
"https://passport.weibo.com/visitor/genvisitor2",
70+
headers=headers,
71+
data={
72+
'cb': 'visitor_gray_callback',
73+
'tid': '',
74+
'from': 'weibo'
75+
},
76+
)
77+
cookies = r.headers.get('set-cookie')
78+
logger.debug(
79+
f"3 stage: {'success' if r.status_code == 200 and cookies else 'no 200 response and cookies, fail!'}"
80+
)
81+
site.headers["Cookie"] = cookies
82+
4283

4384
def import_aiohttp_cookies(cookiestxt_filename):
4485
cookies_obj = MozillaCookieJar(cookiestxt_filename)

maigret/checking.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,16 @@ def process_site_result(
277277
)
278278

279279
if site.activation and html_text and is_need_activation:
280+
logger.debug(f"Activation for {site.name}")
280281
method = site.activation["method"]
281282
try:
282283
activate_fun = getattr(ParsingActivator(), method)
283284
# TODO: async call
284285
activate_fun(site, logger)
285-
except AttributeError:
286+
except AttributeError as e:
286287
logger.warning(
287-
f"Activation method {method} for site {site.name} not found!"
288+
f"Activation method {method} for site {site.name} not found!",
289+
exc_info=True,
288290
)
289291
except Exception as e:
290292
logger.warning(

maigret/resources/data.json

+26-7
Original file line numberDiff line numberDiff line change
@@ -30186,20 +30186,39 @@
3018630186
},
3018730187
"Weibo": {
3018830188
"absenceStrs": [
30189-
"username_box\\",
30190-
"info_list username\\"
30189+
"<h2>400 Bad Request</h2>"
3019130190
],
3019230191
"presenseStrs": [
30193-
"pf_username\\",
30194-
"use_num\\"
30192+
"{\"ok\":1,\"data\":{\"user\":"
3019530193
],
30194+
"headers": {
30195+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:133.0) Gecko/20100101 Firefox/133.0",
30196+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
30197+
"Accept-Language": "en-US,en;q=0.5",
30198+
"Upgrade-Insecure-Requests": "1",
30199+
"Sec-Fetch-Dest": "document",
30200+
"Sec-Fetch-Mode": "navigate",
30201+
"Sec-Fetch-Site": "cross-site",
30202+
"Priority": "u=0, i",
30203+
"Pragma": "no-cache",
30204+
"Cache-Control": "no-cache",
30205+
"Accept-Encoding": "gzip, deflate, br, zstd",
30206+
"TE": "trailers",
30207+
"Cookie": "SUB=_2AkMQDkYqf8NxqwFRmf4QzWnqbop-wwHEieKmUrfxJRMxHRl-yT9kqm4gtRB6O45oxc8K9O2Jsarg5zYMmQy3bR_LfISF; expires=Saturday, 06-Dec-2025 09:51:25 GMT; path=/; domain=.weibo.com; secure; httponly; SameSite=None, SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9Whze9rurqv2pUdg7sY.DTbO; expires=Saturday, 06-Dec-2025 09:51:25 GMT; path=/; domain=.weibo.com, SRT=D.QqHBTrsMMFkSVdRtOeYoWrSNUdRr4Q9QUeE8U3vkW3WzMdbbN-sPVcStNbHi5mYNUCsuPDbhVdrrS3MNAZSLiDP65FJtNqbLJ%219qRQHeiQ9SOdsM5Oi84byJS%21bOMX77%2AB.vAflW-P9Rc0lR-ykKDvnJqiQVbiRVPBtS%21r3J8sQVqbgVdWiMZ4siOzu4DbmKPWf5c4uVePpVQRpVEP%21SqWqdNumPFHL; expires=Mon, 04-Dec-2034 09:51:25 GMT; Max-Age=315360000; path=/; domain=.passport.weibo.com; secure; HttpOnly, SRF=1733478685; expires=Mon, 04-Dec-2034 09:51:25 GMT; Max-Age=315360000; path=/; domain=.passport.weibo.com; secure"
30208+
},
30209+
"activation": {
30210+
"method": "weibo",
30211+
"marks": [
30212+
"<title>\u5fae\u535a</title",
30213+
"<title>Sina Visitor System</title>"
30214+
],
30215+
"url": "https://passport.weibo.com/visitor/genvisitor2"
30216+
},
30217+
"urlProbe": "https://weibo.com/ajax/profile/info?custom={username}",
3019630218
"url": "https://weibo.com/{username}",
3019730219
"urlMain": "https://weibo.com",
3019830220
"usernameClaimed": "clairekuo",
3019930221
"usernameUnclaimed": "noonewouldeverusethis7",
30200-
"headers": {
30201-
"Cookie": "SUB=_2AkMXyuc_f8NxqwJRmP8SyWPrbo13zAvEieKhlhbkJRMxHRl-123"
30202-
},
3020330222
"checkType": "message",
3020430223
"tags": [
3020530224
"cn",

0 commit comments

Comments
 (0)