def _init_cookies(self): """ method Oo and co in 6.min.js generate "dv" from mouse event. This only reason I use PhantomJS. I hope reimplement it in Python. metho _initApi in login_tangram.js initialize login "token" in window.$BAIDU$._maps_id['TANGRAM__PSP_4'].bdPsWtoken. :return: BAIDUID, dv, token """ while True: driver = webdriver.PhantomJS(executable_path="./phantomjs.exe") driver.get("http://pan.baidu.com") el = driver.find_element_by_id("pageSignupCtrl") act = ActionChains(driver) act.move_to_element(el).perform() el = driver.find_element_by_id("dv_Input") dv = el.get_attribute("value") baiduid = driver.get_cookie("BAIDUID")["value"] token = driver.execute_script( "return window.$BAIDU$._maps_id['TANGRAM__PSP_4'].bdPsWtoken") driver.quit() cks = self.session.cookies cks.set("BAIDUID", baiduid) cks.set("yew_dv", dv) cks.set("yew_token", token) if (baiduid is not None) and (dv is not None) and (token is not None): break LOG.debug("dv:{}\nBAIDUID:{}\ntoken:{}".format(dv, baiduid, token)) return baiduid, dv, token
def _login(self): r = self._req("login", data={ "dv": "", "username": self.usr, "password": self.pwd, "token": self.token, "gid": self.gid, }, params=None, cookies={}) if "err_no=257" in r.content or "err_no=6" in r.content: code_string = re.findall(b'codeString=(.*?)&', r.content)[0] self.codeString = code_string LOG.debug('need captcha, codeString=' + code_string.decode('utf-8')) # captcha = self._get_captcha(code_string) dlg = MyCaptchaDialog(None, title="ABC", pth="mini_113.png") dlg.CenterOnScreen() dlg.ShowModal() # 需要输入验证码或验证码输入错误 if "err_no=0" not in r.content: LOG.debug("Login fail") self._remove_cookies()
def _request(self, req_id=None, params=None, data=None, cookies=None): params = {} if params is None else params data = {} if data is None else data cookies = {} if cookies is None else cookies if req_id not in REQUEST_TEMPLATE: LOG.error("Request cfg of %s not found" % req_id) return None return self._req(req_id, params, data, cookies)
def onOpenFile(self, evt): usr = self.cfg.get("usr") pwd = self.cfg.get("pwd") from my_baidu import MyBaiduPan t = G.time_start() bdpan = MyBaiduPan(usr, pwd, ssl_verify=False) LOG.debug(G.time_end(t))
def __init__(self, file_path=""): LOG.info(u"session path:{}".format(file_path)) self.file_path = file_path self.session = {} if self.file_path == "": LOG.error("session file path is empty which shouldn't happen") elif os.path.isfile(self.file_path): with open(self.file_path, "rb") as f: self.session = cPickle.load(f)
def wrapper(*args, **kwargs): ret = func(*args, **kwargs) if type(ret) == requests.Response: try: foo = json.loads(ret.content.decode('utf-8')) if 'errno' in foo and foo['errno'] == -6: LOG.debug('Offline, deleting cookies file then relogin.') args[0]._remove_cookies() args[0]._initiate() except: LOG.error('User unsigned in.') return ret
def __init__(self, file_path=None, file_type="json"): LOG.info(u"cfg path:{}".format(file_path)) self.path = file_path self.type = file_type if self.path is None: LOG.error("cfg file path is None which shouldn't happen") elif not os.path.isfile(self.path): with open(self.path, "w") as f: if "json" == file_type: json.dump(CFG_JSON, f, sort_keys=True, indent=4, separators=(',', ': ')) elif "ini" == file_type: f.write(CFG_INI)
def run(self): while not self.thread_stop: for q in self.queues: if not q.empty(): r = q.get() LOG.debug(r) signal_str = r["signal"] signal_dat = r["data"] sender = self if signal_str in self.sender_map.keys(): sender = self.sender_map[signal_str] sig = signal(signal_str) sig.send(sender, data=signal_dat) time.sleep(self.interval)
def OnInit(self): t = G.time_start() LOG.debug("OnInit") # init conf MyConf(os.path.join(os.getcwd(), "my.settings")) # init session MySession(os.path.join(os.getcwd(), "my.session")) # init database models.create_all_tables() # setup MainFrame frame = MyMainFrame(None, res.S_MF_TITLE) self.SetTopWindow(frame) frame.Show(True) LOG.debug(G.time_end(t)) return True
def _initiate(self): if not self._load_cookies(): LOG.debug("Need login") self.session.get("http://www.baidu.com") self.gid = self._gid() self.token = self._token() self._login() # get PANPSC in cookies self.session.get("http://pan.baidu.com/disk/home", params={ "errno": "0", "errmsg": "Auth Login Sucess", "ssnerror": "0" }) # self._save_cookies() else: cks = self.session.cookies self.dv = cks.get("yew_dv") self.token = cks.get("yew_token") self.gid = self._gid()
def _load_cookies(self): cookies_file = "{0}.cookies".format(self.usr) LOG.debug("cookies file:" + cookies_file) if os.path.exists(cookies_file): LOG.debug("%s cookies file has already existed." % self.usr) with open(cookies_file, "rb") as cookies_file: cookies = requests.utils.cookiejar_from_dict( pickle.load(cookies_file)) LOG.debug(str(cookies)) self.session.cookies = cookies return True else: return False
def onFolderUpdated(self, **kw): dat = File.getItems() self.model.data = dat self.model.Reset(len(dat)) LOG.debug(kw["data"])
def saveSession(self): pos = self.GetPosition() size = self.GetSize() MySession().set(MyMainFrame.__name__, {"pos": pos, "size": size}) LOG.debug({"pos": pos, "size": size})