|
1 |
| -const Promise = require('bluebird'); |
2 |
| -const fs = require('fs.extra'); |
3 |
| -const _ = require('lodash'); |
4 |
| -const readTorrentOriginal = require('read-torrent'); |
5 |
| -const path = require('path'); |
6 |
| - |
7 |
| -const readDir = Promise.promisify(fs.readdir); |
8 |
| -const stat = Promise.promisify(fs.stat); |
9 |
| -const copy = Promise.promisify(fs.copy); |
10 |
| -const readTorrent = Promise.promisify(readTorrentOriginal); |
11 |
| - |
12 |
| -const STREMIO_CACHE_FOLDER = path.join(process.env.HOME, 'Library', 'Application Support', 'stremio', 'stremio-cache'); |
13 |
| -const DESTINATION_FOLDER = path.join(process.env.HOME, 'Downloads'); |
14 |
| - |
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)) |
30 |
| - .catch(err => (err.code === 'ENOTDIR' || err.code === 'ENOENT' ? null : Promise.reject(err))) |
31 |
| - )) |
32 |
| - .then(runAll) |
33 |
| - .filter(notNull) |
34 |
| -; |
35 |
| - |
36 |
| -const readTorrentInformation = torrents => Promise.resolve() |
37 |
| - .then(() => torrents.map(torrent => readTorrent(torrent.location) |
38 |
| - .then(stats => _.assign({ files: stats.files }, torrent)) |
39 |
| - )) |
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}`)) |
46 |
| - .then(stats => _.assign({ name: `${index}` }, stats)) |
47 |
| - .catch(() => null) |
48 |
| - )) |
49 |
| - .then(runAll) |
50 |
| - .filter(notNull) |
51 |
| - .then(fileStats => _.assign({ fileStats }, torrent)) |
52 |
| - )) |
53 |
| - .then(runAll) |
54 |
| - .filter(fullyDownloaded) |
55 |
| -; |
56 |
| - |
57 |
| -const copyToDestination = torrents => Promise.resolve(torrents) |
58 |
| - .map(getSourceAndDestination) |
59 |
| - .map(copyFile) |
60 |
| - .then(runAll) |
61 |
| -; |
62 |
| - |
63 |
| -const report = files => files.forEach(file => console.log(`${file.skipped ? 'Skipped' : 'Successfully extracted'} => ${path.basename(file.destination)}`)); |
64 |
| - |
65 |
| -const getSourceAndDestination = torrent => ({ |
66 |
| - source: path.join(torrent.folder, torrent.fileStats[0].name), |
67 |
| - destination: path.join(DESTINATION_FOLDER, path.basename(torrent.files[0].path)) |
68 |
| -}); |
69 |
| - |
70 |
| -const runAll = promises => Promise.all(promises); |
71 |
| - |
72 |
| -const notNull = value => !!value; |
73 |
| - |
74 |
| -const fullyDownloaded = torrent => torrent.fileStats[0].size === torrent.files[0].length; |
75 |
| - |
76 |
| -const copyFile = file => copy(file.source, file.destination, { replace: false }) |
77 |
| - .thenReturn(file) |
78 |
| - .catch(() => _.assign({ skipped: true }, file)) |
79 |
| -; |
80 |
| - |
81 |
| -extractFilesFromCache(); |
| 1 | +module.exports = require('./src'); |
0 commit comments