This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomicsMerge.py
More file actions
216 lines (187 loc) · 7.83 KB
/
comicsMerge.py
File metadata and controls
216 lines (187 loc) · 7.83 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import zipfile
import os, sys
root = os.getcwd()
sys.path.append(os.path.join(root, "modules"))
sys.path.append(os.path.join(root, "modules", "colorama"))
os.environ["PATH"] = os.path.join(root, "modules", "winrar")
import rarfile
from colorama import init, Fore, Style
from termcolor import colored
from progress.bar import Bar
from datetime import datetime
init(convert=True)
zipEndTypes = [".zip", ".cbz"]
rarEndTypes = [".rar", ".cbr"]
errorsCount = 0
errors = []
while True:
bookPartsDir = input("Enter folder name with files to merge (in input folder): ")
bookPath = os.path.join(root, "input", bookPartsDir)
if (bookPartsDir == ""):
print(colored("Folder name is empty", "red"))
elif (os.path.exists(bookPath) and len(os.listdir(bookPath)) == 0):
print(colored("Folder is empty", "red"))
elif (not os.path.exists(bookPath)):
print(colored("Folder \"{0}\" does not exist".format(bookPartsDir), "red"))
else:
break
overwrite = 0
while True:
endName = input("Enter merged file name with file type (.cbz, .zip, .rar, .cbr support. <name of the input folder>.cbr by default): ") or "{0}.cbr".format(bookPartsDir)
if (endName in os.listdir(os.path.join(root, "output"))):
print(colored("Name \"{0}\" already existed.".format(endName), "red"))
while True:
overwriteInput = input("Overwrite it? y/n: ")
if overwriteInput.upper() == "Y":
overwrite = 1
break
elif overwriteInput.upper() == "N":
overwrite = 0
break
else:
print(colored("Please use Y or N.", "red"))
if (not overwrite):
continue
endType = endName[endName.rfind("."):]
if (endType in zipEndTypes):
endTypeMode = "zip"
break
elif (endType in rarEndTypes):
endTypeMode = "rar"
break
else:
print(colored("Unsupported file type \"{0}\"".format(endType), "red"))
if ("output" not in os.listdir()):
os.mkdir("output")
if ("temp" not in os.listdir()):
os.mkdir("temp")
if ("input" not in os.listdir()):
os.mkdir("input")
bookPartsList = os.listdir(bookPath)
progressBar = Bar('Processing', max=len(bookPartsList))
progressBar.update()
filesCount = 1
chapterCount = 1
if (endTypeMode == "zip"):
endFile = zipfile.ZipFile(os.path.join("output", endName), "w", allowZip64 = True)
for part in bookPartsList:
partPath = os.path.join(bookPath, part)
if os.path.isdir(partPath):
filesList = os.listdir(partPath)
for i in range(len(filesList)):
item = open(os.path.join(partPath, filesList[i]), "rb")
itemData = item.read()
fileType = filesList[i][filesList[i].rfind("."):]
formatFilesCount = str(filesCount).zfill(3)
formatChapterCount = str(chapterCount).zfill(3)
endFile.writestr(formatChapterCount + "-" + formatFilesCount + fileType, itemData)
item.close()
filesCount += 1
progressBar.next()
chapterCount += 1
filesCount = 1
elif zipfile.is_zipfile(partPath):
arhive = zipfile.ZipFile(partPath, "r")
filesList = arhive.namelist()
for i in range(len(filesList)):
fileType = filesList[i][filesList[i].rfind("."):]
itemData = arhive.read(filesList[i])
formatFilesCount = str(filesCount).zfill(3)
formatChapterCount = str(chapterCount).zfill(3)
endFile.writestr(formatChapterCount + "-" + formatFilesCount + fileType, itemData)
filesCount += 1
arhive.close()
progressBar.next()
chapterCount += 1
filesCount = 1
elif rarfile.is_rarfile(partPath):
arhive = rarfile.RarFile(partPath, "r")
filesList = arhive.namelist()
for i in range(len(filesList)):
fileType = filesList[i][filesList[i].rfind("."):]
itemData = arhive.read(filesList[i])
formatFilesCount = str(filesCount).zfill(3)
formatChapterCount = str(chapterCount).zfill(3)
endFile.writestr(formatChapterCount + "-" + formatFilesCount + fileType, itemData)
filesCount += 1
arhive.close()
progressBar.next()
chapterCount += 1
filesCount = 1
else:
errorsCount += 1
errors.append(part)
endFile.close()
elif (endTypeMode == "rar"):
endFile = os.path.join(root, "output", endName + endType)
for part in bookPartsList:
partPath = os.path.join(bookPath, part)
if os.path.isdir(partPath):
filesList = os.listdir(partPath)
for i in range(len(filesList)):#copy with change names
fileType = filesList[i][filesList[i].rfind("."):]
item = open(os.path.join(partPath, filesList[i]), "rb")
itemData = item.read()
formatFilesCount = str(filesCount).zfill(3)
formatChapterCount = str(chapterCount).zfill(3)
tempFile = open(os.path.join("temp", "{0}-{1}".format(formatChapterCount, formatFilesCount) + fileType), "wb")
tempFile.write(itemData)
tempFile.close()
item.close()
filesCount += 1
progressBar.next()
chapterCount += 1
filesCount = 1
elif zipfile.is_zipfile(partPath):
arhive = zipfile.ZipFile(partPath, "r")
filesList = arhive.namelist()
for i in range(len(filesList)):
fileType = filesList[i][filesList[i].rfind("."):]
itemData = arhive.read(filesList[i])
formatFilesCount = str(filesCount).zfill(3)
formatChapterCount = str(chapterCount).zfill(3)
tempFile = open(os.path.join("temp", "{0}-{1}".format(formatChapterCount, formatFilesCount) + fileType), "wb")
tempFile.write(itemData)
tempFile.close()
filesCount += 1
arhive.close()
progressBar.next()
chapterCount += 1
filesCount = 1
elif rarfile.is_rarfile(partPath):
arhive = rarfile.RarFile(partPath, "r")
filesList = arhive.namelist()
for i in range(len(filesList)):
fileType = filesList[i][filesList[i].rfind("."):]
itemData = arhive.read(filesList[i])
formatFilesCount = str(filesCount).zfill(3)
formatChapterCount = str(chapterCount).zfill(3)
tempFile = open(os.path.join("temp", "{0}-{1}".format(formatChapterCount, formatFilesCount) + fileType), "wb")
tempFile.write(itemData)
tempFile.close()
filesCount += 1
arhive.close()
progressBar.next()
chapterCount += 1
filesCount = 1
else:
errorsCount += 1
errors.append(part)
arhivePath = os.path.join("output", endName)
filePath = "temp"
if overwrite:
os.system("@echo off & rar d {0}>Nul".format(arhivePath))
os.system("@echo off & rar m -ep \"{0}\" {1}>Nul".format(arhivePath, filePath))
progressBar.finish()
if (errorsCount > 0):
if "errorLogs" not in os.listdir():
os.mkdir("errorLogs")
date = datetime.now()
logDate = date.strftime("%y-%m-%d-%H.%M.%S")
errorFile = open(os.path.join("errorLogs", "log{0}.txt".format(logDate)), "w")
errorFile.write("\n".join(errors))
errorFile.close()
print(colored("Unsupported files: {0}".format(errorsCount), "yellow"))
print(colored("Go to errorLogs folder for list of the error files.", "yellow"))
print(colored("Done", "green"))
input()