Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit 8cc2d19

Browse files
committed
Added a simple pdf converter. Page numbering to be fixed.
1 parent 181f242 commit 8cc2d19

File tree

1 file changed

+15
-42
lines changed

1 file changed

+15
-42
lines changed

comic_scraper/comic_scraper.py

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
from random import shuffle, uniform
1313
from time import sleep
1414
from copy import deepcopy
15-
#from fpdf import FPDF
16-
#from PIL import Image
17-
#from PyPDF2 import PdfFileMerger
15+
import img2pdf
1816

1917

2018
class Comic:
@@ -184,17 +182,17 @@ def download_chapter(self):
184182
# Convert the folder to a comic book zip filename
185183
if self.comic_mode[0] == 'manga':
186184
chapter_name = os.path.join(
187-
self.comic_download_location, '%s-%g (v%d).cbz'
185+
self.comic_download_location, '%s-%g (v%d)'
188186
% (self.comic_name, self.chapter_num, self.volume_num))
189187
elif self.comic_mode[0] == 'comic':
190188
chapter_name = os.path.join(
191-
self.comic_download_location, '%s-%g.cbz'
189+
self.comic_download_location, '%s-%g'
192190
% (self.comic_name, self.chapter_num))
193191

194192
if self.comic_file_format == 'pdf':
195-
pdfdir(self.chapter_location, chapter_name)
196-
else:
197-
zipdir(self.chapter_location, chapter_name)
193+
pdfdir(self.chapter_location, chapter_name + ".pdf")
194+
elif self.comic_file_format == 'cbz':
195+
zipdir(self.chapter_location, chapter_name + ".cbz")
198196
shutil.rmtree(self.chapter_location)
199197

200198
def initialize_chapter_download(self):
@@ -329,37 +327,12 @@ def zipdir(folder, filename):
329327
def pdfdir(folder, filename):
330328
"""Create PDF of images in the folder."""
331329
assert os.path.isdir(folder)
332-
for root, dirs, files in os.walk(folder):
333-
pass
334-
335-
for fn in files:
336-
im = Image.open(folder + os.sep + fn)
337-
width, height = im.size
338-
pdf = FPDF(unit="pt", format=[width, height])
339-
pdf.add_page()
340-
pdf.image(folder + os.sep + fn, 0, 0)
341-
pdf.output(folder + os.sep + fn.rsplit('.', 1)[0] + '.pdf', 'F')
342-
343-
merger = PdfFileMerger()
344-
for fn in files:
345-
merger.append(
346-
open(folder + os.sep + fn.rsplit('.', 1)[0] + '.pdf', 'rb'))
347-
348-
merge_file = open(filename.rsplit('.', 1)[0] + '.pdf', 'wb')
349-
merger.write(merge_file)
350-
351-
352-
# cover = Image.open(folder + os.sep + fn)
353-
# width, height = cover.size
354-
# pdf = FPDF(unit = "pt", format = [width, height])
355-
# pdf.add_page()
356-
# pdf.image(folder + os.sep + fn, 0, 0)
357-
# pdf.output(folder + os.sep + fn.rsplit('.', 1)[0] + '.pdf', 'F')
358-
#
359-
# merger = PdfFileMerger()
360-
# for fn in files:
361-
# merger.append(open(folder + os.sep + fn.rsplit('.', 1)[0] + '.pdf', 'rb'))
362-
# merger.write(filename.rsplit('.', 1)[0] + '.pdf')
330+
with open(filename, "wb") as f:
331+
for root, dirs, files in os.walk(folder):
332+
# Convert images to pdf
333+
f.write(img2pdf.convert(
334+
[os.path.join(root, fn) for fn in files]))
335+
363336

364337
def main():
365338
"""Parse input and download comic(s)."""
@@ -380,16 +353,16 @@ def main():
380353
"-c", "--chapters", default=False,
381354
help="Specify chapters to download separated by : (10:20).")
382355
parser.add_argument(
383-
"-ct", "--chapterthreads", default=5,
356+
"-ct", "--chapterthreads", default=2,
384357
help="Number of parallel chapters downloads.")
385358
parser.add_argument(
386359
"-pt", "--pagethreads", default=10,
387360
help="Number of parallel chapter pages downloads (per chapter).")
388361
parser.add_argument(
389-
"-wt", "--waittime", default=10,
362+
"-wt", "--waittime", default=15,
390363
help="Wait time before retry if encountered with an error")
391364
parser.add_argument(
392-
"-rt", "--retries", default=30,
365+
"-rt", "--retries", default=10,
393366
help="Number of retries before giving up")
394367
parser.add_argument(
395368
"-f", "--format", default='cbz',

0 commit comments

Comments
 (0)