37 lines
1.4 KiB
Python
37 lines
1.4 KiB
Python
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)
|
|
if article.file_name[-4:] != ".pdf":
|
|
return article # it probably was a youtube video
|
|
|
|
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
|