async def export_chat_data(message, chat, strings): chat_id = chat['chat_id'] key = 'export_lock:' + str(chat_id) if redis.get(key) and message.from_user.id not in OPERATORS: ttl = format_timedelta(timedelta(seconds=redis.ttl(key)), strings['language_info']['babel']) await message.reply(strings['exports_locked'] % ttl) return redis.set(key, 1) redis.expire(key, 7200) msg = await message.reply(strings['started_exporting']) data = { 'general': { 'chat_name': chat['chat_title'], 'chat_id': chat_id, 'timestamp': datetime.now(), 'version': 1 } } for module in [m for m in LOADED_MODULES if hasattr(m, '__export__')]: await asyncio.sleep(0.2) data.update(await module.__export__(chat_id)) jfile = InputFile(io.StringIO(ujson.dumps(data, indent=2)), filename=f'{chat_id}_export.json') text = strings['export_done'] % chat['chat_title'] await message.answer_document(jfile, text, reply=message.message_id) await msg.delete()
async def candle(message: types.Message, regexp_command): chat_id = message.chat.id try: coin = regexp_command.group(1) trades = get_ohcl_trades(coin, 180) trades = trades[-60:] df = pd.DataFrame( trades, columns='time open high low close volume amount'.split()) df['time'] = pd.DatetimeIndex(df['time'] * 10**9) df.set_index('time', inplace=True) df['MA20'] = df['close'].rolling(window=20).mean() df['20dSTD'] = df['close'].rolling(window=20).std() df['Upper'] = df['MA20'] + (df['20dSTD'] * 2) df['Lower'] = df['MA20'] - (df['20dSTD'] * 2) rsi_df = get_rsi_df(df) df = df.tail(30) rsi_df = rsi_df.tail(30) apd = [ mpf.make_addplot(df['Lower'], color='#EC407A', width=0.9), mpf.make_addplot(df['Upper'], color='#42A5F5', width=0.9), mpf.make_addplot(df['MA20'], color='#FFEB3B', width=0.9) ] if rsi_df is not None: apd.append( mpf.make_addplot(rsi_df, color='#FFFFFF', panel=1, ylabel='RSI', ylim=[0, 100])) kwargs = dict(type='candle', ylabel=coin.upper() + ' Price in $', volume=True, figratio=(3, 2), figscale=1.5, addplot=apd) mpf.plot(df, **kwargs, style='nightclouds') mc = mpf.make_marketcolors(up='#69F0AE', down='#FF5252', inherit=True) s = mpf.make_mpf_style(base_mpf_style='nightclouds', facecolor='#121212', edgecolor="#131313", gridcolor="#232323", marketcolors=mc) mpf.plot(df, **kwargs, style=s, scale_width_adjustment=dict(volume=0.55, candle=0.8), savefig=coin + '-mplfiance.png') await bot.send_photo(chat_id=chat_id, photo=InputFile(coin + '-mplfiance.png')) except Exception as e: logging.error("ERROR Making chart:" + str(e)) await bot.send_message(chat_id=chat_id, text="Failed to create chart", parse_mode="HTML")
def get_data_for_time_interval(cls, user_id, interval): query = """ SELECT exercise, type_ex, sum(sets * repetition), sum(duration_training), date_insert FROM data_training """ params = None if interval == cls.TIME_INTERVALS[0]: # за "сегодня" date_today = dt.today().strftime('%Y-%m-%d') params = (date_today, date_today, user_id) elif interval == cls.TIME_INTERVALS[1]: # за "неделя" date_today = dt.today().strftime('%Y-%m-%d') week_ago = (dt.today() - td(days=7)).strftime('%Y-%m-%d') params = (week_ago, date_today, user_id) elif interval == cls.TIME_INTERVALS[2]: # за "месяц" date_today = dt.today().strftime('%Y-%m-%d') month_ago = (dt.today() - relativedelta(months=1)).strftime('%Y-%m-%d') params = (month_ago, date_today, user_id) elif interval == cls.TIME_INTERVALS[3]: # за "год" date_today = dt.today().strftime('%Y-%m-%d') year_ago = (dt.today() - relativedelta(years=1)).strftime('%Y-%m-%d') params = (year_ago, date_today, user_id) query += "WHERE date_insert between ? and ? and user_id = ?" query += "\nGROUP by exercise, type_ex, date_insert\nORDER BY date_insert" data = cls.db.sql_query_execute(query, params) if data: table = cls._transform_data(data) else: return None, None table_for_output = cls._pretty_table(table) if len(data) > 10: # больше 10 строк в таблице с данными # формируем файл с таблицей with open('data.txt', 'w', encoding='utf-8') as f: f.write(table_for_output) file = InputFile('data.txt') return file, cls.TYPE_DATA_OUTPUT[0] else: # формируем текст с таблицей msg = table return msg, cls.TYPE_DATA_OUTPUT[1]
async def sendWelcome(message: types.Message): user_id = message.from_user.id if in_categories_database(message.text): await bot.send_document(user_id, InputFile(path_or_bytesio="docs.pdf")) else: if in_database(user_id) or (user_id in admins): await bot.send_message( user_id, "Меню: ", reply_markup=markup.keyboard_categories(user_id))
async def gan_start(message: types.Message): await GanStates.waiting_model.set() await message.answer( '''You have chosen *Style Gan* model. To do transformation I use `cyclegan`: https://github.com/junyanz/CycleGAN based on paper: https://arxiv.org/pdf/1703.10593.pdf. I train model on google colab with 100 epoch, using following datesets (training one model was taking 5-10 hours depending on the data size): - summer2winter (133mb | 2740 images 256x256) - monet2photo (310mb | 8231 images 256x256) - vangogh2photo (309mb | 7838 images 256x256)''', reply_markup=gan_kb, reply=False, disable_web_page_preview=True, parse_mode='Markdown') photo = InputFile(path_or_bytesio='bot/Algos/cyclegan/help-images/model_choose.png') await bot.send_photo(message.chat.id, photo, 'Examples')
async def create_photo(message: types.Message): await message.answer('get it') import subprocess subprocess.run( 'python3 bot/Algos/vanila_nst/main.py eval --content-image data/content.jpg --style-image data/style2.jpg --model bot/Algos/vanila_nst/models/21styles.model --content-size 200 --cuda=0', shell=True) print('i did it') print(os.listdir()) photo = InputFile(path_or_bytesio='output.jpg') await bot.send_photo(message.chat.id, photo)
async def uploadInputFileToTelegram(imgPath, bot): chatID = adminUserID img = InputFile(imgPath, filename='qr' + str(time())) imgMessage = await bot.send_photo( photo=img, chat_id=chatID, disable_notification=True, ) imgFileId = imgMessage.photo[-1].file_id return imgFileId
async def create_photo(message: types.Message): r = requests.post( "https://api.deepai.org/api/fast-style-transfer", files={ 'content': open('data/content.jpg', 'rb'), 'style': open('data/style.jpg', 'rb'), }, headers={'api-key': 'quickstart-QUdJIGlzIGNvbWluZy4uLi4K'} ) img = wget.download(r.json()['output_url']) # , out='output.jpg') photo = InputFile(path_or_bytesio=img) await bot.send_photo(message.chat.id, photo, 'result')
async def get_sticker(message, strings): if 'reply_to_message' not in message or 'sticker' not in message.reply_to_message: await message.reply(strings['rpl_to_sticker']) return sticker = message.reply_to_message.sticker file_id = sticker.file_id text = strings['ur_sticker'].format(emoji=sticker.emoji, id=file_id) sticker_file = await bot.download_file_by_id(file_id, io.BytesIO()) await message.reply_document( InputFile(sticker_file, filename=f'{sticker.set_name}_{sticker.file_id[:5]}.png'), text)
async def catch_text(message: types.Message): await message.answer("Give me a second_1") meme_text = message.text image = Image.open(PHOTO_PATH) font_type = ImageFont.truetype('arial.ttf', size=45) draw = ImageDraw.Draw(image) width, height = Image.open(PHOTO_PATH).size w, h = draw.textsize(meme_text) draw.text(((width - w) * 0.5, (height - h) * 0.75), text=meme_text, font=font_type, fill='black') image.save(expanduser(PHOTO_PATH)) photo_meme = InputFile(path_or_bytesio=PHOTO_PATH) await bot.send_photo(chat_id=message.from_user.id, photo=photo_meme)
async def get_result_image(message: types.Message, state: FSMContext): """Loading the resulting infographic.""" await message.answer('Please wait until calculations are finished', reply_markup=ReplyKeyboardRemove()) image_path = f'images/input_{message.from_user.id}.jpg' output_image_path = f'images/output_{message.from_user.id}.jpg' number_of_colors = int(message.text) await async_business_logic_call.async_get_colors( image=image_path, number_of_colors=number_of_colors, create_image=True, output_image_path=output_image_path ) output_image = InputFile(output_image_path) await bot.send_photo(message.from_user.id, output_image) await state.finish()
async def sertificate_generator(config): font_size = 14 coord = 400 file_name = f"{config['fio']}.pdf" c = canvas.Canvas(file_name, pagesize=A4) c.setFont('Normal', 14) c.setTitle(config['fio']) c.drawImage(background, 0, 0, width=width, height=height) c.drawString(75, 535, "подтверждает, что ") c.drawString(75, 440, f"принял(а) участие в {config['event_type']}") for line in config['event'].splitlines(): c.drawString(75, coord, line) coord -= int(font_size * 1.5) c.drawString(300, 280, f'дата выдачи «{config["day"]}» ' f'{config["month_year"]} г.') c.drawString(75, 170, f'Директор {" " * 80} А.Н. Слизько') c.drawString(235, 120, f'г. Екатеринбург') c.setFont('Bold', name_size(config['fio'], 'Bold', 24)) c.drawString(75, 485, config['fio']) c.setFillColorRGB(0.898, 0.227451, 0.1412) c.setFont('Normal', 32) c.drawString(70, 580, "СЕРТИФИКАТ") c.save() pdf = InputFile(file_name) if config['mail']: try: server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login("*****@*****.**", getenv('MAIL_PASS')) msg = MIMEMultipart() msg['From'] = "*****@*****.**" msg['To'] = config['mail'] msg['Subject'] = f'Сертификат {config["event"]}' with open(f"{config['fio']}.pdf", "rb") as pdf_file: attach = MIMEApplication(pdf_file.read(), _subtype="pdf") attach.add_header('Content-Disposition', 'attachment', filename=file_name) msg.attach(attach) server.send_message(msg) server.quit() except: await send_message(config['chat_id'], f'Ошибка отправки письма {config["mail"]}, {config["fio"]}') log.exception(f'Ошибка отправки письма {config["mail"]}, {config["fio"]}') await bot.send_document(config['chat_id'], pdf, caption=config['fio']) os.remove(file_name)
async def chart(message: types.Message): chat_id = message.chat.id try: trades = get_last_trades(500) points = [] for t in trades: points.append(t[2]) chart = pygal.StackedLine( fill=True, interpolate='cubic', style=DefaultStyle) # Setting style here is not necessary chart.add('BTC', points) chart.render_sparkline(width=500, height=25, show_dots=False) chart.render_to_png('chart.png') await bot.send_photo(chat_id=chat_id, photo=InputFile('chart.png')) except Exception as e: logging.error("ERROR Making chart:" + str(e)) await bot.send_message(chat_id=chat_id, text="Failed to create chart", parse_mode="HTML")
async def create_photo(message: types.Message): # await message.answer(f'I get your two pictures, get the model: {chosen_model}.' # f' Starting to download pretrained model... (pls wait ~3 min)', ) await message.answer('Starting to work(~3 min)..') shutil.move('data/img1.jpg', 'bot/Algos/cyclegan/datasets/test_root/testA/img1.jpg') shutil.move('data/img2.jpg', 'bot/Algos/cyclegan/datasets/test_root/testB/img2.jpg') ######## if chosen_model == 'summer_to_winter': link = '1tbYQNGBW3zrBZTl2Om3_IEN2tmvA1rmF' # скачиваем образ1 elif chosen_model == 'vangogh': link = '1_PVn400veyxwPWfdQLlE3K9PzHxS3VxI' elif chosen_model == 'monet': link = '1ix3sZzC1zKsvv1S3xihqHqKypqwHyrI9' ########## gdd.download_file_from_google_drive(file_id=link, dest_path='bot/Algos/cyclegan/checkpoints/model.ckpt', showsize=True) os.chdir('bot/Algos/cyclegan') import subprocess subprocess.run( 'python3 main.py --testing True --dataset_dir datasets/test_root --checkpoint_dir checkpoints ' '--gpu_ids -1 --crop_height 500 --crop_width 500 --idt_coef 0.1 --checkpoint checkpoints/model.ckpt', shell=True) photo = InputFile(path_or_bytesio='results/sample.jpg') os.chdir('../../..') await bot.send_photo(message.chat.id, photo, "your res:")
def form_input_file(src: str, filename: str = "document") -> InputFile: tmp = BytesIO() tmp.write(bytes_from_str(src)) tmp.seek(0) return InputFile(tmp, filename=filename)
async def send_image(chat_id, coin, convert, period_seconds, period_counts): trades = get_ohcl_trades(coin, period_seconds, "binance", convert) if trades is None: logging.error("Trades is empty") raise Exception("No data found for coin: " + coin) ranger = -2 * period_counts trades = trades[ranger:] df = pd.DataFrame(trades, columns='time open high low close volume amount'.split()) df['time'] = pd.DatetimeIndex(df['time'] * 10**9) df.set_index('time', inplace=True) df['MA20'] = df['close'].rolling(window=20).mean() df['20dSTD'] = df['close'].rolling(window=20).std() df['Upper'] = df['MA20'] + (df['20dSTD'] * 2) df['Lower'] = df['MA20'] - (df['20dSTD'] * 2) rsi_df = get_rsi_df(df) rsi_df = rsi_df.tail(int(period_counts)) df = df.tail(int(period_counts)) h_lines, y_min, y_max = fibs(df, extend=True) apd = [ mpf.make_addplot(df['Lower'], color='#EC407A', width=0.9), mpf.make_addplot(df['Upper'], color='#42A5F5', width=0.9), mpf.make_addplot(df['MA20'], color='#FFEB3B', width=0.9) ] if rsi_df is not None: apd.append( mpf.make_addplot(rsi_df, color='#FFFFFF', panel=1, y_on_right=True, ylabel='RSI')) kwargs = dict(type='candle', ylabel=coin.upper() + ' Price in ' + convert.upper(), volume=True, volume_panel=1, figratio=(3, 2), figscale=1.5, addplot=apd, ylim=[y_min, y_max]) mpf.plot(df, **kwargs, style='nightclouds') mc = mpf.make_marketcolors(up='#69F0AE', down='#FF5252', inherit=True) s = mpf.make_mpf_style(base_mpf_style='nightclouds', facecolor='#121212', edgecolor="#131313", gridcolor="#232323", marketcolors=mc) mpf.plot(df, **kwargs, style=s, scale_width_adjustment=dict(volume=0.55, candle=0.8), savefig=coin + convert + '-mplfiance.png', hlines=h_lines) await bot.send_photo(chat_id=chat_id, photo=InputFile(coin + convert + '-mplfiance.png'))
async def fibs_chart(message: types.Message, regexp_command): chat_id = message.chat.id try: inputs = regexp_command.group(1) splits = inputs.split() coin = splits[0] period_seconds = 60 period_counts = 80 if len(splits) > 1: period_seconds = splits[1] if period_seconds.lower() == "m" or period_seconds.lower() == "1m": period_seconds = 60 elif period_seconds.lower() == "3m": period_seconds = 180 elif period_seconds.lower() == "5m": period_seconds = 300 elif period_seconds.lower() == "15m": period_seconds = 900 elif period_seconds.lower() == "30m": period_seconds = 1800 elif period_seconds.lower() == "60m" or period_seconds.lower( ) == "h" or period_seconds.lower() == "1h": period_seconds = 3600 elif period_seconds.lower() == "2h": period_seconds = 7200 elif period_seconds.lower() == "4h": period_seconds = 14400 elif period_seconds.lower() == "6h": period_seconds = 21600 elif period_seconds.lower() == "12h": period_seconds = 43200 elif period_seconds.lower() == "24h" or period_seconds.lower( ) == "d" or period_seconds.lower() == "1d": period_seconds = 86400 elif period_seconds.lower() == "3d": period_seconds = 259200 elif period_seconds.lower() == "7d" or period_seconds.lower( ) == "w" or period_seconds.lower() == "1w": period_seconds = 604800 elif period_seconds.isnumeric(): period_seconds = int(period_seconds) else: return await bot.send_message( chat_id=chat_id, text= "Failed to create chart, your period in seconds is not 1M, 3M, 5M, 15M, 30M, 1H, 2H, 4H, 6H, 12H, 1D, 3D, 1W of a number in seconds like 60, 180, 108000 etc" ) if len(splits) == 3: period_counts = splits[2] if period_counts.isnumeric(): period_counts = int(period_counts) else: return await bot.send_message( chat_id=chat_id, text= "Failed to create chart, your range is not a number, try 60 etc", parse_mode="HTML") trades = get_ohcl_trades(coin, period_seconds) ranger = -2 * period_counts trades = trades[ranger:] df = pd.DataFrame( trades, columns='time open high low close volume amount'.split()) df['time'] = pd.DatetimeIndex(df['time'] * 10**9) df.set_index('time', inplace=True) df['MA20'] = df['close'].rolling(window=20).mean() df['20dSTD'] = df['close'].rolling(window=20).std() df['Upper'] = df['MA20'] + (df['20dSTD'] * 2) df['Lower'] = df['MA20'] - (df['20dSTD'] * 2) rsi_df = get_rsi_df(df) rsi_df = rsi_df.tail(int(period_counts)) df = df.tail(int(period_counts)) h_lines, y_min, y_max = fibs(df) apd = [ mpf.make_addplot(df['Lower'], color='#EC407A', width=0.9), mpf.make_addplot(df['Upper'], color='#42A5F5', width=0.9), mpf.make_addplot(df['MA20'], color='#FFEB3B', width=0.9) ] if rsi_df is not None: apd.append( mpf.make_addplot(rsi_df, color='#FFFFFF', panel=1, ylabel='RSI', y_on_right=True, ylim=[0, 100])) if y_min is None: kwargs = dict(type='candle', ylabel=coin.upper() + ' Price in $', volume=True, figratio=(3, 2), figscale=1.5, addplot=apd) else: kwargs = dict(type='candle', ylabel=coin.upper() + ' Price in $', volume=True, figratio=(3, 2), figscale=1.5, addplot=apd, ylim=[y_min, y_max]) mpf.plot(df, **kwargs, style='nightclouds') mc = mpf.make_marketcolors(up='#69F0AE', down='#FF5252', inherit=True) s = mpf.make_mpf_style(base_mpf_style='nightclouds', facecolor='#121212', edgecolor="#131313", gridcolor="#232323", marketcolors=mc) mpf.plot(df, **kwargs, style=s, scale_width_adjustment=dict(volume=0.55, candle=0.8), savefig=coin + '-mplfiance.png', hlines=h_lines) await bot.send_photo(chat_id=chat_id, photo=InputFile(coin + '-mplfiance.png')) except Exception as e: logging.error("ERROR Making chart:" + str(e)) await bot.send_message(chat_id=chat_id, text="Failed to create chart", parse_mode="HTML")
data = { "general": { "chat_name": chat["chat_title"], "chat_id": chat_id, "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "version": VERSION, } } for module in [m for m in LOADED_MODULES if hasattr(m, "__export__")]: await asyncio.sleep(0) # Switch to other events before continue if k := await module.__export__(chat_id): data.update(k) jfile = InputFile( io.StringIO(rapidjson.dumps(data, indent=2)), filename=f"{chat_id}_export.json" ) text = strings["export_done"].format(chat_name=chat["chat_title"]) await message.answer_document(jfile, text, reply=message.message_id) await msg.delete() @register(cmds="import", user_admin=True) @get_strings_dec("imports_exports") async def import_reply(message, strings): if "document" in message: document = message.document else: if "reply_to_message" not in message: await ImportFileWait.waiting.set() await message.reply(strings["send_import_file"])
msg = await message.reply(strings['started_exporting']) data = { 'general': { 'chat_name': chat['chat_title'], 'chat_id': chat_id, 'timestamp': datetime.now(), 'version': VERSION } } for module in [m for m in LOADED_MODULES if hasattr(m, '__export__')]: await asyncio.sleep(0.2) if k := await module.__export__(chat_id): data.update(k) jfile = InputFile(io.StringIO(ujson.dumps(data, indent=2)), filename=f'{chat_id}_export.json') text = strings['export_done'] % chat['chat_title'] await message.answer_document(jfile, text, reply=message.message_id) await msg.delete() @register(cmds='import', user_admin=True) @get_strings_dec('imports_exports') async def import_reply(message, strings): if 'document' in message: document = message.document else: if 'reply_to_message' not in message: await ImportFileWait.waiting.set() await message.reply(strings['send_import_file']) return
async def blank_generator(config): coord = 580 font_type = 'Normal' auto_size = False font_size = 14 file_name = "document.pdf" c = canvas.Canvas(file_name, pagesize=A4) for line in config['event'].split('\n'): if line == '{back}': c.drawImage(background, 0, 0, width=width, height=height) elif re.match(r'\[[0-9]+]', line): font_size = int(line[1, -1]) auto_size = False elif re.match(r'\[[0-9]+a]', line): font_size = int(line[1, -2]) auto_size = True elif line == '[r]': c.setFillColorRGB(0.898, 0.227451, 0.1412) elif line == '[b]': c.setFillColorRGB(0, 0, 0) elif line == '[n]': font_type = 'Normal' elif line == '[f]': font_type = 'Bold' elif re.match(r'<[0-9]+>', line): coord -= int(line[1, -1]) else: if auto_size: auto_sized = name_size(line, font_type, font_size) c.setFont(font_type, auto_sized) c.drawString(70, coord, line) coord -= int(auto_sized * 1.5) else: c.setFont(font_type, font_size) text = await text_splitter(line, font_type, font_size) text = text.split('\n') for string in text: c.drawString(70, coord, string) coord -= int(font_size * 1.5) c.save() pdf = InputFile(file_name) if config['mail']: try: server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login("*****@*****.**", getenv('MAIL_PASS')) msg = MIMEMultipart() msg['From'] = "*****@*****.**" msg['To'] = config['mail'] msg['Subject'] = f'Сертификат {config["event"]}' with open(f"{config['fio']}.pdf", "rb") as pdf_file: attach = MIMEApplication(pdf_file.read(), _subtype="pdf") attach.add_header('Content-Disposition', 'attachment', filename=file_name) msg.attach(attach) server.send_message(msg) server.quit() except: await send_message(config['chat_id'], f'Ошибка отправки письма {config["mail"]}, {config["fio"]}') log.exception(f'Ошибка отправки письма {config["mail"]}, {config["fio"]}') await bot.send_document(config['chat_id'], pdf, caption=config['fio']) os.remove(file_name)
def form_input_file(src: str) -> InputFile: tmp = BytesIO() tmp.write(bytes_from_str(src)) tmp.seek(0) return InputFile(tmp)
def GroupElement(path): return InputMediaDocument(InputFile(path))
def GroupElement(path): return InputMediaPhoto(InputFile(path))