#!/usr/bin/python3 import localconf import requests import pypdftk import pygraphviz import subprocess from requests.auth import HTTPBasicAuth import re import os import sys # todo move this variable outta here.. NODE = localconf.NODE PAGEDJSCLI = localconf.PAGEDJSCLI BASECSS = 'https://hub.vvvvvvaria.org/rosa/pad/p/design-trial_2.css/export/txt' localhost = '/var/www/html/pdfs/' localurl = 'http://localhost/pdfs/' outputname='atnofs-compiled.pdf' authu='octomode' authp='spider' authvvvvvv = HTTPBasicAuth('octomode', 'spider') e2hprocessor='http://totalism.org:7777/glia2?sourceMethod=post&sinkFormat=text/graph/graphviz' chapters=[] outputs=[] counter=0 ooooo_graphs="https://pad.constantvzw.org/p/atnofs-insert" indexmd = requests.get('https://pad.vvvvvvaria.org/atnofs-index.md/export/txt').text # download the index of all contributions with open('index.md','w') as file: file.write(indexmd) basecssget = requests.get(BASECSS).text with open(localhost+'base.css','w') as file: file.write(basecssget) # download the base css style with open('index.md') as file: for line in file: # searches for the chapter links x = re.search ('[.*](.*/pad/)',line) if x: line=line.split('(')[1].split(')')[-2] chapters.append(line) # searches for ooooo insert if 'ooooo' in line: ooooo_insert=line.split('(')[1].split(')')[-2] oooooc=0 if ooooo_insert: all_graphs = requests.get(ooooo_graphs+'/export/txt') graphs=all_graphs.text.split('[graph]"""') for graph in graphs[1:]: graph=graph.split('"""')[-2] dot = requests.post(e2hprocessor,graph.encode('utf-8')) with open('dot/'+str(oooooc)+'.dot','w') as file: file.write(dot.text) graph = pygraphviz.AGraph('dot/'+str(oooooc)+'.dot') graph.layout(prog="dot") graph.draw('dot/'+str(oooooc)+'.svg') oooooc+=1 #then iterates over all chapters to get the pdf versions for chapter in chapters: name=str(counter).zfill(2)+'-'+chapter.split('/')[-3] if not os.path.exists(localhost+name): os.makedirs(localhost+name) pdffile=name+'.pdf' if len(sys.argv)<2 or sys.argv[1]=='nonlocal': if('octomode.vvvvvvaria.org' in chapter): preview = requests.get(chapter.replace('pad/','preview.html'), auth=authvvvvvv) style = requests.get(chapter.replace('pad/','stylesheet.css'), auth=authvvvvvv) else: preview = requests.get(chapter.replace('pad/','preview.html')) style = requests.get(chapter.replace('pad/','stylesheet.css')) stylefile=localhost+name+'/stylesheet.css' with open(stylefile, 'w') as file: file.write(style.text) previewfile=localhost+name+'/preview.html' localpreview=localurl+name+'/preview.html' with open(previewfile, 'w') as file: file.write(preview.text) # hacke before octomode is fixed on vvvvaria with open(previewfile, "r") as f: contents = f.readlines() contents.insert(5, '') with open(previewfile, "w") as f: contents = "".join(contents) f.write(contents) # end hack # this is the pagedjs client version making the pdf out of the localhost version of the html subprocess.run([NODE, PAGEDJSCLI,'--browserArgs','--font-render-hinting=none',localpreview, '-o', localhost+pdffile]) else: if('octomode.vvvvvvaria.org' in chapter): chapter=chapter.replace('://','://'+authu+':'+authp+'@') preview = chapter.replace('pad/','pagedjs.html') subprocess.run([NODE, PAGEDJSCLI,'--browserArgs','--font-render-hinting=none', preview, '-o', localhost+pdffile]) outputs.append(localhost+pdffile) counter+=1 # compiling all the files in one big pdf! output = pypdftk.concat(outputs,out_file=localhost+outputname)