|
| 1 | +const pcloudSdk = require('pcloud-sdk-js'); |
| 2 | +const pMap = require('p-map'); |
| 3 | + |
| 4 | +const client = pcloudSdk.createClient(process.env.ACCESS_TOKEN); |
| 5 | + |
| 6 | +function intersects(a, b) { |
| 7 | + for (const v of a.values()) { |
| 8 | + if (b.has(v)) { |
| 9 | + return true; |
| 10 | + } |
| 11 | + } |
| 12 | + |
| 13 | + return false; |
| 14 | +} |
| 15 | + |
| 16 | +function mergeNames(hashesPerName) { |
| 17 | + const result = []; |
| 18 | + for (const {name, hashes} of hashesPerName) { |
| 19 | + const resultItem = result.find(i => intersects(hashes, i.hashUnion)); |
| 20 | + if (!resultItem) { |
| 21 | + result.push({names: [name], hashUnion: hashes}); |
| 22 | + continue; |
| 23 | + } |
| 24 | + |
| 25 | + resultItem.names.push(name); |
| 26 | + hashes.forEach(h => resultItem.hashUnion.add(h)); |
| 27 | + } |
| 28 | + |
| 29 | + return result; |
| 30 | +} |
| 31 | + |
| 32 | +async function run() { |
| 33 | + const folderId = Number.parseInt(process.env.FOLDER_ID, 10); |
| 34 | + const allFiles = await client.listfolder(folderId, {recursive: true}); |
| 35 | + const goodFolders = allFiles.contents.filter(f => f.name.match(/^[\w-]+ \d+$/)); |
| 36 | + |
| 37 | + const hashesPerName = await pMap(goodFolders, async f => { |
| 38 | + const name = f.name; |
| 39 | + const hashList = f.contents.filter(f => f.contenttype === 'image/jpeg').map(f => f.hash); |
| 40 | + const hashes = new Set(hashList); |
| 41 | + return {name, hashes}; |
| 42 | + }); |
| 43 | + |
| 44 | + // [{names: [], hashUnion: Set}, ...] |
| 45 | + const nonSingleMergedNames = mergeNames(hashesPerName); |
| 46 | + const mergedNames = nonSingleMergedNames.filter(({names}) => names.length > 1); |
| 47 | + |
| 48 | + // [[name1, name2, name3], ...] |
| 49 | + const onlyNames = mergedNames.map(({names}) => names); |
| 50 | + |
| 51 | + console.log(onlyNames); |
| 52 | +} |
| 53 | + |
| 54 | +run().catch(error => { |
| 55 | + console.error(error); |
| 56 | +}); |
0 commit comments