from pathlib import Path from common import load_json from urllib.parse import urlparse, quote as urlquote, unquote as urlunquote from rdflib import Graph, Namespace, RDF, DCTERMS, URIRef import subprocess XPUB = Namespace("http://xpub.nl/terms/") def url_to_path (url): parts = urlparse(url) if parts.hostname == "project.xpub.nl": path = urlunquote(parts.path.lstrip('/')) return Path(path) def path_to_url(path): return "https://project.xpub.nl/" + urlquote(str(path)) def make_cover (inpath, outpath, page=1): p = subprocess.run(["gs", "-dSAFER", "-r600", "-sDEVICE=jpeg", f"-dFirstPage={page}", f"-dLastPage={page}", "-o", outpath, inpath]) if p.returncode == 0: p = subprocess.run(["mogrify", "-resize", "640x", outpath]) return p.returncode == 0 g = Graph() data = load_json() for project in data['projects']: print (project['thesis']) p = url_to_path(project['thesis']) cover_path = p.parent / (p.stem + ".cover.jpg") g.add((URIRef(project['url']), XPUB['cover'], URIRef(path_to_url(cover_path)))) print (f" {p}->{cover_path}") if not cover_path.exists(): make_cover(p, cover_path) with open("covers.json", "w") as fout: # print (g.serialize(), file=fout) print (g.serialize(format="json-ld", context="https://xpub.nl/contexts/projects.json"), file=fout) # from generated_covers import books_with_generated_covers # import subprocess # from shutil import copyfile # for dbid, path in books_with_generated_covers("calibre"): # for pdf in path.glob("*.pdf"): # print (pdf) # # gs -dSAFER -r600 -sDEVICE=jpeg -dFirstPage=2 -dLastPage=2 -o cover.jpg test.pdf && mogrify -resize 640x cover.jpg # # gs -dSAFER -r600 -sDEVICE=jpeg -dLastPage=1 -o cover.jpg "calibre/Michael Moss/To talk of many things_ Of shoes--and ships--and sealing-wax--Of cabbages--and kings- (85)/To talk of many things_ Of shoes--and ship - Michael Moss.pdf" # subprocess.run(["gs", "-dSAFER", "-r600", "-sDEVICE=jpeg", "-dLastPage=1", "-o", "cover.jpg", str(pdf)]) # subprocess.run(["mogrify", "-resize", "1200x", "cover.jpg"]) # subprocess.run(["exiftool", "-Comment=Generated cover: gs/pdffirstpage", "cover.jpg"]) # copyfile("cover.jpg", path / "cover.jpg")