fread/helpers/__init__.py

35 lines
761 B
Python
Raw Normal View History

2017-07-17 15:48:41 +00:00
import errno, os, PIL, fpdf, urllib.request
2017-02-08 23:39:07 +00:00
JPG = 0
PNG = 1
def makeDir(issue):
try:
os.makedirs(str(issue))
return True
except OSError as e:
if e.errno != errno.EEXIST:
raise
return False
2017-07-17 15:48:41 +00:00
def makePDF(issue, pages, ftype = JPG):
ext = "jpg" if ftype == JPG else "png"
2017-02-08 23:39:07 +00:00
cover = PIL.Image.open("%s/1.%s" % (str(issue), ext))
x, y = cover.size
pdf = fpdf.FPDF(unit = "pt", format = [x, y])
for page in range(pages):
pdf.add_page()
2017-02-08 23:39:07 +00:00
pdf.image("%s/%i.%s" % (str(issue), page + 1, ext), 0, 0)
pdf.output("%s/issue.pdf" % str(issue), "F")
2017-07-17 15:48:41 +00:00
def downloadPage(url, issue, page, ftype = JPG):
f = urllib.request.urlopen(url)
2017-07-17 15:48:41 +00:00
with open("%s/%i.%s" % (str(issue), int(page), "jpg" if ftype == JPG else "png"), "b+w") as o:
o.write(f.read())