def consulta_ISBN(update, context): try: estante = context.user_data.get('estante', '-') text = update.message.text.strip() payload['search'] = text logger.info('Recebeu ISBN {}'.format(text)) r = requests.post(ISBN_URL, json=payload, headers=headers) try: livro_json = r.json()['value'][0] except IndexError: livro_json = { 'RowKey': text, 'Title': text, 'Colection': '', 'Subject': 'ISBN não encontrado', 'Authors': '' } livro_json['estante'] = estante writer.writerow(livro_json) csv_out.flush() livro_json_fields = { field: livro_json[field] for field in [*fields.split(','), 'estante'] } livro_json_fields['Authors'] = ','.join(livro_json_fields['Authors']) livro = Livro(**livro_json_fields) session.add(livro) session.commit() reply_text = livro_json['RowKey'] + ' - ' + livro_json['Title'] except Exception as err: reply_text = 'ERRO:' + str(err) logger.error(err, exc_info=True) update.message.reply_text(reply_text)
async def connect(self): await asyncio.sleep(self.delay) async with aiohttp.ClientSession() as session: while True: self.has_sent_sign = True try: if config.use_proxy: host, port, username, password = random.choice( g.proxy_list).split(':') proxy = f'http://{username}:{password}@{host}:{port}' else: proxy = '' async with session.ws_connect(self.ws_url, proxy=proxy) as ws: await self.on_connect(ws) #except TimeoutError: # ... except Exception as e: log.error( f'ID({self.vk_id}) disconnected. Reconnecting.\nLast message: {self.msg}\nException: {e}' ) try: self.task.cancel() except Exception as e: log.error('Internet problems.') self.alive = False if self.bruh: return await asyncio.sleep(3)
def get_zhihu_video_download_url(video_url): page_source = utils.get_page_source(video_url) src = etree.HTML(page_source).xpath( '//*[@id="player"]/div/div/div[1]/video/@src') if len(src) == 0: logger.error('没有发现视频链接') return None return src[0]
async def get(url): try: async with aiohttp.ClientSession() as session: async with session.get(url) as resp: res = await resp.text() return json.loads(res) except Exception as e: log.error(f'Exception in _.get: {e}')
def consulta_Livro(update, context): try: text = update.message.text.strip() + '%' rows = session.query(Livro).filter( or_(Livro.RowKey.like(text) , Livro.Title.like(text)) ).limit(10).all() reply_text = '\n'.join(list(rows)) except Exception as err: reply_text = 'ERRO:' + str(err) logger.error(err, exc_info=True) update.message.reply_text(reply_text)
async def send_message(message): url = ENDPOINT_TEMPLATE.format(bot_api_key=config.TELEGRAM_BOT_TOKEN) params = { 'chat_id': config.CHAT_ID, 'text': message, } async with aiohttp.ClientSession() as session: async with session.get(url, params=params) as resp: if resp.status != HTTPStatus.OK: text = await resp.text() logger.error(f'Error {resp.status}: {text}')
async def post(url, data=None): try: async with aiohttp.ClientSession() as session: async with session.post( url, json=data, headers={'Authorization': 'Basic YWxsbzp3RmZMckxxYg=='}) as resp: res = await resp.text() return json.loads(res) except Exception as e: log.error(f'Exception in _.post: {e}')
def sec_exception(e): """ SecExcpetion """ try: addr = request.remote_addr logger.error("{}: {}:".format(session.get('username'), addr), exc_info=1) except Exception as ec: logger.exception(ec) resp = make_response(Resp(Resp.ERROR, str(e)).to_json(), 200) resp.headers['Content-Type'] = "application/json" return resp
def find_description(self): try: line = self.id_line_number + 1 word = None while line < self.pass_id: word = self.get_current_line(str(line)) line += 1 word = re.sub(r"\n", " ", word[:-1]) self.description = word except TypeError as e: logger.error(f"Error happened in {self.file}") logger.error(e) raise Exception("Error happened when getting cert description")
async def main(): g.stats = Stats() if config.mode == 1: proxy_f = '/app/proxies.list' else: proxy_f = 'proxies.list' with open(proxy_f) as f: g.proxy_list = f.read().splitlines() if config.mode == 1: matb = MotherBase(mother_api) try: ensure_future(matb.get_users()) ensure_future(matb.run_interaction_loop()) while True: await asyncio.sleep(1) except Exception as e: log.error(f'Main thread exception: {e}') finally: await matb.shutdown() elif config.mode == 2: with open(f'../../core/db/{config.single_db}') as f: users = json.loads(f.read()) for user in users: g.user_handler.append(User(user['url'], user['user_id'], randint(1, 3))) with open(f'../../core/tasks/{config.single_task}') as f: tasks = json.loads(f.read()) # shuffle(tasks) g.task_handler.extend(tasks) while True: tracemalloc.start() await asyncio.sleep(120) snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno') print("[ Top 10 ]") for stat in top_stats[:10]: print(stat) else: log.error('Unknown launch mode.')
def wrapped(*args, **kwargs): reference.Reference().create() try: token = request.json['token'] except Exception: reference.Reference().clean() return ready_for_output({ 'status': 'error', 'message': 'API token is required' }) try: success = session.State().authorize(token) if not success: return ready_for_output({ 'status': 'error', 'message': 'Invalid API token' }) result = f(*args, **kwargs) except Exception as e: logger.error(str(e)) result = ready_for_output({ 'status': 'error', 'message': 'Unknown API error' }) session.State().clean() reference.Reference().clean() return result result = ready_for_output(result) session.State().clean() reference.Reference().clean() return result
async def process_message(self, ws, msg): # log.debug(f'ws_onmessage: {msg}') self.msg = msg # if 'pong' not in msg: # await ws.send_str('ping') if msg == 'BAD_CLIENT_BAN': log.error(f'USER {self.vk_id} RECEIVED BAD_CLIENT_BAN!') self.bruh = True g.bruh_moment += 1 return try: data = json.loads(msg) except: return if 'v' in data and type(data['v']) == int: return # спасибо иванн недвезкии if 'code' in data['v']: try: pw = sign.get_sign(data['v']['code']) except: log.error(f'{self.vk_id} bruh moment') await ws.close() return try: await ws.send_str('R' + str(pw)) except: log.error( f'User {self.vk_id} disconnected before sign was sent') self.has_sent_sign = True
def wrapped(*args, **kwargs): reference.Reference().create() try: result = f(*args, **kwargs) except Exception as e: logger.error(str(e)) result = ready_for_output({ 'status': 'error', 'message': 'Unknown API error' }) reference.Reference().clean() return result result = ready_for_output(result) reference.Reference().clean() return result
def _beam_compile(self): """This function will take the existing beam setup and reduce the list to a smaller number""" for beam_type in self.beams.keys(): try: beam = self.beams.get(beam_type)[0] beams = self.beams.get(beam_type) temp = [] stop = len(beams) main = 0 lengths = [] while main < stop: if beam is not None: temp.append(beam) else: print(f"Beam was {beam}") main += 1 counter = 0 next_beam = None for b in beams: if b == beam: counter += 1 elif next_beam is None and counter > 0: next_beam = b lengths.append(counter) beam = next_beam if lengths[-1] == 0: break temp[-1]["qty"] = lengths[-1] self.beams[beam_type] = temp except IndexError as err: logger.error("Out of index error") logger.error(err) except Exception as err: logger.error("A really bad error happened") logger.error(err)
def make_file_changes(self, log): with open(log, "a") as info: try: name = Path(self.file.parent, self.new_name + ".pdf") self.file.replace(name) # print(Path(self.file.parent, self.new_name + ".pdf")) info.write(name.name + "\n") except TypeError as err: logger.error(err) except OSError as err: logger.info("Error renaming File?") print("Error renaming file ?") print(err) except Exception as err: logger.error("Big error happened") logger.error(err)
def handle_msg(msg): text = msg.text chat_id = msg.chat.id # 正则匹配 IP 地址 if utils.check_ip(text): # 查询 IP 地址 addr = services.query_ip(text) bot.send_message(chat_id=chat_id, text='该 IP 所处位置为: {}'.format(addr)) # 检测是否为包含 BV 号 elif text.startswith('BV') or text.startswith('bv') or text.startswith( 'https://b23.tv') or text.find('www.bilibili.com/video') != -1: bv = text if text.startswith('https://b23.tv'): bv = text[15:27] if text.find('www.bilibili.com/video') != -1: bv = re.findall('www.bilibili.com/video/(.*?)\?', text, re.S)[0] keyboard = [[ InlineKeyboardButton( "下载封面", callback_data='download_bilibili_cover:{}'.format(bv)), InlineKeyboardButton( "下载视频", callback_data='download_bilibili_video:{}'.format(bv)) ]] reply_markup = InlineKeyboardMarkup(keyboard) bot.send_message(chat_id=msg.chat.id, text='检测到 Bilibili 链接,您想让小关干嘛呢?', reply_markup=reply_markup) # 检测是否为淘口令、京东链接等 elif text.find('https://m.tb.cn/') != -1 or text.startswith( 'https://item.m.jd.com/product'): bot.send_message(chat_id=chat_id, text='{}正在为您查询该商品的价格哦,请稍等哈!'.format(config.BOT_NAME)) if text.find('https://m.tb.cn/') != -1: # 从淘口令中提取短连接 tb_id = re.findall('https://m\.tb\.cn/(.*?)\?sm=', text, re.S)[0] item_link = 'https://m.tb.cn/{}'.format(tb_id) elif text.startswith('https://item.m.jd.com/product'): item_link = text else: logger.error('无效的商品链接: {}'.format(text)) bot.send_message(chat_id=msg.chat.id, text='无效的商品链接') return img_path, suggestion = services.query_price(item_link) if not img_path and not suggestion: # 暂未收录该商品 bot.send_message(chat_id=msg.chat.id, text='该商品暂未被收录哦!') else: bot.send_photo(chat_id=msg.chat.id, photo=open(img_path, 'rb')) bot.send_message(chat_id=msg.chat.id, text=suggestion) # 检测是否为知乎视频的分享链接 elif text.find('https://www.zhihu.com/zvideo/') != -1: bot.send_message(chat_id=chat_id, text='{}正在为您搜寻,稍后会将下载链接呈上 😊😊😊'.format( config.BOT_NAME)) zvideo_id = re.findall('https://www.zhihu.com/zvideo/(.*?)\?', text, re.S)[0] video_src_url = services.get_zhihu_video_link(zvideo_id) logger.info('Find video ') bot.send_message(chat_id=chat_id, text=video_src_url) # 检测是否为知乎答案的分享链接 elif text.find('https://www.zhihu.com/question/') != -1: question_url = 'https://{}'.format( re.findall('https://(.*?)\?', text, re.S)[0]) download_list = services.get_download_list_by_question_url( question_url) keyboard = [] for each in download_list: keyboard.append( InlineKeyboardButton( text=each['title'], callback_data='download_zhihu_video:{}'.format( each['video_id']))) reply_markup = InlineKeyboardMarkup([keyboard]) bot.send_message(chat_id=msg.chat.id, text='请选择你要下载的视频:', reply_markup=reply_markup) # 处理类似 /help 命令的文本 elif text.startswith('/'): command = text[1:] bot.send_message(chat_id=chat_id, text=handle_command(command)) elif text.find('你好') != -1: bot.send_message(chat_id=chat_id, text='你好啊,我的名字叫{},很高兴认识你!'.format(config.BOT_NAME)) else: bot.send_message(chat_id=chat_id, text=text)
def ready_for_output(result): if result['status'] != 'ok': logger.error(result['message']) return jsonify(result)