feat: dont redownload existing epub

This commit is contained in:
eneller
2025-07-03 21:42:32 +02:00
parent 1b5e1ee491
commit f570aef863
2 changed files with 16 additions and 4 deletions

View File

@@ -5,12 +5,14 @@ from django.core.paginator import Paginator
from epub2go.convert import get_all_books, Book, allbooks_url from epub2go.convert import get_all_books, Book, allbooks_url
import os import os
import glob
from urllib.parse import urlparse from urllib.parse import urlparse
import logging import logging
from epub2go_web.tasks import getEpub from epub2go_web.tasks import getEpub, getDir
logger = logging.getLogger(__name__) #TODO configure logging logger = logging.getLogger(__name__) #TODO configure logging
logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(levelname)s - %(message)s')
books = sorted(get_all_books(), key= lambda b: b.title)# TODO get from pickle books = sorted(get_all_books(), key= lambda b: b.title)# TODO get from pickle
books_count = len(books) books_count = len(books)
@@ -24,9 +26,16 @@ def index(request: HttpRequest):
searchParam = request.GET.get('s', None) searchParam = request.GET.get('s', None)
if targetParam: if targetParam:
if validateUrl(targetParam): if validateUrl(targetParam):
# download file # does an epub already exist?
result = getEpub.delay(targetParam) dir_download = getDir(targetParam)
fpath = result.get(timeout=60) listDir = glob.glob('*.epub', root_dir=dir_download)
if len(listDir) != 0:
fpath = os.path.join(dir_download, listDir[0])
logger.info('Found existing file at %s', fpath)
else:
# download file
result = getEpub.delay(targetParam)
fpath = result.get(timeout=60)
fname = os.path.basename(fpath) fname = os.path.basename(fpath)
file = open(fpath, 'rb') file = open(fpath, 'rb')
response = FileResponse(file) response = FileResponse(file)

View File

@@ -9,3 +9,6 @@ converter = GBConvert(downloaddir=settings.MEDIA_ROOT)
def getEpub(book_url): def getEpub(book_url):
# TODO check for existing file and age # TODO check for existing file and age
return converter.download(book_url) return converter.download(book_url)
def getDir(book_url):
return converter.getDir(book_url)