def new_handler(self, *args, **kwargs): retry_time = 1 if times == -1: while True: try: is_ok = func(self, *args, **kwargs) if is_ok: return is_ok self.log.error( f'retry[{retry_time}:{self.bj_id}:{func.__name__}]:{args, kwargs}' ) retry_time += 1 except Exception: self.log.error( f'retry[{retry_time}:{self.bj_id}:{func.__name__}]:{args, kwargs} \n' + util.error_msg()) elif times > 0: for i in range(times): try: is_ok = func(self, *args, **kwargs) if is_ok: return is_ok self.log.error( f'retry[{retry_time}:{self.bj_id}:{func.__name__}]:{args, kwargs}' ) retry_time += 1 except Exception: self.log.error( f'retry[{retry_time}:{self.bj_id}:{func.__name__}]:{args} \n' + util.error_msg()) self.log.error( f'Fail retry[{retry_time}:{self.bj_id}:{func.__name__}]:{args, kwargs}' )
def _pre(): try: driver.get(conf.tar_url) return self._pre_trade() except: log.error(util.error_msg()) return False
def _parse_vod_info(html): files = html.xpath('//file') duration = 0 video_info = [] for f in files: try: int(f.get('duration')) except: continue try: video_info.append({ 'key': f.get('key'), 'duration': int(f.get('duration')), 'url': f.text, 'cum_duration': duration, }) duration += int(f.get('duration')) except: self.log.error( f'[parse_video_info]:{self.video_key(station_num)}:{url} \n' + util.error_msg()) result = { 'video': video_info, 'total': duration, 'type': VOD_TYPE.VOD, } self.stash[self.video_key(station_num)] = result return result
def merge_ts2mp4(dirname: str, output_name: str = 'output.mp4'): """ https://ffmpeg.org/ffmpeg.html https://moejj.com/ffmpeghe-bing-shi-pin-wen-jian-guan-fang-wen-dang/ 从文件夹中获取所有对应后缀名文件列表,并按照序号排序 ffmpeg -i "concat:input1.ts|input2.ts|input3.ts" -c copy output.ts # this is a comment file '/path/to/file1' file '/path/to/file2' file '/path/to/file3' ffmpeg -f concat -safe 0 -i mylist.txt -c copy output """ file_list = list(Path(dirname).glob('*.ts')) ordered_files = sorted(file_list, key=lambda x: (int(re.search(r'([0-9]+)(?=\.ts)', str(x))[0]), x)) if ordered_files: merge_path = os.path.abspath(os.path.join(dirname, 'merge.txt')) output_path = os.path.abspath(os.path.join(dirname, output_name)) with open(merge_path, 'w') as f: for i in ordered_files: if os.path.isfile(str(i)): f.write(f'file {i.name}\n') ffmpeg_cmd = f'ffmpeg -y -f concat -safe 0 -i {merge_path} -codec copy -bsf:a aac_adtstoasc {output_path}' try: subprocess.check_call(ffmpeg_cmd, shell=True) os.remove(merge_path) except subprocess.CalledProcessError as e: print(util.error_msg())
def down(self, url: str, path: Path, param: typing.Dict = None, chunk_size: int = 1024 * 9, timeout: int = 8): path_dir = os.path.dirname(path) os.makedirs(path_dir, exist_ok=True) try: resp = requests.get(url, params=param, stream=True, timeout=timeout) if resp.status_code == 200: with open(str(path), 'wb') as f: for r in resp.iter_content(chunk_size=chunk_size): f.write(r) self.log.info( f'[{self.bj_id}:{str(path.parent).split("/")[-1]}] download {path.name} success' ) return True except (Timeout, ConnectionError): self.log.error(f'[TIMEOUT get]:{url}:{param}') return False except Exception: self.log.error( f'[{self.bj_id}:{str(path.parent).split("/")[-1]}] : {path.name}\n' + util.error_msg()) return False
def get(self, url, params=None, timeout=3): try: resp = self.session.get(url, params=params, timeout=timeout) return resp except (Timeout, ConnectionError): self.log.error(f'[TIMEOUT get]:{url}:{params}') except Exception: self.log.error(f'[get]:{url}:{params} \n' + util.error_msg()) return None
def android_auth_code(device_name, tar_name, is_only=False, is_strict=True): def _clear_text(txt): return re.sub('\s', '', txt) def _is_swipe(user_list): start_x, start_y = user_list[-1].location['x'], user_list[-1].location[ 'y'] end_x, end_y = user_list[0].location['x'], user_list[0].location['y'] before_page = driver.page_source driver.swipe(start_x, start_y, end_x, end_y) after_page = driver.page_source return before_page != after_page def _judge(tar, describe): if is_strict: return tar == describe return tar in describe android_google_auth['deviceName'] = device_name try: driver = webdriver.Remote(url, desired_capabilities=android_google_auth) wait = WebDriverWait(driver, 30) result = {} is_swipe = True while is_swipe: user_list_xpath = '//android.widget.ListView[@resource-id="com.google.android.apps.authenticator2:id/user_list"]/android.view.ViewGroup' user_list = wait.until( EC.presence_of_all_elements_located( (By.XPATH, user_list_xpath))) for u in user_list: describe = u.find_element_by_id( 'com.google.android.apps.authenticator2:id/current_user' ).text if _judge( _clear_text(tar_name).lower(), _clear_text(describe).lower()): num = u.find_element_by_id( 'com.google.android.apps.authenticator2:id/pin_value' ).text if is_only or is_strict: return _clear_text(num) result[_clear_text(describe)] = _clear_text(num) is_swipe = _is_swipe(user_list) driver.close_app() return result except: print(util.error_msg()) return None
def _login(self, account_info): self._set_zh() account = account_info['account'] passwd = account_info['passwd'] auth_key = account_info['auth_key'] try: wait = WebDriverWait(driver, 500) driver.get(conf.login_url) self._dialog_close() account_ele = wait.until( EC.presence_of_element_located((By.NAME, 'auth_key'))) account_ele.clear() account_ele.send_keys(account) passwd_ele = wait.until( EC.presence_of_element_located((By.NAME, 'password'))) passwd_ele.clear() passwd_ele.send_keys(passwd) driver.find_element_by_id('submit').click() print('输入二次验证码') if conf.device_name and auth_key: auth_input = wait.until( EC.presence_of_element_located( (By.XPATH, '//div[@class="google-input"]/div/input'))) flag = True while flag: code = android_auth_code(conf.device_name, auth_key) if code: auth_input.clear() auth_input.send_keys(code) try: WebDriverWait(driver, 1).until( EC.presence_of_element_located( (By.CLASS_NAME, 'warn'))) except: flag = False element = WebDriverWait(driver, 500).until(EC.title_contains("个人中心")) return True except: log.error(util.error_msg()) return False
def _address(self, account_info, stash): if stash.get(account_info['account'], None): return stash[account_info['account']] is_ok = self._login(account_info) if is_ok: try: rep = req.get(address_url, **self._get_request_kw()) data = rep.json() if data['payment_address']['currency'] == conf.currency: deposit_address = data['payment_address']['deposit_address'] stash[account_info['account']] = deposit_address return deposit_address except: log.error(util.error_msg())
def _ts2mp4(self, dirname: str, output_name: str = 'output'): """ https://ffmpeg.org/ffmpeg.html https://moejj.com/ffmpeghe-bing-shi-pin-wen-jian-guan-fang-wen-dang/ 从文件夹中获取所有对应后缀名文件列表,并按照序号排序 ffmpeg -i "concat:input1.ts|input2.ts|input3.ts" -c copy output.ts # this is a comment file '/path/to/file1' file '/path/to/file2' file '/path/to/file3' ffmpeg -f concat -safe 0 -i mylist.txt -c copy output """ output_mp4_name = f'{output_name}.mp4' compress_mp4_name = f'{output_name}_tmp.mp4' file_list = list(Path(dirname).glob('*.ts')) ordered_files = sorted( file_list, key=lambda x: (int(re.search(r'([0-9]+)(?=\.ts)', str(x))[0]), x)) if ordered_files: merge_path = os.path.abspath(Path(dirname, 'merge.txt')) output_path = os.path.abspath(Path(dirname, output_mp4_name)) compress_path = os.path.abspath(Path(dirname, compress_mp4_name)) with open(merge_path, 'w') as f: for i in ordered_files: if os.path.isfile(str(i)): f.write(f'file {i.name}\n') ffmpeg_cmd = f'ffmpeg -y -f concat -safe 0 -i {merge_path} -codec copy -bsf:a aac_adtstoasc {compress_path}' compress_cmd = f'ffmpeg -y -i {compress_path} -c:v libx264 -crf 28 {output_path}' try: self.log.info(f'start to merge {output_path}') subprocess.check_call(ffmpeg_cmd, shell=True) os.remove(merge_path) for i in file_list: i.unlink() # subprocess.check_call(compress_cmd, shell=True) # os.remove(compress_path) except subprocess.CalledProcessError as e: self.log.error(util.error_msg()) return self.log.info(f'merge {output_path} success')
def cancel_all_order(self): sleep(2) wait = WebDriverWait(driver, 10) order_table_xpath = '//div[@class="bottom-current-delegation"]/table/div/tbody/tr' try: order_ele = wait.until(EC.presence_of_all_elements_located((By.XPATH, order_table_xpath))) for e in order_ele: td_ele = e.find_elements_by_tag_name('td') if td_ele and len(td_ele) > 1: td_ele[9].click() sleep(0.5) wait.until(EC.presence_of_element_located(( By.XPATH, '/html[1]/body[1]/div[3]/div[1]/div[2]/button[2]' ))).click() except: log.error(util.error_msg()) finally: return not self._check_order()
def down_img(self, url, param, path, chunk_size=1024 * 3, timeout=8): path_dir = os.path.dirname(path) os.makedirs(path_dir, exist_ok=True) try: resp = self.session.get(url, params=param, stream=True, timeout=timeout) if resp.status_code == 200: with open(path, 'wb') as f: for r in resp.iter_content(chunk_size=chunk_size): f.write(r) return True except (Timeout, ConnectionError): self.log.error(f'[TIMEOUT get]:{url}:{param}') except Exception: self.log.error(f'[Download]:{url}:{param} \n' + util.error_msg()) return False
def send_slack(self, state_level, msg): if self.webhook is None: return ip = util.get_outer_ip() payload = { "color": state_level, "fields": [ { 'title': '{} [{}]'.format(util.now(), ip), 'value': '{} {}'.format(MESSAGE_STATUS.NAME_DICT[state_level], msg), } ] } try: requests.post(self.webhook, json=payload) except Exception: self.err(util.error_msg())
def get_ticker(self): """盘口""" try: wait = WebDriverWait(driver, 30) ask_ele = wait.until(EC.presence_of_element_located(( By.XPATH, '//tbody[@class="scrollStyle ask-table"]/tr[last()]/td[1]') )) ask_price = util.safe_decimal(ask_ele.text) bid_ele = wait.until(EC.presence_of_element_located(( By.XPATH, '//tbody[@class="scrollStyle bid-table"]/tr[1]/td[1]') )) bid_price = util.safe_decimal(bid_ele.text) return { const.SIDE.ASK: ask_price, const.SIDE.BID: bid_price, } except: log.error(util.error_msg()) raise Exception('Error ticker')
def get_balance(self): pair = conf.pair ask, bid = pair.upper().split('_') try: wait = WebDriverWait(driver, 10) ele = wait.until(EC.presence_of_all_elements_located(( By.XPATH, '//div[@class="balance semi-bold"]') )) pattern = r'(\w+).*?(\d+\.\d*)' c = {} for e in ele: currency_name, amount = re.search(pattern, e.text.strip(), re.S).groups() c[currency_name] = decimal.Decimal(amount) return { const.SIDE.BID: c[bid], const.SIDE.ASK: c[ask], } except: log.error(util.error_msg()) raise Exception('Error balance')
def _get_video_info(self, station_num: int) -> dict: result = {} if self.check_bad_vod(station_num): return result if self.video_key(station_num) in self.stash and self.stash[ self.video_key(station_num)]['total'] > 0: return self.stash[self.video_key(station_num)] def _parse_vod_info(html): files = html.xpath('//file') duration = 0 video_info = [] for f in files: try: int(f.get('duration')) except: continue try: video_info.append({ 'key': f.get('key'), 'duration': int(f.get('duration')), 'url': f.text, 'cum_duration': duration, }) duration += int(f.get('duration')) except: self.log.error( f'[parse_video_info]:{self.video_key(station_num)}:{url} \n' + util.error_msg()) result = { 'video': video_info, 'total': duration, 'type': VOD_TYPE.VOD, } self.stash[self.video_key(station_num)] = result return result vod_param = self.station(station_num) if vod_param: url = self.INFO_URL + '?' + vod_param + '&szSysType=html5' resp = self.get(url) # TODO: user_vod if resp.status_code == 200: self.log.info(f'[get_video_info]:{station_num}') html = etree.HTML(resp.text) try: category = html.xpath('//video[@category]')[0].get( 'category') if category == '00210000': result = _parse_vod_info(html) except Exception: self.log.error( f'[get_video_info]:{self.video_key(station_num)}:{url} \n' + util.error_msg()) self.add_bad_vod(station_num) return result return result
def trade(self): def _pre(): try: driver.get(conf.tar_url) return self._pre_trade() except: log.error(util.error_msg()) return False def _strategy(): """ 瞄准买一卖一缝隙刷单 目标是尽量成交自己的单,持续时间尽量久 先卖后买 先买后卖 两种操作 交替执行 sell_buy buy_sell 单被吃 反向操作进行两次 Example: 进行buy_sell 导致买单被吃 之后做两次sell_buy 再buy_sell sell_buy 循环 保存操作状态 :return: """ spread = self.ticker[const.SIDE.ASK] - self.ticker[const.SIDE.BID] order_price = util.safe_decimal(round( float(self.ticker[const.SIDE.BID]) + random.uniform(float(self.price_point), float(spread - self.price_point)), util.get_round(self.price_point))) order_amount = util.safe_decimal(round( random.uniform(float(self.amount_point), float(self._get_amount_balance(const.SIDE.ASK))), util.get_round(self.amount_point))) with Stash(strategy_id) as stash: self._balance_asset(order_price, stash) if spread > self.price_point: # 有空隙 if self._judge_mode(stash) == STRATEGY_FLAG.FLAG_SB: is_ok = self.limit_sell(order_price, order_amount) if is_ok is None: stash[MODE_KEY] = MODE.FLAG_BS stash[BALANCE_KEY] = const.SIDE.ASK log.info('no suitable sell order') return if is_ok: pending_order = self.get_pending_order() self.limit_buy(pending_order[0]['price'], pending_order[0]['unsettled_amount']) if stash.get(MODE_KEY) == MODE.FILL_B: stash[MODE_KEY] = MODE.FLAG_SB else: stash[MODE_KEY] = MODE.FLAG_BS else: log.warn('sell order has filled') stash[MODE_KEY] = MODE.FILL_S else: is_ok = self.limit_buy(order_price, order_amount) if is_ok is None: stash[MODE_KEY] = MODE.FLAG_SB stash[BALANCE_KEY] = const.SIDE.BID log.info('no suitable buy order') return if is_ok: pending_order = self.get_pending_order() self.limit_sell(pending_order[0]['price'], pending_order[0]['unsettled_amount']) if stash.get(MODE_KEY) == MODE.FILL_S: stash[MODE_KEY] = MODE.FLAG_BS else: stash[MODE_KEY] = MODE.FLAG_SB else: log.warn('buy order has filled') stash[MODE_KEY] = MODE.FILL_B else: wait_spread = random.uniform(1, 10) msg = 'BID ASk price too close sleep {} sec'.format(wait_spread) log.info(msg) print(msg) sleep(wait_spread) # 页面准备 is_prepare = _pre() if is_prepare: while True: # TODO: 块价值 or balance check self.cancel_order_tap() is_ok = self.cancel_all_order() self._refresh() if is_ok: log.info('CANCEL ALL ORDER') sleep(2) try: self.update_market() _strategy() except: log.error(util.error_msg())