Skip to content

Commit b1497ff

Browse files
Add binary and export the module
1 parent ff3b9e9 commit b1497ff

File tree

4 files changed

+97
-83
lines changed

4 files changed

+97
-83
lines changed

bin/cli.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env node
2+
const path = require('path');
3+
const extractFilesFromCache = require('../src').extractFilesFromCache;
4+
5+
extractFilesFromCache()
6+
.then(files => files.forEach(file =>
7+
console.log(`${file.skipped ? 'Skipped' : 'Successfully extracted'} => ${path.basename(file.destination)}`)
8+
))
9+
;

index.js

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1 @@
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');

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "1.0.0",
44
"description": "Extracts video files from stremio cache folder",
55
"main": "index.js",
6-
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
6+
"bin": {
7+
"stremio-cache-extractor": "bin/cli.js"
88
},
99
"keywords": [
1010
"stremio",

src/index.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
;
21+
22+
const searchTorrents = folders => Promise.resolve()
23+
.then(() => folders.map(folderName => stat(path.join(STREMIO_CACHE_FOLDER, folderName, `${folderName}.torrent`))
24+
.then(stats => _.assign({
25+
name: folderName,
26+
folder: path.join(STREMIO_CACHE_FOLDER, folderName),
27+
location: path.join(STREMIO_CACHE_FOLDER, folderName, `${folderName}.torrent`)
28+
}, stats))
29+
.catch(err => (err.code === 'ENOTDIR' || err.code === 'ENOENT' ? null : Promise.reject(err)))
30+
))
31+
.then(runAll)
32+
.filter(notNull)
33+
;
34+
35+
const readTorrentInformation = torrents => Promise.resolve()
36+
.then(() => torrents.map(torrent => readTorrent(torrent.location)
37+
.then(stats => _.assign({ files: stats.files }, torrent))
38+
))
39+
.then(runAll)
40+
;
41+
42+
const searchFullyDownloadedFiles = torrents => Promise.resolve()
43+
.then(() => torrents.map(torrent => Promise.resolve()
44+
.then(() => torrent.files.map((file, index) => stat(path.join(torrent.folder, `${index}`))
45+
.then(stats => _.assign({ name: `${index}` }, stats))
46+
.catch(() => null)
47+
))
48+
.then(runAll)
49+
.filter(notNull)
50+
.then(fileStats => _.assign({ fileStats }, torrent))
51+
))
52+
.then(runAll)
53+
.filter(fullyDownloaded)
54+
;
55+
56+
const copyToDestination = torrents => Promise.resolve(torrents)
57+
.map(getSourceAndDestination)
58+
.map(copyFile)
59+
.then(runAll)
60+
;
61+
62+
const getSourceAndDestination = torrent => {
63+
const destination = _.maxBy(torrent.files, 'length');
64+
const source = torrent.fileStats[torrent.files.indexOf(destination)];
65+
66+
return {
67+
source: path.join(torrent.folder, source.name),
68+
destination: path.join(DESTINATION_FOLDER, path.basename(destination.path))
69+
};
70+
};
71+
72+
const runAll = promises => Promise.all(promises);
73+
74+
const notNull = value => !!value;
75+
76+
const fullyDownloaded = torrent => torrent.fileStats[0].size === torrent.files[0].length;
77+
78+
const copyFile = file => copy(file.source, file.destination, { replace: false })
79+
.thenReturn(file)
80+
.catch(() => _.assign({ skipped: true }, file))
81+
;
82+
83+
module.exports = {
84+
extractFilesFromCache
85+
};

0 commit comments

Comments
 (0)