async def stats(message): text = f"<b>Sophie {SOPHIE_VERSION} stats</b>\n" text += "* <code>{}</code> total users, in <code>{}</code> chats\n".format( await db.user_list.count_documents({}), await db.chat_list.count_documents({})) text += "* <code>{}</code> new users and <code>{}</code> new chats in the last 48 hours\n".format( await db.user_list.count_documents({ 'first_detected_date': { '$gte': datetime.datetime.now() - datetime.timedelta(days=2) } }), await db.chat_list.count_documents({ 'first_detected_date': { '$gte': datetime.datetime.now() - datetime.timedelta(days=2) } })) text += "* <code>{}</code> total notes\n".format( await db.notes.count_documents({})) text += "* <code>{}</code> total warns\n".format( await db.warns.count_documents({})) text += "* <code>{}</code> total gbanned users\n".format( await db.blacklisted_users.count_documents({})) text += "* <code>{}</code> chats in <code>{}</code> total feds, <code>{}</code> fbanned users\n".format( await db.fed_groups.count_documents({}), await db.fed_list.count_documents({}), await db.fbanned_users.count_documents({})) text += "* <code>{}</code> total crash happened in this week\n".format( await db.errors.count_documents({ 'date': { '$gte': datetime.datetime.now() - datetime.timedelta(days=7) } })) local_db = await db.command("dbstats") if 'fsTotalSize' in local_db: text += '* Database size is <code>{}</code>, free <code>{}</code>\n'.format( convert_size(local_db['dataSize']), convert_size(local_db['fsTotalSize'] - local_db['fsUsedSize'])) else: text += '* Database size is <code>{}</code>, free <code>{}</code>\n'.format( convert_size(local_db['storageSize']), convert_size(536870912 - local_db['storageSize'])) text += "* <code>{}</code> total keys in Redis database\n".format( len(redis.keys())) await message.reply(text)
async def do_backup(chat_id, reply=False): await bot.send_message(chat_id, "Dumping the DB, please wait...", reply_to_message_id=reply) date = strftime("%Y-%m-%d_%H:%M:%S", gmtime()) file_name = f"Backups/dump_{date}.7z" if not os.path.exists("Backups/"): os.mkdir("Backups/") await term( f"mongodump --uri \"{CONFIG['basic']['mongo_conn']}\" --out=Backups/tempbackup" ) # Let's also save Redis cache with open('Backups/tempbackup/redis_keys.json', 'w+') as f: keys = redis.keys() new = {} for key in keys: key_type = redis.type(key) if key_type == 'string': new[key] = redis.get(key) elif key_type == 'list': new[key] = list(redis.lrange(key, 0, -1)) f.write(ujson.dumps(new, indent=2)) # Copy config file shutil.copyfile('data/bot_conf.json', 'Backups/tempbackup/bot_conf.json') await bot.send_message(chat_id, "Compressing and uploading to Telegram...", reply_to_message_id=reply) password = CONFIG['advanced']['backups_password'] await term( f"cd Backups/tempbackup/; 7z a -mx9 ../../{file_name} * -p{password} -mhe=on" ) shutil.rmtree('Backups/tempbackup') if not os.path.exists(file_name): await bot.send_message(chat_id, "Error!", reply_to_message_id=reply) return text = "<b>Backup created!</b>" size = convert_size(os.path.getsize(file_name)) text += f"\nBackup name: <code>{file_name}</code>" text += f"\nSize: <code>{size}</code>" await tbot.send_file(chat_id, file_name, reply_to=reply, caption=text, parse_mode="html")
async def upload_file(message, state, **kwargs): msg = await message.reply("Downloading file...") # Parse filename file_name = message.document.file_name check = re.match(r'OrangeFox\-([^\-]+)\-([^\-]+)\-([^\-]+).zip', file_name) if not check: await msg.edit_text('Error! File name isnt valid with regex pattern!') return async with state.proxy() as data: data['file_name'] = file_name data['build_ver'] = check.group(1) data['file_id'] = message.document.file_id data['build_date'] = int(time.time()) codename = check.group(3).lower() data['device_codename'] = codename if data['build_type'] == 'stable' and not check.group( 2).lower() == 'stable': await msg.edit_text( 'Error! Stable builds should be builded with <code>export BUILD_TYPE=Stable</code> tag!' ) return # Check device device = mongodb.ofox_devices.find_one({'codename': codename}) if not device: await state.finish() await message.answer( f"Device not found in database, please write <code>/changedevice {codename}</code>" ) return # Check on migrated device if 'status' not in device: await state.finish() await message.answer( f"This device was migrated, please write <code>/changedevice {codename}</code>" ) return # Set default builds vars if 'default_bugs' in device: data['build_bugs'] = device['default_bugs'] if 'default_notes' in device: data['special_notes'] = device['default_notes'] hash_md5 = hashlib.md5() hash_sha256 = hashlib.sha256() build_file = io.BytesIO(await tbot.download_media(message.document.file_id, file=bytes)) for chunk in iter(lambda: build_file.read(4096), b""): hash_md5.update(chunk) hash_sha256.update(chunk) async with state.proxy() as data: data['file_md5'] = hash_md5.hexdigest() data['file_sha256'] = hash_sha256.hexdigest() data['file_size'] = convert_size(build_file.getbuffer().nbytes) if not os.path.exists('OrangeFox-temp-builds/'): os.makedirs('OrangeFox-temp-builds/') with open('OrangeFox-temp-builds/' + file_name, 'wb') as f: f.write(build_file.getvalue()) build_file.close() text = 'Done! Please write changelog for this build. HTML markdown supported as well.' text += f'\bIf you upload first build - type just "Initial build for {codename}"' await ReleaseState.write_changelog.set() await msg.edit_text(text)