-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
154 lines (137 loc) · 6.08 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import json
import sys
import requests
import os
import shutil
import uuid
from PIL import Image
APP_VERSION = "v1.0"
APP_AUTHOR = "Anindya Das"
APP_REPO_URL = "https://github.com/Anindya-Das02"
APP_INFO = f"""Comic Downloader ({APP_VERSION})
=========================
A simple python script to download your favourite Comics, Mangas & more. Easily create good quality pdfs.
Please read "How to use.md" from github for detailed instructions.
Created By: {APP_AUTHOR}
GitHub: {APP_REPO_URL}"""
APP_HELP = f"Please refer {APP_REPO_URL}"
class ArgumentManager:
def __init__(self, arg) -> None:
if arg == "--version" or arg == "-v":
print(f"App version: {APP_VERSION}")
elif arg == "--info" or arg == "-i":
print(APP_INFO)
elif arg == "--help" or arg == "-h":
print(APP_HELP)
class ComicDownloader:
def __init__(self) -> None:
print("reading source_reader.json ...")
self.fd = json.load(open("source_reader.json",'r'))
self.source_url = self.fd['source_url']
self.d_type = self.fd['d_type']
self.total_pages = self.fd['total_pages']
self.file_name = self.fd['file_name']
self.img_extension = self.fd['img_extension']
self.clean_up = self.fd['clean_up']
def do_task(self) -> None:
self.process_requirements()
if self.fd["source_url"] == "<file_source>":
self.file_source_download()
else:
self.download()
self.create_pdf()
self.clean_up_process()
print("[E.N.D]")
def process_requirements(self) -> None:
# pdfs created will be stored in downloads folder
# create downloads folder if not found
if not os.path.isdir("downloads"):
os.mkdir("downloads")
# creating dir imgs-*/ to save (or) hold downloaded images
uuid_string = str(uuid.uuid1().hex)
self.target_dir = f"imgs_{uuid_string}"
os.mkdir(self.target_dir)
print("pre-download requirements completed!")
def file_source_download(self):
if "file_source" not in self.fd or self.fd["file_source"] == None or len(self.fd["file_source"].strip()) == 0:
print(f"[ERROR] 'file_source' tag is empty or missing in source_reader.json")
self.terminate_process()
urls = []
try:
with open(self.fd['file_source'],'r') as source_file:
urls.extend([line.strip("\n") for line in source_file.readlines()])
except FileNotFoundError:
print(f"[ERROR] file not found! File '{self.fd['file_source']}' does not exists in this folder!")
self.terminate_process()
print(f"fetching from {len(urls)}..")
page_no = 1
for url in urls:
try:
print(f"downloading {page_no}: {url}")
data = self.fetch_image_data(url)
self.write_bytes_to_file(data,page_no)
print(f"downloading {page_no}: {url}")
except:
print("[ERROR] an error occured while downloading image (or) writing image file")
page_no += 1
def download(self) -> None:
print("Initiating download..")
print(f"{self.total_pages} images to download")
if self.d_type.lower() in ("padded_incremental", "pi"):
if "pad_len" not in self.fd['padded_incremental'] or "start_no" not in self.fd['padded_incremental']:
print("[ERROR] required tag(s) 'pad_len' or 'start_no' missing in source_reader.json")
print("[TRY] adding tag(s) 'pad_len', 'start_no' inside 'padded_incremental' tag")
self.terminate_process()
padding = self.fd['padded_incremental']['pad_len']
page_start = self.fd['padded_incremental']['start_no']
self.download_images_from_url(padding = padding, page_start = page_start)
else:
self.download_images_from_url()
def write_bytes_to_file(self,data,page_no) -> None:
page_name = f"{self.target_dir}/page-{page_no}.{self.img_extension}"
with open(page_name,'wb') as image_file:
image_file.write(data)
def download_images_from_url(self, padding = 0, page_start = 1) -> None:
total_page = self.total_pages
for i in range(page_start,self.total_pages + 1):
try:
page_no = str(i).zfill(padding + 1)
url = f"{self.source_url.replace('*', page_no)}"
print(f"downloading [{i}/{total_page}]: {url}")
img_data = self.fetch_image_data(url)
self.write_bytes_to_file(img_data,page_no)
print(f"download completed [{i}/{total_page}]")
except:
print("[ERROR] an error occured while downloading image (or) writing image file")
print("Finished image downloading process..")
def fetch_image_data(self,url:str = '') -> bytes :
return requests.get(url).content
def create_pdf(self) -> None:
print("creating pdf..")
img_list = [i for i in os.listdir(self.target_dir)]
imgs_to_pdf_list = []
for i in sorted(img_list):
imgs_to_pdf_list.append(Image.open(f"{self.target_dir}/{i}").convert('RGB'))
filename = f"{self.file_name}.pdf"
imgs_to_pdf_list[0].save(f"downloads/{filename}",save_all=True, append_images=imgs_to_pdf_list[1:])
print(f"'{filename}' created successfully")
def clean_up_process(self,error_clean = False) -> None:
if error_clean or self.clean_up:
print("starting cleanup..")
try:
shutil.rmtree(self.target_dir)
print("clean up completed!")
except:
print("[ERROR] error occured while clean up")
else:
print("cleanup skipped!")
def terminate_process(self) -> None:
# remove empty folder img-*/ on exception
self.clean_up_process(error_clean = True)
print("terminating...")
exit(0)
if __name__ == '__main__':
if len(sys.argv) > 1:
ArgumentManager(sys.argv[1])
exit(0)
ComicDownloader().do_task()