view bin/plinks.py @ 4:462179da7dc2

try harder not to write empty links files
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Wed, 19 Feb 2020 10:39:05 +0000
parents 83ed7c5846b2
children a28d731977da
line wrap: on
line source

#!/lustre/sw/miniconda3/bin/python3
import sys,pdfx,traceback
from os import path

def run(file):
  global pdf
  pdf=pdfx.PDFx(file)
  return pdf.get_references_as_dict()

me=sys.argv[1]
with open('/dev/shm/x/badpdfs_%s'%me,'w') as bf:
  for l in sys.stdin:
    (fno,f)=l.rstrip().split()
    try:
      links=run(f)
      if bool(links) and (links.get('scrape',False) or
                          links.get('annot',False)):
        with open('/dev/shm/x/links_%s'%fno,'w') as of:
          for k in links.keys():
            for l in links[k]:
              print("%s\t%s"%(k,l),file=of)
    except Exception as e:
      print("%s\t%s"%(fno,e),file=bf)
    if (path.exists('/dev/shm/stopJob')):
      print("Quiting early: %s %s"%(me,fno),file=sys.stderr)
      exit(1)