def get_last_login_sid(): config_path = get_user_config_path() last_login_log = os.path.join(config_path, ".last_login_log") if os.path.exists(last_login_log): with open(last_login_log) as f: return f.read() return None
def get_accounts_basic_info_list(): config_path = get_user_config_path() sids = [i for i in os.listdir(config_path) if (i < 'a') and (not i.startswith('.'))] account_list = [] for sid in sids: account = get_data_from_local_json(sid, "account.json") if not account: continue account_list.append(account) return account_list
def get_data_from_local_json(sid, file_name): config_path = get_user_config_path() path = osp.join(config_path, sid, file_name) if not osp.exists(path): return None with open(path) as f: content = f.read() js_obj = None try: js_obj = json.loads(content) except ValueError: # content is "" logger.error("%s content: `%s`" % (file_name, content)) return js_obj
def get_accounts_basic_info_list(): config_path = get_user_config_path() sids = [ i for i in os.listdir(config_path) if (i < 'a') and (not i.startswith('.')) ] account_list = [] for sid in sids: account = get_data_from_local_json(sid, "account.json") if not account: continue account_list.append(account) return account_list
def delete_config_from_local_json(sid, file_name): config_path = get_user_config_path() path = osp.join(config_path, sid, file_name) if osp.exists(path): os.remove(path)
def save_data_to_local_json(d, file_name): config_path = get_user_config_path() sid = str(d["sid"]) path = osp.join(config_path, sid, file_name) with open(path, "w") as f: f.write(json.dumps(d))
def set_last_login_sid(sid): config_path = get_user_config_path() last_login_log = os.path.join(config_path, ".last_login_log") with open(last_login_log, 'w') as f: f.write(sid)
def set_user_path(self, sid): # $HOME/.fetion/sid self._user_path = os.path.join(get_user_config_path(), sid) if not os.path.exists(self._user_path): os.mkdir(self._user_path)
def i_generate_verification_pic(user, res_obj, debug=False): """ There two types response, HTTP status 421 or SIPResponse code 421, the former response body contains verification node, the latter contains reason note only. """ body_dom = minidom.parseString(res_obj.body) veri_nodes = body_dom.getElementsByTagName("verification") if veri_nodes: veri_node = veri_nodes[0] attr = veri_node.getAttribute algorithm = attr("algorithm") _type = attr("type") text = attr("text") tips = attr("tips") else: reason_node = body_dom.getElementsByTagName("reason")[0] w_val = res_obj.headers.get_field_value("W") algorithm = strip2(w_val, 'algorithm="', '",') _type = strip2(w_val, 'type="', '"') text = reason_node.getAttribute("text") tips = "" veri = Verification(algorithm=algorithm, _type=_type, text=text, tips=tips) ssi_cookie = user.get_ssi_cookie() or "" host = "nav.fetion.com.cn" headers = dict({ 'Cookie': 'ssic=%s' % ssi_cookie, "Connection": "close", "User-Agent": "IIC2.0/PC %s" % PROTOCOL_VERSION, }) httplib.HTTPConnection.response_class = HTTPResponse conn = httplib.HTTPConnection(host) if debug: debuglevel = 1 else: debuglevel = 0 conn.set_debuglevel(debuglevel) if veri.algorithm: algorithm = veri.algorithm else: algorithm = "" url = '/nav/GetPicCodeV4.aspx?algorithm=%s' % algorithm conn.request("GET", url, headers=headers) res_obj = conn.getresponse() assert httplib.OK == res_obj.status body_dom = minidom.parseString(res_obj.body) conn.close() pic_cert_node = body_dom.getElementsByTagName("pic-certificate")[0] attr = pic_cert_node.getAttribute veri.pid = attr("id") pic_base64 = attr("pic") pic_save_path = os.path.join(get_user_config_path(), "%s" % user.sid, "verify_code.jpeg") with open(pic_save_path, "wb") as f: f.write(base64.decodestring(pic_base64)) veri.picture_path = pic_save_path return veri
def i_generate_verification_pic(user, res_obj, debug = False): """ There two types response, HTTP status 421 or SIPResponse code 421, the former response body contains verification node, the latter contains reason note only. """ body_dom = minidom.parseString(res_obj.body) veri_nodes = body_dom.getElementsByTagName("verification") if veri_nodes: veri_node = veri_nodes[0] attr = veri_node.getAttribute algorithm = attr("algorithm") _type = attr("type") text = attr("text") tips = attr("tips") else: reason_node = body_dom.getElementsByTagName("reason")[0] w_val = res_obj.headers.get_field_value("W") algorithm = strip2(w_val, 'algorithm="', '",') _type = strip2(w_val, 'type="', '"') text = reason_node.getAttribute("text") tips = "" veri = Verification(algorithm = algorithm, _type = _type, text = text, tips = tips) ssi_cookie = user.get_ssi_cookie() or "" host = "nav.fetion.com.cn" headers = dict({ 'Cookie' : 'ssic=%s' % ssi_cookie, "Connection" : "close", "User-Agent" : "IIC2.0/PC %s" % PROTOCOL_VERSION, }) httplib.HTTPConnection.response_class = HTTPResponse conn = httplib.HTTPConnection(host) if debug: debuglevel = 1 else: debuglevel = 0 conn.set_debuglevel(debuglevel) if veri.algorithm: algorithm = veri.algorithm else: algorithm = "" url = '/nav/GetPicCodeV4.aspx?algorithm=%s' % algorithm conn.request("GET", url, headers = headers) res_obj = conn.getresponse() assert httplib.OK == res_obj.status body_dom = minidom.parseString(res_obj.body) conn.close() pic_cert_node = body_dom.getElementsByTagName("pic-certificate")[0] attr = pic_cert_node.getAttribute veri.pid = attr("id") pic_base64 = attr("pic") pic_save_path = os.path.join(get_user_config_path(), "%s" % user.sid, "verify_code.jpeg") with open(pic_save_path, "wb") as f: f.write(base64.decodestring(pic_base64)) veri.picture_path = pic_save_path return veri