Skip to content

Commit c34fcdc

Browse files
authored
Merge pull request #616 from ahuangg/alan-branch
update: improved retrieval of cookies workflow
2 parents 7844dfa + 913a03f commit c34fcdc

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

weibo_spider/config_util.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,29 +174,44 @@ def add_user_uri_list(user_config_file_path, user_uri_list):
174174
user_uri_list[0] = '\n' + user_uri_list[0]
175175
with codecs.open(user_config_file_path, 'a', encoding='utf-8') as f:
176176
f.write('\n'.join(user_uri_list))
177-
177+
178178
def get_cookie():
179179
"""Get weibo.cn cookie from Chrome browser"""
180180
try:
181181
chrome_cookies = browser_cookie3.chrome(domain_name='weibo.cn')
182182
cookies_dict = {cookie.name: cookie.value for cookie in chrome_cookies}
183-
cookie_string = '; '.join(f'{name}={value}' for name, value in cookies_dict.items())
184-
return cookie_string
183+
return cookies_dict
185184
except Exception as e:
186185
logger.error(u'Failed to obtain weibo.cn cookie from Chrome browser: %s', str(e))
187186
raise
188187

189-
def update_cookie_config(user_config_file_path):
190-
"Update cookie in config.json"
188+
def update_cookie_config(cookie, user_config_file_path):
189+
"""Update cookie in config.json"""
191190
if not user_config_file_path:
192191
user_config_file_path = os.getcwd() + os.sep + 'config.json'
193192
try:
194-
cookie = get_cookie()
195193
with codecs.open(user_config_file_path, 'r', encoding='utf-8') as f:
196194
config = json.load(f)
197-
config['cookie'] = cookie
198-
with codecs.open(user_config_file_path, 'w', encoding='utf-8') as f:
199-
json.dump(config, f, indent=4, ensure_ascii=False)
195+
196+
cookie_string = '; '.join(f'{name}={value}' for name, value in cookie.items())
197+
198+
if config['cookie'] != cookie_string:
199+
config['cookie'] = cookie_string
200+
with codecs.open(user_config_file_path, 'w', encoding='utf-8') as f:
201+
json.dump(config, f, indent=4, ensure_ascii=False)
200202
except Exception as e:
201203
logger.error(u'Failed to update cookie in config file: %s', str(e))
202204
raise
205+
206+
def check_cookie(user_config_file_path):
207+
"""Checks if user is logged in"""
208+
try:
209+
cookie = get_cookie()
210+
if cookie["MLOGIN"] == '0':
211+
logger.warning("使用 Chrome 在此登录 %s", "https://passport.weibo.com/sso/signin?entry=wapsso&source=wapssowb&url=https://m.weibo.cn/")
212+
sys.exit()
213+
else:
214+
update_cookie_config(cookie, user_config_file_path)
215+
except Exception as e:
216+
logger.error(u'Check for cookie failed: %s', str(e))
217+
raise

weibo_spider/spider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,14 @@ def _get_config():
366366
config_path = FLAGS.config_path
367367
elif not os.path.isfile(config_path):
368368
shutil.copy(src, config_path)
369-
config_util.update_cookie_config(config_path)
370369
logger.info(u'请先配置当前目录(%s)下的config.json文件,'
371370
u'如果想了解config.json参数的具体意义及配置方法,请访问\n'
372371
u'https://github.com/dataabc/weiboSpider#2程序设置' %
373372
os.getcwd())
374373
sys.exit()
375374
try:
376375
with open(config_path) as f:
376+
config_util.check_cookie(config_path)
377377
config = json.loads(f.read())
378378
return config
379379
except ValueError:

0 commit comments

Comments
 (0)