-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
98 lines (88 loc) · 3.27 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var imdb = require('imdb-api');
var json2csv = require('json2csv');
var exec = require('child_process').exec;
var fs = require('fs');
var appArguments = process.argv.slice(2, process.argv.length);
var types = ['getinfo', 'export'];
var type;
if (appArguments.length === 0) {
type = types[0];
} else {
types.forEach(function (thisType) {
if (appArguments[0] === thisType) {
type = thisType;
}
})
}
if (!type) {
console.log("Invalid option");
process.exit();
} else if (type === types[0]) {
getMoviesList(function (moviesList) {
moviesList.forEach(function (movie) {
imdb.getReq({ name: movie }, function(err, movieInfo) {
fs.writeFile(movie + '/info.json', JSON.stringify(movieInfo, null, 4),
function (err) {
if (err) {
console.log("Cannot save movie info" + movie);
return console.log(err);
}
return console.log("Saved Info for " + movie);
}
);
});
});
});
} else if (type === types[1]) {
getMoviesList(function (moviesList) {
if (appArguments[1] === 'no-details') {
return fs.writeFile('lmdb-movies-list-export.json', JSON.stringify(moviesList, null, 4),
function (err) {
if (err) {
console.log("Cannot save movies list");
return console.log(err);
}
return console.log("Exported movies list without IMDb Info!");
}
);
}
var moviesInfoList = [];
var resolvedPromisesCount = 0;
moviesList.forEach(function (movie, index) {
imdb.getReq({ name: movie }, function(err, movieInfo) {
++resolvedPromisesCount;
if (err || !movieInfo) {
return console.log("Movie not found - " + movie);
}
moviesInfoList.push(movieInfo);
if (resolvedPromisesCount === moviesList.length) {
json2csv({ data: moviesInfoList },
function (err, moviesListCsv) {
if (err) {
console.log("Cannot convert to CSV");
return console.log(err);
}
fs.writeFile('lmdb-movies-list-export.csv', moviesListCsv,
function (err) {
if (err) {
console.log("Cannot save movies list");
return console.log(err);
}
return console.log("Exported movies list!");
}
);
}
);
}
});
});
});
}
function getMoviesList (callback) {
var movies = [];
exec("ls -d */ | cut -f1 -d'/'", function(error, stdout, stderr) {
movies = stdout.trim().split("\n");
callback && callback(movies);
});
return movies;
}