script to compile the PDFs of the atnofs publication
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.3 KiB

#!/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/'
localgs = '/var/www/html/svgs/'
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
page=1
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(localhost+'.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(localhost+'.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
ooooo_insert=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'))
dot = dot.text
dot = dot.split('edge [penwidth="5"];')[1]
head='''
digraph ""{
viewport="2700";
bgcolor="transparent";
node [shape="circle", margin="0.01", target="_blank", style="filled", width="1.5"];
edge [penwidth="5"];
'''
dot=head+dot
with open('dot/'+str(oooooc)+'.dot','w') as file:
file.write(dot)
graph = pygraphviz.AGraph('dot/'+str(oooooc)+'.dot')
graph.layout(prog="dot")
graph.draw(localgs+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'))
style=style.text
if page > 0:
style=style+'@page:first { counter-increment: page '+str(page)+'; }'
stylefile=localhost+'.'+name+'/stylesheet.css'
with open(stylefile, 'w') as file:
file.write(style)
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, '<link href="stylesheet.css" rel="stylesheet" type="text/css"><link href="../.base.css" rel="stylesheet" type="text/css">')
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
pagenr=pypdftk.get_num_pages(localhost+pdffile)
if (pagenr % 2) != 0:
pypdftk.concat([localhost+pdffile,localhost+'.blank.pdf'],localhost+'.temp.pdf')
os.replace(localhost+'.temp.pdf',localhost+pdffile)
pagenr+=1
page=page+pagenr
# compiling all the files in one big pdf!
output = pypdftk.concat(outputs,out_file=localhost+outputname)