You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.6 KiB
Python

from reportlab.lib.pagesizes import *
from reportlab.pdfgen import canvas
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from calibrestekje import Book, Publisher, init_session
from readfrompad import curl, parse_pad
from xml.etree import ElementTree as ET
paragraphs_by_header = parse_pad(curl("https://pad.xpub.nl/p/bootleg_annotations/export/txt"))
from pprint import pprint
pprint(paragraphs_by_header)
pagewidth, pageheight = landscape(A6)
doc = SimpleDocTemplate("text.pdf", pagesize=landscape(A6),
rightMargin=18, leftMargin=18,
topMargin=0, bottomMargin=18)
content = []
styles = getSampleStyleSheet()
session = init_session("sqlite:///metadata.db")
for book in session.query(Book).all():
book_url = "https://hub.xpub.nl/bootleglibrary/book/{}".format(book.id)
print (book.title)
print (book_url)
# print (book.authors)
# c.drawString(10,pageheight-10, book.title)
# c.showPage()
# create a paragraph and append content to it - e.g. book.title, book.authors etc
p = Paragraph('<font size=12>{}</font>'.format(book.title), styles["Italic"])
content.append(p)
# content.append(PageBreak())
content.append(Spacer(1, 12))
#import ipdb; ipdb.set_trace()
format_string = '<font size=12>{}</font>'
all_authors = [author.name for author in book.authors]
glued_together = format_string.format(", ".join(all_authors))
# ALTERNATIVE WAY... (without list comprehensions)
first = True
author_text = ""
for author in book.authors:
if not first:
author_text += ", "
author_text += "<font size=12>{}</font>".format(author.name)
first = False
#if all_authors==['John Markoff']:
# import ipdb; ipdb.set_trace()
p = Paragraph(glued_together, styles["Normal"])
content.append(p)
content.append(PageBreak())
content.append(Spacer(1, 12))
# BACK SIDE
if book_url in paragraphs_by_header:
print ("FOUND ANNOTATIONS FOR BOOK", book_url)
# ANNOTATIONS FROM PAD
annotations = paragraphs_by_header[book_url]
for p in annotations:
p_text = ET.tostring(p, method="html", encoding="utf-8")
p = Paragraph(p_text, styles["Normal"])
content.append(p)
content.append(PageBreak())
content.append(Spacer(1, 12))
else:
# BLANK BACK SIDE
p = Paragraph("", styles["Normal"])
content.append(p)
content.append(PageBreak())
content.append(Spacer(1, 12))
doc.build(content)