@@ -12,31 +12,66 @@ const readTorrent = Promise.promisify(readTorrentOriginal);
12
12
const STREMIO_CACHE_FOLDER = path . join ( process . env . HOME , 'Library' , 'Application Support' , 'stremio' , 'stremio-cache' ) ;
13
13
const DESTINATION_FOLDER = path . join ( process . env . HOME , 'Downloads' ) ;
14
14
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 ) )
18
30
. catch ( err => ( err . code === 'ENOTDIR' || err . code === 'ENOENT' ? null : Promise . reject ( err ) ) )
19
31
) )
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 )
22
38
. then ( stats => _ . assign ( { files : stats . files } , torrent ) )
23
39
) )
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 } ` ) )
27
46
. then ( stats => _ . assign ( { name : `${ index } ` } , stats ) )
28
47
. catch ( ( ) => null )
29
48
) )
30
49
. then ( promises => Promise . all ( promises ) . filter ( result => ! ! result ) )
31
50
. then ( fileStats => _ . assign ( { fileStats } , torrent ) )
32
51
) )
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 )
42
60
;
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