def get_cookie_from_login_sina_com_cn(account, password): """ 获取一个账号的Cookie """ login_url = "https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.19)" username = base64.b64encode(account.encode("utf-8")).decode("utf-8") headers = { 'Referer': 'https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.19)', 'Upgrade-Insecure-Requests': '1', 'Host': 'login.sina.com.cn', 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Accept-Encoding': 'gzip, deflate, br', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' } post_data = { "entry": "sso", "gateway": "1", "from": "null", "savestate": "30", "useticket": "0", "pagerefer": "", "vsnf": "1", "su": username, "service": "sso", "sp": password, "sr": "1440*900", "encoding": "UTF-8", "cdult": "3", "domain": "sina.com.cn", "prelt": "0", "returntype": "TEXT", } session = requests.Session() r = session.post(login_url, headers=headers, data=post_data, verify=False) json_str = r.content.decode("gbk") info = json.loads(json_str) LOGGER.info('get cookies for %s' % account) if info["retcode"] == "0": LOGGER.info("Get Cookie Success!( Account:%s )" % account) cookies = session.cookies.get_dict() for k, v in cookies.items(): print(k, v) return cookies else: LOGGER.warning("Get Cookie failed!( Account:%s )" % account) LOGGER.warning(info) return None
def getRealtimeInfo(code_list, entry_time): stocks = {'success': False} if entry_time == 3: return stocks try: LOGGER.info(f"Retry to grab realtime infomation ({entry_time}/2)") stocks = twstock.realtime.get(code_list) except KeyboardInterrupt: os._exit() except ConnectionError: LOGGER.warning("Connection Fail...") stocks = getRealtimeInfo(code_list, entry_time + 1) except: LOGGER.exception("Fail to Get realtime info") return stocks
def getStockInfo(stock_code): LOGGER.info(f"Processing {stock_code} ...") retry = 0 stock = None while retry <= StockInfoSetting.RETRY_MAX_TIME and stock is None: try: stock = twstock.Stock(stock_code) break except ConnectionError: LOGGER.warning( f'Grab {stock_code} Fail, Retry ({retry}/{StockInfoSetting.RETRY_MAX_TIME})' ) except Exception: LOGGER.warning( f'Grab {stock_code} Fail, Retry ({retry}/{StockInfoSetting.RETRY_MAX_TIME})', exc_info=True) retry += 1 sleep(1) return stock
def load_telegram_json(): try: return json.load(TELEGRAM_JSON_FILE.open()) except: LOGGER.warning('Fail to load config of telegram', exc_info=True) return None