@@ -174,29 +174,44 @@ def add_user_uri_list(user_config_file_path, user_uri_list):
174
174
user_uri_list [0 ] = '\n ' + user_uri_list [0 ]
175
175
with codecs .open (user_config_file_path , 'a' , encoding = 'utf-8' ) as f :
176
176
f .write ('\n ' .join (user_uri_list ))
177
-
177
+
178
178
def get_cookie ():
179
179
"""Get weibo.cn cookie from Chrome browser"""
180
180
try :
181
181
chrome_cookies = browser_cookie3 .chrome (domain_name = 'weibo.cn' )
182
182
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
185
184
except Exception as e :
186
185
logger .error (u'Failed to obtain weibo.cn cookie from Chrome browser: %s' , str (e ))
187
186
raise
188
187
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"" "
191
190
if not user_config_file_path :
192
191
user_config_file_path = os .getcwd () + os .sep + 'config.json'
193
192
try :
194
- cookie = get_cookie ()
195
193
with codecs .open (user_config_file_path , 'r' , encoding = 'utf-8' ) as f :
196
194
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 )
200
202
except Exception as e :
201
203
logger .error (u'Failed to update cookie in config file: %s' , str (e ))
202
204
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
0 commit comments