Skip to content

Commit 46b0b39

Browse files
committed
Improve, xoxo
1 parent 3c07f6c commit 46b0b39

File tree

3 files changed

+58
-34
lines changed

3 files changed

+58
-34
lines changed

dups.js

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,55 @@ function minDate(file) {
2929
async function run() {
3030
const folderId = Number.parseInt(process.env.FOLDER_ID, 10);
3131
const allFiles = await client.listfolder(folderId, {recursive: true});
32-
const goodFolders = allFiles.contents.filter(f => f.name.match(/^[\w-]+ \d+$/));
32+
const goodFolders = allFiles.contents.filter((f) =>
33+
f.name.match(/^[\w-]+ \d+$/)
34+
);
3335

34-
const dupFilesPerFolderUnfiltered = await pMap(goodFolders, async folder => {
35-
const name = folder.name;
36-
const imageFiles = folder.contents.filter(file => file.contenttype === 'image/jpeg');
37-
const hashToFilesMap = groupByHash(imageFiles);
38-
const fileGroups = [...hashToFilesMap.values()].filter(files => files.length > 1);
39-
fileGroups.sort((a, b) => {
40-
return minDate(a) - minDate(b);
41-
});
42-
return {name, fileGroups};
43-
});
36+
const dupFilesPerFolderUnfiltered = await pMap(
37+
goodFolders,
38+
async (folder) => {
39+
const name = folder.name;
40+
const imageFiles = folder.contents.filter(
41+
(file) => file.contenttype === 'image/jpeg'
42+
);
43+
const hashToFilesMap = groupByHash(imageFiles);
44+
const fileGroups = [...hashToFilesMap.values()].filter(
45+
(files) => files.length > 1
46+
);
47+
fileGroups.sort((a, b) => {
48+
return minDate(a) - minDate(b);
49+
});
50+
return {name, fileGroups};
51+
}
52+
);
4453

45-
const dupFilesPerFolder = dupFilesPerFolderUnfiltered.filter(({fileGroups}) => fileGroups.length);
54+
const dupFilesPerFolder = dupFilesPerFolderUnfiltered.filter(
55+
({fileGroups}) => fileGroups.length
56+
);
4657

47-
const lineFormat = f => `${f.name} (${new Date(minDate(f)).toISOString()})`;
58+
const deletion = dupFilesPerFolder.map(({name, fileGroups}) => {
59+
const keepDelGroups = fileGroups.map((g) => ({
60+
keep: g[0],
61+
del: g.slice(1)
62+
}));
63+
return {name, keepDelGroups};
64+
});
4865

49-
// Delete dups
50-
await pMap(dupFilesPerFolder, async ({name, fileGroups}) => {
51-
console.log(name);
52-
await pMap(fileGroups, async group => {
53-
const first = group[0];
54-
console.log(` Keep ${lineFormat(first)}`);
55-
await pMap(group.slice(1), file => {
56-
console.log(` Del ${lineFormat(file)}`);
57-
client.deletefile(file.fileid);
58-
});
59-
}, {concurrency: 1});
60-
}, {concurrency: 1});
66+
console.log(JSON.stringify(deletion, null, 4));
67+
68+
const allToDelete = deletion
69+
.flatMap(({keepDelGroups}) => keepDelGroups.map(({del}) => del))
70+
.flat();
71+
await pMap(
72+
allToDelete,
73+
async (file) => {
74+
console.log(`Deleting ${file.name}`);
75+
await client.deletefile(file.fileid);
76+
},
77+
{concurrency: 10}
78+
);
6179
}
6280

63-
run().catch(error => {
81+
run().catch((error) => {
6482
console.error(error);
6583
});

intersects.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ function intersects(a, b) {
1616
function mergeNames(hashesPerName) {
1717
const result = [];
1818
for (const {name, hashes} of hashesPerName) {
19-
const resultItem = result.find(i => intersects(hashes, i.hashUnion));
19+
const resultItem = result.find((i) => intersects(hashes, i.hashUnion));
2020
if (!resultItem) {
2121
result.push({names: [name], hashUnion: hashes});
2222
continue;
2323
}
2424

2525
resultItem.names.push(name);
26-
hashes.forEach(h => resultItem.hashUnion.add(h));
26+
hashes.forEach((h) => resultItem.hashUnion.add(h));
2727
}
2828

2929
return result;
@@ -32,25 +32,31 @@ function mergeNames(hashesPerName) {
3232
async function run() {
3333
const folderId = Number.parseInt(process.env.FOLDER_ID, 10);
3434
const allFiles = await client.listfolder(folderId, {recursive: true});
35-
const goodFolders = allFiles.contents.filter(f => f.name.match(/^[\w-]+ \d+$/));
35+
const goodFolders = allFiles.contents.filter((f) =>
36+
f.name.match(/^[\w-]+ \d+$/)
37+
);
3638

37-
const hashesPerName = await pMap(goodFolders, async f => {
39+
const hashesPerName = await pMap(goodFolders, async (f) => {
3840
const name = f.name;
39-
const hashList = f.contents.filter(f => f.contenttype === 'image/jpeg').map(f => f.hash);
41+
const hashList = f.contents
42+
.filter((f) => f.contenttype === 'image/jpeg')
43+
.map((f) => f.hash);
4044
const hashes = new Set(hashList);
4145
return {name, hashes};
4246
});
4347

4448
// [{names: [], hashUnion: Set}, ...]
4549
const nonSingleMergedNames = mergeNames(hashesPerName);
46-
const mergedNames = nonSingleMergedNames.filter(({names}) => names.length > 1);
50+
const mergedNames = nonSingleMergedNames.filter(
51+
({names}) => names.length > 1
52+
);
4753

4854
// [[name1, name2, name3], ...]
4955
const onlyNames = mergedNames.map(({names}) => names);
5056

5157
console.log(onlyNames);
5258
}
5359

54-
run().catch(error => {
60+
run().catch((error) => {
5561
console.error(error);
5662
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"description": "",
55
"scripts": {
6-
"lint": "xo --pretier"
6+
"lint": "xo --prettier"
77
},
88
"author": "",
99
"license": "ISC",

0 commit comments

Comments
 (0)