Skip to content

Commit d8e1e8f

Browse files
Making code look prettier
1 parent 8d070af commit d8e1e8f

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

index.js

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,66 @@ const readTorrent = Promise.promisify(readTorrentOriginal);
1212
const STREMIO_CACHE_FOLDER = path.join(process.env.HOME, 'Library', 'Application Support', 'stremio', 'stremio-cache');
1313
const DESTINATION_FOLDER = path.join(process.env.HOME, 'Downloads');
1414

15-
readDir(STREMIO_CACHE_FOLDER)
16-
.then(items => items.map(item => stat(path.join(STREMIO_CACHE_FOLDER, item, `${item}.torrent`))
17-
.then(stats => _.assign({ name: item, folder: path.join(STREMIO_CACHE_FOLDER, item), location: path.join(STREMIO_CACHE_FOLDER, item, `${item}.torrent`) }, stats))
15+
const extractFilesFromCache = () => readDir(STREMIO_CACHE_FOLDER)
16+
.then(searchTorrents)
17+
.then(readTorrentInformation)
18+
.then(searchFullyDownloadedFiles)
19+
.then(copyToDestination)
20+
.then(report)
21+
;
22+
23+
const searchTorrents = folders => Promise.resolve()
24+
.then(() => folders.map(folderName => stat(path.join(STREMIO_CACHE_FOLDER, folderName, `${folderName}.torrent`))
25+
.then(stats => _.assign({
26+
name: folderName,
27+
folder: path.join(STREMIO_CACHE_FOLDER, folderName),
28+
location: path.join(STREMIO_CACHE_FOLDER, folderName, `${folderName}.torrent`)
29+
}, stats))
1830
.catch(err => (err.code === 'ENOTDIR' || err.code === 'ENOENT' ? null : Promise.reject(err)))
1931
))
20-
.then(promises => Promise.all(promises).filter(result => !!result))
21-
.then(torrents => torrents.map(torrent => readTorrent(torrent.location)
32+
.then(runAll)
33+
.filter(notNull)
34+
;
35+
36+
const readTorrentInformation = torrents => Promise.resolve()
37+
.then(() => torrents.map(torrent => readTorrent(torrent.location)
2238
.then(stats => _.assign({ files: stats.files }, torrent))
2339
))
24-
.then(promises => Promise.all(promises))
25-
.then(torrents => torrents.map(torrent => Promise.resolve(torrent)
26-
.then(torrentInfo => torrentInfo.files.map((file, index) => stat(path.join(torrentInfo.folder, `${index}`))
40+
.then(runAll)
41+
;
42+
43+
const searchFullyDownloadedFiles = torrents => Promise.resolve()
44+
.then(() => torrents.map(torrent => Promise.resolve()
45+
.then(() => torrent.files.map((file, index) => stat(path.join(torrent.folder, `${index}`))
2746
.then(stats => _.assign({ name: `${index}` }, stats))
2847
.catch(() => null)
2948
))
3049
.then(promises => Promise.all(promises).filter(result => !!result))
3150
.then(fileStats => _.assign({ fileStats }, torrent))
3251
))
33-
.then(promises => Promise.all(promises))
34-
.filter(torrent => torrent.fileStats[0].size === torrent.files[0].length)
35-
// .each(torrent => console.log(`${}`))
36-
.map(torrent => ({
37-
source: path.join(torrent.folder, torrent.fileStats[0].name),
38-
destination: path.join(DESTINATION_FOLDER, path.basename(torrent.files[0].path))
39-
}))
40-
.map(copyAction => copy(copyAction.source, copyAction.destination, { replace: true }))
41-
.then(promises => Promise.all(promises))
52+
.then(runAll)
53+
.filter(fullyDownloaded)
54+
;
55+
56+
const copyToDestination = torrents => Promise.resolve(torrents)
57+
.map(getSourceAndDestination)
58+
.map(copyFile)
59+
.then(runAll)
4260
;
61+
62+
const report = files => files.forEach(file => console.log(`Successfully extracted ${path.basename(file.destination)}`));
63+
64+
const getSourceAndDestination = torrent => ({
65+
source: path.join(torrent.folder, torrent.fileStats[0].name),
66+
destination: path.join(DESTINATION_FOLDER, path.basename(torrent.files[0].path))
67+
});
68+
69+
const runAll = promises => Promise.all(promises);
70+
71+
const notNull = value => !!value;
72+
73+
const fullyDownloaded = torrent => torrent.fileStats[0].size === torrent.files[0].length;
74+
75+
const copyFile = file => copy(file.source, file.destination, { replace: true }).thenReturn(file);
76+
77+
extractFilesFromCache();

0 commit comments

Comments
 (0)