def tts_cache_check(self): min_file_size = 1024 max_size = self['cache'].get('tts_size', 50) * 1024 * 1024 cache_path = self.gt('cache', 'path') if not os.path.isdir(cache_path): msg = F('Директория c tts кэшем не найдена {}', cache_path) self.log(msg) self.own.say_info(msg) return current_size = 0 files, wrong_files = [], [] # Формируем список из пути и размера файлов, заодно считаем общий размер. # Файлы по 1 KiB считаем поврежденными и удалим в любом случае for file in os.listdir(cache_path): pfile = os.path.join(cache_path, file) if os.path.isfile(pfile): fsize = os.path.getsize(pfile) if fsize > min_file_size: current_size += fsize files.append([pfile, fsize]) else: wrong_files.append(pfile) # Удаляем поврежденные файлы if wrong_files: for file in wrong_files: os.remove(file) wrong_files = [os.path.split(file)[1] for file in wrong_files] self.log( F('Удалены поврежденные файлы: {}', ', '.join(wrong_files)), logger.WARN) normal_size = not files or current_size < max_size or max_size < 0 say = F('Размер tts кэша {}: {}', utils.pretty_size(current_size), F('Ок.') if normal_size else F('Удаляем...')) self.log(say, logger.INFO) if normal_size: return self.own.say_info(say) new_size = int(max_size * 0.7) deleted_files = 0 # Сортируем файлы по дате последнего доступа files.sort(key=lambda x: os.path.getatime(x[0])) deleted = [] for file in files: if current_size <= new_size: break current_size -= file[1] os.remove(file[0]) deleted_files += 1 deleted.append(os.path.split(file[0])[1]) self.log(F('Удалено: {}', ', '.join(deleted))) msg = F('Удалено {} файлов. Новый размер TTS кэша {}', deleted_files, utils.pretty_size(current_size)) self.log(msg, logger.INFO) self.own.say_info(msg)
def do_list_media(pipe): import mimetypes parent_pipe.close() mf = _list_media_files(target_dir, exts=exts, prefix=prefix) for (p, st) in mf: path = p[len(target_dir):] if not path.startswith('/'): path = '/' + path timestamp = st.st_mtime size = st.st_size pipe.send({ 'path': path, 'mimeType': mimetypes.guess_type(path)[0] if mimetypes.guess_type(path)[0] is not None else 'video/mpeg', 'momentStr': utils.pretty_date_time( datetime.datetime.fromtimestamp(timestamp)), 'momentStrShort': utils.pretty_date_time( datetime.datetime.fromtimestamp(timestamp), short=True), 'sizeStr': utils.pretty_size(size), 'timestamp': timestamp }) pipe.close()
def do_list_media(pipe): mf = _list_media_files(target_dir, exts=exts, prefix=prefix) for (p, st) in mf: path = p[len(target_dir):] if not path.startswith('/'): path = '/' + path timestamp = st.st_mtime size = st.st_size pipe.send({ 'path': path, 'momentStr': utils.pretty_date_time( datetime.datetime.fromtimestamp(timestamp)), 'momentStrShort': utils.pretty_date_time( datetime.datetime.fromtimestamp(timestamp), short=True), 'sizeStr': utils.pretty_size(size), 'timestamp': timestamp }) pipe.close()
def paste(id: str): paste = get_paste(id) text = paste.data.decode("utf-8") return render_template("paste.html", id=id, size=pretty_size(text), text=text)
def __backup(self, manual, remove_old) -> bool: def say(msg_: str): if manual: self.own.say(msg_, lvl=0) timestamp = time.time() if not manual: self._last_backup = timestamp self.cfg.save_state() filename = time.strftime('%Y.%m.%d-%H.%M.%S.zip', time.localtime(timestamp)) file_path = os.path.join(self.cfg.path['backups'], filename) fail_msg = F('Ошибка создания бэкапа') if os.path.exists(file_path): self.log(F('Файл {} уже существует, отмена.', filename), logger.ERROR) say('{} - {}'.format(fail_msg, F('файл уже существует'))) return False to_remove = self._candidates_to_remove() if remove_old else [] work_time = time.time() try: old_size, new_size = self._make_backup(timestamp, file_path) except Exception as e: self.log('{} {}:{}'.format(fail_msg, filename, e), logger.ERROR) say(fail_msg) try: os.remove(file_path) except IOError: pass return False work_time = time.time() - work_time rate = round(new_size / old_size, 2) old_size = utils.pretty_size(old_size) new_size = utils.pretty_size(new_size) work_time = utils.pretty_time(work_time) msg = F('Бэкап {} создан за {} [size: {}, compressed: {}, rate: {}%]', filename, work_time, old_size, new_size, rate) self.log(msg, logger.INFO) say(F('Бэкап успешно создан')) if to_remove: self._remove_candidates([x for x, _ in to_remove]) return True
def tts_cache_check(self): if not os.path.isdir(self.path['tts_cache']): self._print(msg='Директория c tts кэшем не найдена {}'.format( self.path['tts_cache']), mode=3) return max_size = self['cache'].get('tts_size', 50) * 1024 * 1024 current_size = 0 files = [] # Формируем список из пути и размера файлов, заодно считаем общий размер. for file in os.listdir(self.path['tts_cache']): pfile = os.path.join(self.path['tts_cache'], file) if os.path.isfile(pfile): fsize = os.path.getsize(pfile) current_size += fsize files.append([pfile, fsize]) normal_size = not files or current_size < max_size or max_size < 0 self._print( 'Размер tts кэша {}: {}'.format( utils.pretty_size(current_size), 'Ок.' if normal_size else 'Удаляем...'), logger.INFO, 1 if normal_size else 3) if normal_size: return new_size = int(max_size * 0.7) deleted_files = 0 # Сортируем файлы по дате последнего доступа files.sort(key=lambda x: os.path.getatime(x[0])) for file in files: if current_size <= new_size: break current_size -= file[1] self._print('Удаляю {}'.format(file[0])) os.remove(file[0]) deleted_files += 1 self._print( 'Удалено {} файлов. Новый размер TTS кэша {}.'.format( deleted_files, utils.pretty_size(current_size)), logger.INFO, 3)
def tts_cache_check(self): cache_path = self.gt('cache', 'path') if not os.path.isdir(cache_path): self._print(msg=LNG['miss_tts_cache'].format(cache_path), mode=3) return max_size = self['cache'].get('tts_size', 50) * 1024 * 1024 current_size = 0 files = [] # Формируем список из пути и размера файлов, заодно считаем общий размер. for file in os.listdir(cache_path): pfile = os.path.join(cache_path, file) if os.path.isfile(pfile): fsize = os.path.getsize(pfile) current_size += fsize files.append([pfile, fsize]) normal_size = not files or current_size < max_size or max_size < 0 say = LNG['tts_cache_size'].format( utils.pretty_size(current_size), LNG['tts_cache_act_list'][0] if normal_size else LNG['tts_cache_act_list'][1] ) self._print(say, logger.INFO, 1 if normal_size else 3) if normal_size: return new_size = int(max_size * 0.7) deleted_files = 0 # Сортируем файлы по дате последнего доступа files.sort(key=lambda x: os.path.getatime(x[0])) for file in files: if current_size <= new_size: break current_size -= file[1] self._print(LNG['delete_file'].format(file[0])) os.remove(file[0]) deleted_files += 1 self._print(LNG['deleted_files'].format(deleted_files, utils.pretty_size(current_size)), logger.INFO, 3)
def _process_flow(self, f): if self.filt and not f.match(self.filt): return if f.response: sz = utils.pretty_size(len(f.response.content)) if self.o.verbosity > 0: result = " << %s %s" % (str_response(f.response), sz) if self.o.verbosity > 1: result = result + "\n\n" + self.indent(4, f.response.headers) if self.o.verbosity > 2: if utils.isBin(f.response.content): d = utils.hexdump(f.response.content) d = "\n".join("%s\t%s %s" % i for i in d) cont = self.indent(4, d) elif f.response.content: cont = self.indent(4, f.response.content) else: cont = "" result = result + "\n\n" + cont elif f.error: result = " << %s" % f.error.msg if self.o.verbosity == 1: print >> self.outfile, str_request(f.request) print >> self.outfile, result elif self.o.verbosity == 2: print >> self.outfile, str_request(f.request) print >> self.outfile, self.indent(4, f.request.headers) print >> self.outfile print >> self.outfile, result print >> self.outfile, "\n" elif self.o.verbosity >= 3: print >> self.outfile, str_request(f.request) print >> self.outfile, self.indent(4, f.request.headers) if utils.isBin(f.request.content): print >> self.outfile, self.indent( 4, utils.hexdump(f.request.content)) elif f.request.content: print >> self.outfile, self.indent(4, f.request.content) print >> self.outfile print >> self.outfile, result print >> self.outfile, "\n" self.state.delete_flow(f) if self.o.wfile: self.fwriter.add(f)
def _process_flow(self, f): if self.filt and not f.match(self.filt): return if f.response: sz = utils.pretty_size(len(f.response.content)) if self.o.verbosity > 0: result = " << %s %s"%(str_response(f.response), sz) if self.o.verbosity > 1: result = result + "\n\n" + self.indent(4, f.response.headers) if self.o.verbosity > 2: if utils.isBin(f.response.content): d = utils.hexdump(f.response.content) d = "\n".join("%s\t%s %s"%i for i in d) cont = self.indent(4, d) elif f.response.content: cont = self.indent(4, f.response.content) else: cont = "" result = result + "\n\n" + cont elif f.error: result = " << %s"%f.error.msg if self.o.verbosity == 1: print >> self.outfile, str_request(f.request) print >> self.outfile, result elif self.o.verbosity == 2: print >> self.outfile, str_request(f.request) print >> self.outfile, self.indent(4, f.request.headers) print >> self.outfile print >> self.outfile, result print >> self.outfile, "\n" elif self.o.verbosity >= 3: print >> self.outfile, str_request(f.request) print >> self.outfile, self.indent(4, f.request.headers) if utils.isBin(f.request.content): print >> self.outfile, self.indent(4, utils.hexdump(f.request.content)) elif f.request.content: print >> self.outfile, self.indent(4, f.request.content) print >> self.outfile print >> self.outfile, result print >> self.outfile, "\n" self.state.delete_flow(f) if self.o.wfile: self.fwriter.add(f)
def get_text(self, nofocus=False, padding=3): if not self.request and not self.response: txt = [ ("title", " Connection from %s..."%(self.connection.address)), ] else: txt = [ ("ack", "!") if self.intercepting and not self.request.acked else " ", ("method", self.request.method), " ", ( "text" if (self.response or self.error) else "title", self.request.url(), ), ] if self.response or self.error or self.is_replay(): txt.append("\n" + " "*(padding+2)) if self.is_replay(): txt.append(("method", "[replay] ")) if not (self.response or self.error): txt.append(("text", "waiting for response...")) if self.response: txt.append( ("ack", "!") if self.intercepting and not self.response.acked else " " ) txt.append("-> ") if self.response.code in [200, 304]: txt.append(("goodcode", str(self.response.code))) else: txt.append(("error", str(self.response.code))) t = self.response.headers.get("content-type") if t: t = t[0].split(";")[0] txt.append(("text", " %s"%t)) if self.response.content: txt.append(", %s"%utils.pretty_size(len(self.response.content))) elif self.error: txt.append( ("error", self.error.msg) ) if self.focus and not nofocus: txt.insert(0, ("focus", ">>" + " "*(padding-2))) else: txt.insert(0, " "*padding) return txt
def do_list_media(pipe): mf = _list_media_files(target_dir, exts=exts, prefix=prefix) for (p, st) in mf: path = p[len(target_dir):] if not path.startswith('/'): path = '/' + path timestamp = st.st_mtime size = st.st_size pipe.send({ 'path': path, 'momentStr': utils.pretty_date_time(datetime.datetime.fromtimestamp(timestamp)), 'momentStrShort': utils.pretty_date_time(datetime.datetime.fromtimestamp(timestamp), short=True), 'sizeStr': utils.pretty_size(size), 'timestamp': timestamp }) pipe.close()
def do_list_media(pipe): mf = _list_media_files(target_dir, exts=exts, prefix=prefix) for (p, st) in mf: path = p[len(target_dir) :] if not path.startswith("/"): path = "/" + path timestamp = st.st_mtime size = st.st_size pipe.send( { "path": path, "momentStr": utils.pretty_date_time(datetime.datetime.fromtimestamp(timestamp)), "momentStrShort": utils.pretty_date_time(datetime.datetime.fromtimestamp(timestamp), short=True), "sizeStr": utils.pretty_size(size), "timestamp": timestamp, } ) pipe.close()
def do_list_media(pipe): import mimetypes parent_pipe.close() mf = _list_media_files(target_dir, exts=exts, prefix=prefix) for (p, st) in mf: path = p[len(target_dir):] if not path.startswith('/'): path = '/' + path timestamp = st.st_mtime size = st.st_size pipe.send({ 'path': path, 'mimeType': mimetypes.guess_type(path)[0] if mimetypes.guess_type(path)[0] is not None else 'video/mpeg', 'momentStr': utils.pretty_date_time(datetime.datetime.fromtimestamp(timestamp)), 'momentStrShort': utils.pretty_date_time(datetime.datetime.fromtimestamp(timestamp), short=True), 'sizeStr': utils.pretty_size(size), 'timestamp': timestamp }) pipe.close()
async def youtube(msg): if msg.get('text'): if msg['text'].startswith('/yt '): res = await search_yt(msg['text'][4:]) vids = [ '{}: <a href="{}">{}</a>'.format(num + 1, i['url'], i['title']) for num, i in enumerate(res) ] await bot.sendMessage( msg['chat']['id'], '\n'.join(vids) if vids else "Nenhum resultado foi encontrado", 'HTML', reply_to_message_id=msg['message_id'], disable_web_page_preview=True) return True elif msg['text'].split()[0] == '/ytdl': text = msg['text'][6:] if text: sent_id = (await bot.sendMessage( msg['chat']['id'], 'Obtendo informações do vídeo...', 'Markdown', reply_to_message_id=msg['message_id']))['message_id'] try: if re.match( r'^(https?://)?(youtu\.be/|(m\.|www\.)?youtube\.com/watch\?v=).+', text): yt = ydl.extract_info(text, download=False) else: yt = ydl.extract_info('ytsearch:' + text, download=False)['entries'][0] for f in yt['formats']: if f['format_id'] == '140': fsize = f['filesize'] or 0 name = yt['title'] except Exception as e: return await bot.editMessageText( (msg['chat']['id'], sent_id), 'Ocorreu um erro.\n\n' + str(e)) if not fsize > 52428800: if ' - ' in name: performer, title = name.rsplit(' - ', 1) else: performer = yt.get('creator') or yt.get('uploader') title = name await bot.editMessageText( (msg['chat']['id'], sent_id), 'Baixando <code>{}</code> do YouTube...\n({})'.format( name, pretty_size(fsize)), 'HTML') ydl.download( ['https://www.youtube.com/watch?v=' + yt['id']]) await bot.editMessageText((msg['chat']['id'], sent_id), 'Enviando áudio...') await bot.sendChatAction(msg['chat']['id'], 'upload_document') await bot.sendAudio(msg['chat']['id'], open(ydl.prepare_filename(yt), 'rb'), performer=performer, title=title, duration=yt['duration'], reply_to_message_id=msg['message_id']) os.remove(ydl.prepare_filename(yt)) await bot.deleteMessage((msg['chat']['id'], sent_id)) else: await bot.editMessageText(( msg['chat']['id'], sent_id ), f'Ow, o arquivo resultante ({pretty_size(fsize)}) ultrapassa o meu limite de 50 MB' ) else: await bot.sendMessage(msg['chat']['id'], '*Uso:* /ytdl URL do vídeo ou nome', 'Markdown', reply_to_message_id=msg['message_id']) return True
async def youtube(msg): if msg.get('text'): if msg['text'].startswith('/yt '): pesquisar = msg['text'][4:] search = SearchVideos(pesquisar, offset=1, mode="dict", max_results=10) resultado = search.result() #titulo = [] #link = [] dic_youtube = [] for resultado in resultado['search_result']: titulo_captado = resultado['title'] link_captado = resultado['link'] #titulo.append(titulo_captado) #link.append(link_captado) dic = {'titulo': titulo_captado, 'url': link_captado} dic_youtube.append(dic) vids = [ '[{}] {} {}'.format(num + 1, i['titulo'], i['url']) for num, i in enumerate(dic_youtube) ] await bot.sendMessage( msg['chat']['id'], '\n'.join(vids) if vids else "Nenhum resultado foi encontrado", 'HTML', reply_to_message_id=msg['message_id'], disable_web_page_preview=True) #return True if msg['text'].startswith('youtube '): pesquisar = msg['text'][8:] search = SearchVideos(pesquisar, offset=1, mode="dict", max_results=10) resultado = search.result() # titulo = [] # link = [] dic_youtube = [] for resultado in resultado['search_result']: titulo_captado = resultado['title'] link_captado = resultado['link'] # titulo.append(titulo_captado) # link.append(link_captado) dic = {'titulo': titulo_captado, 'url': link_captado} dic_youtube.append(dic) vids = [ '[{}] {} {}'.format(num + 1, i['titulo'], i['url']) for num, i in enumerate(dic_youtube) ] await bot.sendMessage( msg['chat']['id'], '\n'.join(vids) if vids else "Nenhum resultado foi encontrado", 'HTML', reply_to_message_id=msg['message_id'], disable_web_page_preview=True) #---------------------------------------------------------------------------------------------------------------------------------------------- elif msg['text'].split( )[0] == '/ytdl': #aqui era /ytdl tirei pra nao pesar o server text = msg['text'][6:] if text: sent_id = (await bot.sendMessage( msg['chat']['id'], 'Obtendo informações do vídeo...', 'Markdown', reply_to_message_id=msg['message_id']))['message_id'] try: if re.match( r'^(https?://)?(youtu\.be/|(m\.|www\.)?youtube\.com/watch\?v=).+', text): yt = ydl.extract_info(text, download=False) else: yt = ydl.extract_info('ytsearch:' + text, download=False)['entries'][0] for f in yt['formats']: if f['format_id'] == '140': fsize = f['filesize'] or 0 name = yt['title'] except Exception as e: return await bot.editMessageText( (msg['chat']['id'], sent_id), 'Ocorreu um erro.\n\n' + str(e)) if not fsize > 52428800: if ' - ' in name: performer, title = name.rsplit(' - ', 1) else: performer = yt.get('creator') or yt.get('uploader') title = name await bot.editMessageText( (msg['chat']['id'], sent_id), 'Baixando <code>{}</code> do YouTube...\n({})'.format( name, pretty_size(fsize)), 'HTML') ydl.download( ['https://www.youtube.com/watch?v=' + yt['id']]) await bot.editMessageText((msg['chat']['id'], sent_id), 'Enviando áudio...') await bot.sendChatAction(msg['chat']['id'], 'upload_document') await bot.sendAudio(msg['chat']['id'], open(ydl.prepare_filename(yt), 'rb'), performer=performer, title=title, duration=yt['duration'], reply_to_message_id=msg['message_id']) os.remove(ydl.prepare_filename(yt)) await bot.deleteMessage((msg['chat']['id'], sent_id)) else: await bot.editMessageText(( msg['chat']['id'], sent_id ), f'Ow, o arquivo resultante ({pretty_size(fsize)}) ultrapassa o meu limite de 50 MB' ) else: await bot.sendMessage(msg['chat']['id'], '*Uso:* /ytdl URL do vídeo ou nome', 'Markdown', reply_to_message_id=msg['message_id']) return True elif msg['text'].split()[0] == '': #aqui era ytldl gorpo tirei tb text = msg['text'][17:] if text: sent_id = (await bot.sendMessage( msg['chat']['id'], 'Obtendo informações do vídeo...', 'Markdown', reply_to_message_id=msg['message_id']))['message_id'] try: if re.match( r'^(https?://)?(youtu\.be/|(m\.|www\.)?youtube\.com/watch\?v=).+', text): yt = ydl.extract_info(text, download=False) else: yt = ydl.extract_info('ytsearch:' + text, download=False)['entries'][0] for f in yt['formats']: if f['format_id'] == '140': fsize = f['filesize'] or 0 name = yt['title'] except Exception as e: return await bot.editMessageText( (msg['chat']['id'], sent_id), 'Ocorreu um erro.\n\n' + str(e)) if not fsize > 52428800: if ' - ' in name: performer, title = name.rsplit(' - ', 1) else: performer = yt.get('creator') or yt.get('uploader') title = name await bot.editMessageText( (msg['chat']['id'], sent_id), 'Baixando <code>{}</code> do YouTube...\n({})'.format( name, pretty_size(fsize)), 'HTML') ydl.download( ['https://www.youtube.com/watch?v=' + yt['id']]) await bot.editMessageText((msg['chat']['id'], sent_id), 'Enviando áudio...') await bot.sendChatAction(msg['chat']['id'], 'upload_document') await bot.sendAudio(msg['chat']['id'], open(ydl.prepare_filename(yt), 'rb'), performer=performer, title=title, duration=yt['duration'], reply_to_message_id=msg['message_id']) os.remove(ydl.prepare_filename(yt)) await bot.deleteMessage((msg['chat']['id'], sent_id)) else: await bot.editMessageText(( msg['chat']['id'], sent_id ), f'Ow, o arquivo resultante ({pretty_size(fsize)}) ultrapassa o meu limite de 50 MB' ) else: await bot.sendMessage(msg['chat']['id'], '*Uso:* /ytdl URL do vídeo ou nome', 'Markdown', reply_to_message_id=msg['message_id']) return True elif msg['text'].startswith('download youtube'): text = msg['text'][17:] if text: sent_id = (await bot.sendMessage( msg['chat']['id'], 'Obtendo informações do vídeo...', 'Markdown', reply_to_message_id=msg['message_id']))['message_id'] try: if re.match( r'^(https?://)?(youtu\.be/|(m\.|www\.)?youtube\.com/watch\?v=).+', text): yt = ydl.extract_info(text, download=False) else: yt = ydl.extract_info('ytsearch:' + text, download=False)['entries'][0] for f in yt['formats']: if f['format_id'] == '140': fsize = f['filesize'] or 0 name = yt['title'] except Exception as e: return await bot.editMessageText( (msg['chat']['id'], sent_id), 'Ocorreu um erro.\n\n' + str(e)) if not fsize > 52428800: if ' - ' in name: performer, title = name.rsplit(' - ', 1) else: performer = yt.get('creator') or yt.get('uploader') title = name await bot.editMessageText( (msg['chat']['id'], sent_id), 'Baixando <code>{}</code> do YouTube...\n({})'.format( name, pretty_size(fsize)), 'HTML') ydl.download( ['https://www.youtube.com/watch?v=' + yt['id']]) await bot.editMessageText((msg['chat']['id'], sent_id), 'Enviando áudio...') await bot.sendChatAction(msg['chat']['id'], 'upload_document') await bot.sendAudio(msg['chat']['id'], open(ydl.prepare_filename(yt), 'rb'), performer=performer, title=title, duration=yt['duration'], reply_to_message_id=msg['message_id']) os.remove(ydl.prepare_filename(yt)) await bot.deleteMessage((msg['chat']['id'], sent_id)) else: await bot.editMessageText(( msg['chat']['id'], sent_id ), f'Ow, o arquivo resultante ({pretty_size(fsize)}) ultrapassa o meu limite de 50 MB' ) else: await bot.sendMessage(msg['chat']['id'], '*Uso:* /ytdl URL do vídeo ou nome', 'Markdown', reply_to_message_id=msg['message_id']) return True