Better structure

This commit is contained in:
Remy Moll
2022-04-17 22:32:56 +02:00
parent 0a6dde8c78
commit 0d76bcbb98
15 changed files with 310 additions and 58 deletions

View File

@@ -0,0 +1,33 @@
import os
import subprocess
import logging
logger = logging.getLogger(__name__)
import configuration
config = configuration.parsed["DOWNLOADS"]
shrink_sizes = []
def shrink_pdf(article):
initial_size = os.path.getsize(article.save_path + article.file_name)
c = subprocess.run(
["gs", "-sDEVICE=pdfwrite", "-dPDFSETTINGS=/screen", "-dNOPAUSE", "-dBATCH", f"-sOutputFile={config['default_download_path']}/compressed.pdf", f'"{article.save_path + article.file_name}"'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
if c.returncode == 0:
m = subprocess.run(
["mv", "-f", f"{config['default_download_path']}/compressed.pdf", article.save_path + article.file_name]
)
if m.returncode == 0:
final_size = os.path.getsize(article.save_path + article.file_name)
shrink_sizes.append(initial_size - final_size)
logger.info(f"Compression worked. Avg shrinkage: {sum(shrink_sizes)/len(shrink_sizes) / 1000} (kb)")
return article # even though no modifications were made
else:
logger.error(f"Compression ran but I could not copy back the file {m.stderr.decode()} - {m.stdout.decode()}")
else:
logger.error(f"Could not run the compression! {c.stderr.decode()} - {c.stdout.decode()}")
return article