Loading opds_catalog/dl.py +54 −4 Original line number Diff line number Diff line Loading @@ -41,12 +41,9 @@ def getFileData(book): s = None fo = None #print("************ ТИП КНИГИ: %s"%book.cat_type) if book.cat_type==opdsdb.CAT_NORMAL: file_path=os.path.join(full_path, book.filename) try: #print("************ ПУТЬ К КНИГЕ: %s" % file_path) fo=codecs.open(file_path, "rb") #s = fo.read() except FileNotFoundError: Loading @@ -69,7 +66,9 @@ def getFileData(book): return fo def getFileDataZip(fo,transname): def getFileDataZip(book): transname = getFileName(book) fo = getFileData(book) dio = io.BytesIO() zo = zipfile.ZipFile(dio, 'w', zipfile.ZIP_DEFLATED) zo.writestr(transname, fo.read()) Loading @@ -79,6 +78,57 @@ def getFileDataZip(fo,transname): return dio def getFileDataConv(book, convert_type): if book.format != 'fb2': return None fo = getFileData(book) if not fo: return None transname = getFileName(book) (n, e) = os.path.splitext(transname) dlfilename = "%s.%s" % (n, convert_type) if convert_type == 'epub': converter_path = config.SOPDS_FB2TOEPUB elif convert_type == 'mobi': converter_path = config.SOPDS_FB2TOMOBI else: fo.close() return None tmp_fb2_path = os.path.join(config.SOPDS_TEMP_DIR, book.filename) tmp_conv_path = os.path.join(config.SOPDS_TEMP_DIR, dlfilename) fw = open(tmp_fb2_path,'w') fw.write(fo.read()) fw.close() fo.close() popen_args = ("\"%s\" \"%s\" \"%s\"" % (converter_path, tmp_fb2_path, tmp_conv_path)) proc = subprocess.Popen(popen_args, shell=True, stdout=subprocess.PIPE) out = proc.stdout.readlines() os.remove(tmp_fb2_path) if os.path.isfile(tmp_conv_path): fo = codecs.open(tmp_conv_path, "rb") else: return None # Как то нужно удалять временный файл после отдачи его # os.remove(tmp_conv_path) return fo def getFileDataEpub(book): return getFileDataConv(book,'epub') def getFileDataMobi(book): return getFileDataConv(book,'mobi') def Download(request, book_id, zip_flag): """ Загрузка файла книги """ book = Book.objects.get(id=book_id) Loading opds_catalog/management/commands/sopds_telebot.py +15 −10 Original line number Diff line number Diff line Loading @@ -112,16 +112,12 @@ class Command(BaseCommand): response = '<b>%(title)s</b>\n%(author)s\n<b>Аннотация:</b>%(annotation)s\n' % {'title': book.title, 'author': authors, 'annotation':book.annotation} buttons = [InlineKeyboardButton(book.format.upper(), callback_data='/getfileorig%s'%book_id)] # url=config.SOPDS_SITE_ROOT+reverse("opds_catalog:download", kwargs={"book_id": book.id, "zip_flag": 0}))] if not book.format in settings.NOZIP_FORMATS: buttons += [InlineKeyboardButton(book.format.upper()+'.ZIP', callback_data='/getfilezip%s'%book_id)] # url=config.SOPDS_SITE_ROOT+reverse("opds_catalog:download",kwargs={"book_id": book.id, "zip_flag": 1}))] # if (config.SOPDS_FB2TOEPUB != "") and (book.format == 'fb2'): # buttons += [InlineKeyboardButton('EPUB', # # url=config.SOPDS_SITE_ROOT+reverse("opds_catalog:convert",kwargs={"book_id": book.id, "convert_type": "epub"}))] # if (config.SOPDS_FB2TOMOBI != "") and (book.format == 'fb2'): # buttons += [InlineKeyboardButton('MOBI', # # url=config.SOPDS_SITE_ROOT+reverse("opds_catalog:convert",kwargs={"book_id": book.id, "convert_type": "mobi"}))] if (config.SOPDS_FB2TOEPUB != "") and (book.format == 'fb2'): buttons += [InlineKeyboardButton('EPUB', callback_data='/getfileepub%s'%book_id)] if (config.SOPDS_FB2TOMOBI != "") and (book.format == 'fb2'): buttons += [InlineKeyboardButton('MOBI', callback_data='/getfilemobi%s'%book_id)] markup = InlineKeyboardMarkup([buttons]) bot.sendMessage(chat_id=update.message.chat_id, text=response, parse_mode='HTML', reply_markup=markup) Loading Loading @@ -153,12 +149,21 @@ class Command(BaseCommand): document = dl.getFileData(book) #document = config.SOPDS_SITE_ROOT + reverse("opds_catalog:download",kwargs={"book_id": book.id, "zip_flag": 0}) if re.match(r'/getfilezip',query.data): document = dl.getFileDataZip(dl.getFileData(book), filename) document = dl.getFileDataZip(book) #document = config.SOPDS_SITE_ROOT + reverse("opds_catalog:download", kwargs={"book_id": book.id, "zip_flag": 1}) filename = filename + '.zip' if re.match(r'/getfileepub',query.data): document = dl.getFileDataEpub(book) #document = config.SOPDS_SITE_ROOT+reverse("opds_catalog:convert",kwargs={"book_id": book.id, "convert_type": "epub"}))] filename = filename + '.epub' if re.match(r'/getfilemobi',query.data): document = dl.getFileDataMobi(book) #document = config.SOPDS_SITE_ROOT+reverse("opds_catalog:convert",kwargs={"book_id": book.id, "convert_type": "mobi"}))] filename = filename + '.mobi' if document: bot.send_document(chat_id=query.message.chat_id,document=document,filename=filename) self.logger.info("Send file: %s" % filename) Loading sopds/settings.py +1 −1 Original line number Diff line number Diff line Loading @@ -218,7 +218,7 @@ CONSTANCE_CONFIG_FIELDSETS = { '2. Server Options': ('SOPDS_AUTH', 'SOPDS_ALPHABET_MENU', 'SOPDS_DOUBLES_HIDE', 'SOPDS_COVER_SHOW', 'SOPDS_SPLITITEMS', 'SOPDS_MAXITEMS', 'SOPDS_TITLE_AS_FILENAME', 'SOPDS_NOCOVER_PATH'), '3. Scanner Options': ('SOPDS_FB2SAX','SOPDS_ZIPSCAN','SOPDS_ZIPCODEPAGE', 'SOPDS_INPX_ENABLE', 'SOPDS_INPX_SKIP_UNCHANGED', 'SOPDS_INPX_TEST_ZIP', 'SOPDS_INPX_TEST_FILES', 'SOPDS_DELETE_LOGICAL'), '4. Scanner Shedule': ('SOPDS_SCAN_SHED_MIN', 'SOPDS_SCAN_SHED_HOUR', 'SOPDS_SCAN_SHED_DAY','SOPDS_SCAN_SHED_DOW'), '5. Telegramm Bot Options': ('SOPDS_TELEBOT_API_TOKEN'), '5. Telegramm Bot Options': ('SOPDS_TELEBOT_API_TOKEN',), '6. Converters Options': ('SOPDS_FB2TOEPUB', 'SOPDS_FB2TOMOBI', 'SOPDS_TEMP_DIR'), '7. Log & PID Files': ('SOPDS_SERVER_LOG', 'SOPDS_SCANNER_LOG', 'SOPDS_TELEBOT_LOG','SOPDS_SERVER_PID','SOPDS_SCANNER_PID','SOPDS_TELEBOT_PID'), } Loading Loading
opds_catalog/dl.py +54 −4 Original line number Diff line number Diff line Loading @@ -41,12 +41,9 @@ def getFileData(book): s = None fo = None #print("************ ТИП КНИГИ: %s"%book.cat_type) if book.cat_type==opdsdb.CAT_NORMAL: file_path=os.path.join(full_path, book.filename) try: #print("************ ПУТЬ К КНИГЕ: %s" % file_path) fo=codecs.open(file_path, "rb") #s = fo.read() except FileNotFoundError: Loading @@ -69,7 +66,9 @@ def getFileData(book): return fo def getFileDataZip(fo,transname): def getFileDataZip(book): transname = getFileName(book) fo = getFileData(book) dio = io.BytesIO() zo = zipfile.ZipFile(dio, 'w', zipfile.ZIP_DEFLATED) zo.writestr(transname, fo.read()) Loading @@ -79,6 +78,57 @@ def getFileDataZip(fo,transname): return dio def getFileDataConv(book, convert_type): if book.format != 'fb2': return None fo = getFileData(book) if not fo: return None transname = getFileName(book) (n, e) = os.path.splitext(transname) dlfilename = "%s.%s" % (n, convert_type) if convert_type == 'epub': converter_path = config.SOPDS_FB2TOEPUB elif convert_type == 'mobi': converter_path = config.SOPDS_FB2TOMOBI else: fo.close() return None tmp_fb2_path = os.path.join(config.SOPDS_TEMP_DIR, book.filename) tmp_conv_path = os.path.join(config.SOPDS_TEMP_DIR, dlfilename) fw = open(tmp_fb2_path,'w') fw.write(fo.read()) fw.close() fo.close() popen_args = ("\"%s\" \"%s\" \"%s\"" % (converter_path, tmp_fb2_path, tmp_conv_path)) proc = subprocess.Popen(popen_args, shell=True, stdout=subprocess.PIPE) out = proc.stdout.readlines() os.remove(tmp_fb2_path) if os.path.isfile(tmp_conv_path): fo = codecs.open(tmp_conv_path, "rb") else: return None # Как то нужно удалять временный файл после отдачи его # os.remove(tmp_conv_path) return fo def getFileDataEpub(book): return getFileDataConv(book,'epub') def getFileDataMobi(book): return getFileDataConv(book,'mobi') def Download(request, book_id, zip_flag): """ Загрузка файла книги """ book = Book.objects.get(id=book_id) Loading
opds_catalog/management/commands/sopds_telebot.py +15 −10 Original line number Diff line number Diff line Loading @@ -112,16 +112,12 @@ class Command(BaseCommand): response = '<b>%(title)s</b>\n%(author)s\n<b>Аннотация:</b>%(annotation)s\n' % {'title': book.title, 'author': authors, 'annotation':book.annotation} buttons = [InlineKeyboardButton(book.format.upper(), callback_data='/getfileorig%s'%book_id)] # url=config.SOPDS_SITE_ROOT+reverse("opds_catalog:download", kwargs={"book_id": book.id, "zip_flag": 0}))] if not book.format in settings.NOZIP_FORMATS: buttons += [InlineKeyboardButton(book.format.upper()+'.ZIP', callback_data='/getfilezip%s'%book_id)] # url=config.SOPDS_SITE_ROOT+reverse("opds_catalog:download",kwargs={"book_id": book.id, "zip_flag": 1}))] # if (config.SOPDS_FB2TOEPUB != "") and (book.format == 'fb2'): # buttons += [InlineKeyboardButton('EPUB', # # url=config.SOPDS_SITE_ROOT+reverse("opds_catalog:convert",kwargs={"book_id": book.id, "convert_type": "epub"}))] # if (config.SOPDS_FB2TOMOBI != "") and (book.format == 'fb2'): # buttons += [InlineKeyboardButton('MOBI', # # url=config.SOPDS_SITE_ROOT+reverse("opds_catalog:convert",kwargs={"book_id": book.id, "convert_type": "mobi"}))] if (config.SOPDS_FB2TOEPUB != "") and (book.format == 'fb2'): buttons += [InlineKeyboardButton('EPUB', callback_data='/getfileepub%s'%book_id)] if (config.SOPDS_FB2TOMOBI != "") and (book.format == 'fb2'): buttons += [InlineKeyboardButton('MOBI', callback_data='/getfilemobi%s'%book_id)] markup = InlineKeyboardMarkup([buttons]) bot.sendMessage(chat_id=update.message.chat_id, text=response, parse_mode='HTML', reply_markup=markup) Loading Loading @@ -153,12 +149,21 @@ class Command(BaseCommand): document = dl.getFileData(book) #document = config.SOPDS_SITE_ROOT + reverse("opds_catalog:download",kwargs={"book_id": book.id, "zip_flag": 0}) if re.match(r'/getfilezip',query.data): document = dl.getFileDataZip(dl.getFileData(book), filename) document = dl.getFileDataZip(book) #document = config.SOPDS_SITE_ROOT + reverse("opds_catalog:download", kwargs={"book_id": book.id, "zip_flag": 1}) filename = filename + '.zip' if re.match(r'/getfileepub',query.data): document = dl.getFileDataEpub(book) #document = config.SOPDS_SITE_ROOT+reverse("opds_catalog:convert",kwargs={"book_id": book.id, "convert_type": "epub"}))] filename = filename + '.epub' if re.match(r'/getfilemobi',query.data): document = dl.getFileDataMobi(book) #document = config.SOPDS_SITE_ROOT+reverse("opds_catalog:convert",kwargs={"book_id": book.id, "convert_type": "mobi"}))] filename = filename + '.mobi' if document: bot.send_document(chat_id=query.message.chat_id,document=document,filename=filename) self.logger.info("Send file: %s" % filename) Loading
sopds/settings.py +1 −1 Original line number Diff line number Diff line Loading @@ -218,7 +218,7 @@ CONSTANCE_CONFIG_FIELDSETS = { '2. Server Options': ('SOPDS_AUTH', 'SOPDS_ALPHABET_MENU', 'SOPDS_DOUBLES_HIDE', 'SOPDS_COVER_SHOW', 'SOPDS_SPLITITEMS', 'SOPDS_MAXITEMS', 'SOPDS_TITLE_AS_FILENAME', 'SOPDS_NOCOVER_PATH'), '3. Scanner Options': ('SOPDS_FB2SAX','SOPDS_ZIPSCAN','SOPDS_ZIPCODEPAGE', 'SOPDS_INPX_ENABLE', 'SOPDS_INPX_SKIP_UNCHANGED', 'SOPDS_INPX_TEST_ZIP', 'SOPDS_INPX_TEST_FILES', 'SOPDS_DELETE_LOGICAL'), '4. Scanner Shedule': ('SOPDS_SCAN_SHED_MIN', 'SOPDS_SCAN_SHED_HOUR', 'SOPDS_SCAN_SHED_DAY','SOPDS_SCAN_SHED_DOW'), '5. Telegramm Bot Options': ('SOPDS_TELEBOT_API_TOKEN'), '5. Telegramm Bot Options': ('SOPDS_TELEBOT_API_TOKEN',), '6. Converters Options': ('SOPDS_FB2TOEPUB', 'SOPDS_FB2TOMOBI', 'SOPDS_TEMP_DIR'), '7. Log & PID Files': ('SOPDS_SERVER_LOG', 'SOPDS_SCANNER_LOG', 'SOPDS_TELEBOT_LOG','SOPDS_SERVER_PID','SOPDS_SCANNER_PID','SOPDS_TELEBOT_PID'), } Loading