-
Notifications
You must be signed in to change notification settings - Fork 1
/
correction.py
225 lines (179 loc) · 9.01 KB
/
correction.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
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
217
218
219
220
221
222
223
224
225
import os
import hashlib
from termcolor import colored
import re
from banco.bd import conection
from defaults import BD_NAME
from unidecode import unidecode
from PIL import Image
from mysql.connector.errors import ProgrammingError
def getDirectoryName(directory):
folder = os.path.basename(directory) # Obtem o name da folder
if ("[JPN]" in folder.upper()) or ("[JAP]" in folder.upper()) or ("[JNP]" in folder.upper()):
folder = folder.replace("[JPN]", "").replace("[JAP]", "").replace("[JNP]", "")
elif "[" in folder:
scan = folder[folder.index("["):folder.index("]")]
folder = folder.replace(scan, "").replace("[", "").replace("]", "")
folder = folder.replace(" - ", " ")
if ("volume" in folder.lower()):
folder = folder[:folder.lower().rindex("volume")]
elif ("capítulo" in folder.lower()):
folder = folder[:folder.lower().rindex("capítulo")]
elif ("capitulo" in folder.lower()):
folder = folder[:folder.lower().rindex("capitulo")]
return folder.replace(" ", " ").strip()
class CorrectionPage:
def __init__(self, table, manga, volume, chapter):
pass
self.table = manga if table is None else table
self.table = unidecode(self.table.replace("-", " "))[:40].strip()
self.manga = manga
self.volume = volume
self.chapter = chapter
self.language = ""
self.pageName = ""
self.pageNumber = ""
self.hashPage = ""
self.hashOldPage = ""
self.isExtra = False
selectChapter = """
SELECT id, manga, volume, capitulo, linguagem FROM {}_capitulos
WHERE manga = %s AND volume = %s AND capitulo = %s AND linguagem = %s AND is_extra = %s
"""
correctionMd5Image = """
UPDATE {}_paginas SET hash_pagina = %s
WHERE id_capitulo = %s AND (nome = %s or hash_pagina = %s)
"""
tableExists = """
SELECT Table_Name FROM information_schema.tables
WHERE table_schema = "%s"
AND (Table_Name LIKE "%s_textos")
GROUP BY Table_Name
"""
def saveFile(log):
with open(os.path.abspath('') + '/sql_nao_processado.log', 'a+', encoding='utf-8') as file:
file.write(''.join(log).replace("\n", "") + '\n')
def listCorrection(description):
with open(os.path.abspath('') + '/listaCorrecao.log', 'a+', encoding='utf-8') as file:
file.write(description + '\n')
def generateSqlFile(table, correction, idChapter, information):
sql = selectChapter.format(table) % ('"'+correction.manga+'"', correction.volume, correction.chapter, '"'+correction.language+'"', correction.isExtra)
saveFile(information + " --- " + correction.manga + ' - vol: ' + correction.volume + ' cap: ' + correction.chapter + " --- 1 - " + sql )
sql = correctionMd5Image.format(table) % ('"'+correction.hashPage+'"', idChapter, '"'+correction.pageName+'"', '"'+correction.hashOldPage+'"')
saveFile(information + " --- " + correction.manga + ' - vol: ' + correction.volume + ' cap: ' + correction.chapter + " --- 2 - " + sql)
def updatePage(correction):
with conection() as connection:
file = False
table = correction.table.replace(" ", "_")
try:
cursor = connection.cursor(buffered=True)
cursor.execute(tableExists % (BD_NAME, table))
if cursor.rowcount <= 0:
file = True
except ProgrammingError as e:
print(colored(f'Erro na verificação da tabela: {e.msg}', 'red', attrs=['reverse', 'blink']))
return
if file:
generateSqlFile(table, correction, "!000", " - não encontrado tabela")
else:
idChapter = 0
try:
args = (correction.manga, correction.volume, correction.chapter, correction.language, correction.isExtra)
cursor = connection.cursor(buffered=True)
sql = selectChapter.format(table)
cursor.execute(sql, args)
if cursor.rowcount <= 0:
generateSqlFile(table, correction, "$000", " - não encontrado capitulo")
return
else:
row = cursor.next()
idChapter = row[0]
except ProgrammingError as e:
print(colored(f'Erro na obtenção do capitulo: {e.msg}', 'red', attrs=['reverse', 'blink']))
try:
cursor = connection.cursor()
args = (correction.hashPage, idChapter, correction.pageName, correction.hashOldPage)
sql = correctionMd5Image.format(table)
cursor.execute(sql, args)
connection.commit()
if cursor.rowcount <= 0:
generateSqlFile(table, correction, idChapter, " - nenhum registro atualizado no update")
else:
listCorrection(correction.language + ' - ' + correction.manga + ' - vol: ' + correction.volume + ' cap: ' + correction.chapter)
except ProgrammingError as e:
print(colored(f'Erro ao realizar o update do md5: {e.msg}', 'red', attrs=['reverse', 'blink']))
class Correction:
def __init__(self, table, language, directory, isExtra):
pass
# Caminhos temporários
self.language = language
self.directory = ''.join(directory + "/").replace("//", "/")
self.table = table
self.isExtra = isExtra
def generateClassCorrectionPage(self, directory):
folder = os.path.basename(directory) # Pasta que está
folder = folder.lower()
volume = '0'
chapter = '0'
if ("capítulo" in folder) or ("capitulo" in folder) or (("extra" in folder) and (folder.rindex("volume") < folder.rindex("extra"))):
if "capítulo" in folder:
volume = folder[folder.rindex("volume"):folder.rindex("capítulo")]
volume = volume.replace("volume", "").replace("-", "").strip()
chapter = folder[folder.rindex("capítulo"):]
chapter = chapter.replace("capítulo", "").strip()
elif "capitulo" in folder:
volume = folder[folder.rindex("volume"):folder.rindex("capitulo")]
volume = volume.replace("volume", "").replace("-", "").strip()
chapter = folder[folder.rindex("capitulo"):]
chapter = chapter.replace("capitulo", "").strip()
elif "extra" in folder:
volume = folder[folder.rindex("volume"):folder.rindex("extra")]
volume = volume.replace("volume", "").replace("-", "").strip()
chapter = folder[folder.rindex("extra"):]
chapter = chapter.replace("extra", "").strip()
volume = re.sub('\D', '', volume)
chapter = re.sub('\D', '', chapter)
if volume == "":
volume = '0'
if chapter == "":
chapter = '0'
obj = CorrectionPage(self.table, getDirectoryName(directory), volume, chapter)
obj.language = self.language
obj.isExtra = self.isExtra
return obj
def fixMD5(self):
pages = []
print(colored("Inicio da correção dos MD5...", 'green', attrs=['reverse', 'blink']))
for directory, subFolder, files in os.walk(self.directory):
# Ignora as pastas temporárias da busca
subFolder[:] = [sub for sub in subFolder if sub not in ['tmp']]
# Ignora as pastas de capa
subFolder[:] = [sub for sub in subFolder if "capa" not in sub.lower()]
if len(files) <= 0:
continue
pageNumber = 0
for file in files:
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
pageNumber += 1
page = self.generateClassCorrectionPage(directory)
md5hash = hashlib.md5()
with open(os.path.join(directory, file),'rb') as f:
line = f.read()
md5hash.update(line)
page.hashPage = md5hash.hexdigest()
try:
md5hashOld = hashlib.md5(Image.open(os.path.join(directory, file)).tobytes())
page.hashOldPage = md5hashOld.hexdigest()
except ProgrammingError as e:
print(colored(f'Erro ao carregar hash antigo: {e.msg}', 'red', attrs=['reverse', 'blink']))
page.pageName = file
pages.append(page)
print(os.path.join(directory, file))
print(colored("Atualizando as paginas...", 'green', attrs=['reverse', 'blink']))
for page in pages:
print(colored("Atualizando MD5: " + page.manga + " - vol: " + page.volume + " cap: " + page.chapter + " - " + page.pageName, 'green', attrs=['reverse', 'blink']))
updatePage(page)
print(colored("Concluido. " + self.directory, 'green', attrs=['reverse', 'blink']))
if __name__ == '__main__':
correction = Correction(None, "ja", "G:/reprocessando/aa obter pelo nome", False)
correction.fixMD5()