-
Notifications
You must be signed in to change notification settings - Fork 21
/
processDownload.py
263 lines (198 loc) · 8.67 KB
/
processDownload.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Ideally, all downloaded archives should run through this function.
import UploadPlugins.Madokami.uploader as up
import archCleaner as ac
import deduplicator.archChecker
import traceback
import os.path
import ScrapePlugins.MangaScraperDbBase
import settings
import runStatus
PHASH_DISTANCE = 4
NEGATIVE_KEYWORDS = [
'www.hentairules.com', # HentaiRules seems to consistently bundle lots of shit into a single archive, which
# then gets deduped against, and you wind up with 37 one-shots in a single file,
# which then makes the tags less useful.
]
class DownloadProcessor(ScrapePlugins.MangaScraperDbBase.MangaScraperDbBase):
pluginName = 'Download Processor'
loggerPath = 'Main.DlProc'
tableKey = 'n/a'
def updatePath(self, oldPath, newPath):
oldItemRoot, oldItemFile = os.path.split(oldPath)
newItemRoot, newItemFile = os.path.split(newPath)
srcRow = self.getRowsByValue(limitByKey=False, downloadpath=oldItemRoot, filename=oldItemFile)
if srcRow and len(srcRow) == 1:
self.log.info("OldPath: '%s', '%s'", oldItemRoot, oldItemFile)
self.log.info("NewPath: '%s', '%s'", newItemRoot, newItemFile)
srcId = srcRow[0]['dbId']
self.log.info("Fixing DB Path!")
self.updateDbEntryById(srcId, filename=newItemRoot, downloadpath=newItemFile)
def crossLink(self, delItem, dupItem, isPhash=False, rowId=None):
self.log.warning("Duplicate found! Cross-referencing file")
delItemRoot, delItemFile = os.path.split(delItem)
dupItemRoot, dupItemFile = os.path.split(dupItem)
self.log.info("Remove: '%s', '%s'", delItemRoot, delItemFile)
self.log.info("Match: '%s', '%s'", dupItemRoot, dupItemFile)
srcRow = self.getRowsByValue(limitByKey=False, downloadpath=delItemRoot, filename=delItemFile)
dstRow = self.getRowsByValue(limitByKey=False, downloadpath=dupItemRoot, filename=dupItemFile)
# print("HaveItem", srcRow)
if srcRow or rowId:
if rowId:
if srcRow:
if not any([rowId == row['dbId'] for row in srcRow]):
self.log.warning("Cross linking found multiple candidate SOURCE matches")
self.log.warning("Wat?")
self.log.warning("Row IDs: %s", [row['dbId'] for row in srcRow])
srcId = rowId
else:
srcId = srcRow[0]['dbId']
self.log.info("Relinking!")
self.updateDbEntryById(srcId, filename=dupItemFile, downloadpath=dupItemRoot)
if isPhash:
tags = 'deleted was-duplicate phash-duplicate'
else:
tags = 'deleted was-duplicate'
self.addTags(dbId=srcId, tags=tags, limitByKey=False)
# Allow for situations where we're linking to something that already has other links
if dstRow:
dstId = dstRow[0]['dbId']
self.addTags(dbId=srcId, tags='crosslink-{dbId}'.format(dbId=dstId), limitByKey=False)
self.addTags(dbId=dstId, tags='crosslink-{dbId}'.format(dbId=dstId), limitByKey=False)
self.log.info("Found destination row. Cross-linking!")
return
self.log.warn("Cross-referencing file failed!")
self.log.warn("Remove: '%s', '%s'", delItemRoot, delItemFile)
self.log.warn("Match: '%s', '%s'", dupItemRoot, dupItemFile)
self.log.warn("SrcRow: '%s'", srcRow)
self.log.warn("DstRow: '%s'", dstRow)
def scanIntersectingArchives(self, containerPath, intersections, phashThresh, moveToPath):
pathPositiveFilter = [item['dir'] for item in settings.mangaFolders.values()]
self.log.info("File intersections:")
keys = list(intersections)
keys.sort()
# Only look at the 3 largest keys
for key in keys[-2:]:
if not runStatus.run:
self.log.warning("Exiting early from scanIntersectingArchives() due to halt flag.")
return
self.log.info(" %s common files:", key)
# And limit the key checks to two files
for archivePath in intersections[key][:2]:
# Limit the checked files to just the context of the new file
if not archivePath.startswith(containerPath):
continue
self.log.info(" Scanning %s", archivePath)
# I need some sort of deletion lock for file removal. Outside deletion is disabled until that's done.
# EDIT: Wrapped the deduper end in a lock.
dc = deduplicator.archChecker.ArchChecker(archivePath, phashDistance=phashThresh, pathPositiveFilter=pathPositiveFilter, negativeKeywords=NEGATIVE_KEYWORDS)
retTags, bestMatch, dummy_intersections = dc.process(moveToPath=moveToPath)
retTags = retTags.strip()
if bestMatch:
self.log.info(" Scan return: '%s', best-match: '%s'", retTags, bestMatch)
isPhash = False
if "phash-duplicate" in retTags:
isPhash = True
self.crossLink(archivePath, bestMatch, isPhash=isPhash)
else:
self.log.info(" Scan return: '%s'. No best match.", retTags)
def processDownload(self, seriesName, archivePath, deleteDups=False, includePHash=False, pathPositiveFilter=None, crossReference=True, doUpload=True, rowId=None, **kwargs):
if 'phashThresh' in kwargs:
phashThresh = kwargs.pop('phashThresh')
self.log.warn("Phash search distance overridden!")
self.log.warn("Search distance = %s", phashThresh)
for line in traceback.format_stack():
self.log.warn(line.rstrip())
else:
phashThresh = PHASH_DISTANCE
self.log.info("Phash search distance = %s", phashThresh)
if 'dedupMove' in kwargs:
moveToPath = kwargs.pop('dedupMove')
else:
moveToPath = False
if moveToPath:
retTags = ""
else:
archCleaner = ac.ArchCleaner()
try:
retTags, archivePath = archCleaner.processNewArchive(archivePath, **kwargs)
except Exception:
self.log.critical("Error processing archive '%s'", archivePath)
self.log.critical(traceback.format_exc())
retTags = "corrupt unprocessable"
# Limit dedup matches to the served directories.
if not pathPositiveFilter:
self.log.info("Using manga download folders for path filtering.")
pathPositiveFilter = [item['dir'] for item in settings.mangaFolders.values()]
# Let the remote deduper do it's thing.
# It will delete duplicates automatically.
dc = deduplicator.archChecker.ArchChecker(archivePath, phashDistance=phashThresh, pathPositiveFilter=pathPositiveFilter, lock=False)
retTagsTmp, bestMatch, intersections = dc.process(moveToPath=moveToPath)
retTags += " " + retTagsTmp
retTags = retTags.strip()
if bestMatch and crossReference:
isPhash = False
if "phash-duplicate" in retTags:
isPhash = True
self.crossLink(archivePath, bestMatch, isPhash=isPhash, rowId=rowId)
# try:
# self.scanIntersectingArchives(os.path.split(archivePath)[0], intersections, phashThresh, moveToPath)
# except Exception:
# self.log.error("Failure in scanIntersectingArchives()?")
# for line in traceback.format_exc().split("\n"):
# self.log.error(line)
# self.log.error("Ignoring exception")
# processNewArchive returns "damaged" or "duplicate" for the corresponding archive states.
# Since we don't want to upload archives that are either, we skip if retTags is anything other then ""
# Also, don't upload porn
if (not self.pron) and (not retTags or retTags=="fewfiles") and seriesName and doUpload:
try:
self.log.info("Trying to upload file '%s'.", archivePath)
up.uploadFile(seriesName, archivePath)
retTags += " uploaded"
except ConnectionRefusedError:
self.log.warning("Uploading file failed! Connection Refused!")
for line in traceback.format_exc().split("\n"):
self.log.error(" %s", line)
except Exception:
self.log.error("Uploading file failed! Unknown Error!")
for line in traceback.format_exc().split("\n"):
self.log.error(" %s", line)
else:
self.log.info("File not slated for upload: '%s' (tags: '%s')", archivePath, retTags)
if retTags:
self.log.info("Applying tags to archive: '%s'", retTags)
if "deleted" in retTags:
self.log.warning("Item was deleted!")
return retTags.strip()
# Subclasses to specify the right table names
class MangaProcessor(DownloadProcessor):
tableName = 'MangaItems'
pron = False
def __init__(self, *args, **kwargs):
self.loggerPath += "-" + self.tableName
super().__init__(*args, **kwargs)
class HentaiProcessor(DownloadProcessor):
tableName = 'HentaiItems'
pron = True
def __init__(self, *args, **kwargs):
self.loggerPath += "-" + self.tableName
super().__init__(*args, **kwargs)
def processDownload(*args, **kwargs):
if 'pron' in kwargs:
isPron = kwargs.pop('pron')
else:
isPron = False
if isPron:
dlProc = HentaiProcessor()
else:
dlProc = MangaProcessor()
return dlProc.processDownload(*args, **kwargs)
def dedupItem(itemPath, rmPath):
dlProc = MangaProcessor()
dlProc.processDownload(seriesName=None, archivePath=itemPath, dedupMove=rmPath, deleteDups=True, includePHash=True)
if __name__ == "__main__":
import logSetup
logSetup.initLogging()
import sys
if len(sys.argv) > 1:
processDownload(seriesName=None, archivePath=[sys.argv[1]])