forked from sinedied/movie-renamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenamer.js
296 lines (235 loc) · 6.48 KB
/
renamer.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env node
var fs = require('fs'),
_ = require('lodash'),
q = require('q'),
request = require('request-promise'),
inquirer = require("inquirer"),
cheerio = require('cheerio'),
path = require('path');
// constants
var imdbUrl = 'http://www.imdb.com/find?q=';
var resultSelector = '.findList .result_text';
var movieExt = '.mkv';
// global
var ui = new inquirer.ui.BottomBar();
var log = ui.log.write;
var progress = {
total: 0,
current: 0
};
function main() {
var processedFiles = [];
log('Listing files...');
getMovies(process.argv[2])
.then(function(files) {
if (!files.length) {
log('No files to process!');
throw 1;
}
log('Scraping IMDB results...');
progress.total = files.length;
// scrap names for all files
return scrapNamesOnce(files);
})
.then(function(files) {
ui.updateBottomBar('');
return askUser(files);
})
.then(renameFiles);
}
function getFinalName(file, baseName) {
if (!file.results || !file.results.length)
return null;
var index;
if (!baseName && file.year) {
var regex = new RegExp(file.year);
index = _.findIndex(file.results, function(result) {
return regex.test(result);
});
}
if (!index || index < 0)
index = 0;
var name = baseName || file.results[index];
if (file.multi)
name += ' [MULTi]';
if (file.dts)
name += ' [DTS]';
if (file.vo)
name += ' [VO]';
if (file.atmos)
name += ' [Atmos]';
if (file.bluray)
name += ' [BluRay]';
if (file.quality)
name += ' [' + file.quality + 'p]';
return name + movieExt;
}
function scrapNamesOnce(files) {
var promises = _.map(files, function(file) {
return getImdbName(file.name).then(function(results) {
file.results = results;
progress.current++;
ui.updateBottomBar('Scraped ' + progress.current + '/' + progress.total);
return results;
});
});
// wait until all scrap requests are finished
return q.all(promises).then(function() {
return files;
});
}
function scrapNames(files) {
// recursive promise builder so sequence requests
var promiseBuilder = function(i) {
return getImdbName(files[i].name).then(function(results) {
files[i].results = results;
progress.current++;
ui.updateBottomBar('Scraped ' + progress.current + '/' + progress.total);
return ++i < files.length ? promiseBuilder(i) : files;
});
}
return promiseBuilder(0);
}
function askUser(files) {
var deferred = q.defer();
var questions = [];
var skipRename = '>> Do not rename';
_.each(files, function(file) {
file.new = getFinalName(file);
// best match?
questions.push({
type: 'confirm',
name: file.original + '.best',
message: ' ' + file.original + '\n -> ' + file.new,
default: true,
when: !!file.new
});
// choose from results
var choices = file.results || [];
choices.unshift(skipRename);
questions.push({
type: 'list',
name: file.original + '.choice',
message: 'Choose name',
choices: choices,
when: function(answers) {
return file.new && !answers[file.original + '.best'];
}
});
// TODO: manual option
});
inquirer.prompt(questions, function(answers) {
_.each(files, function(file) {
// update new file name if best choice was not selected
if (!answers[file.original + '.best']) {
var baseName = answers[file.original + '.choice'];
baseName = baseName && baseName !== skipRename ? baseName : null;
file.new = baseName ? getFinalName(file, baseName) : null;
}
});
deferred.resolve(files);
});
return deferred.promise;
}
function renameFiles(files) {
log('Renaming files...');
_.each(files, function(file) {
if (file.new) {
fs.rename(path.join(process.argv[2],file.original), path.join(process.argv[2],file.new), function(err) {
if (err)
log('Error while renaming "' + file.original + '": ' + err);
});
}
});
log('Done!');
}
function getMovies(path) {
var readdir = q.denodeify(fs.readdir);
return readdir(path || __dirname).then(function(files) {
files = _.filter(files, function(name) {
return name.indexOf(movieExt, name.length - movieExt.length) !== -1;
});
return _.map(files, parseFileName);
});
}
function getImdbName(search) {
var url = imdbUrl + encodeURIComponent(search);
return request(url).then(function (body) {
var $ = cheerio.load(body);
var movies = $('.findList').first();
var results = [];
movies.find('.result_text')
.each(function() {
var title = cleanTitle($(this).text());
if (title)
results.push(sanitizeForFileName(title));
});
return results;
}, function(result) {
log('Error while retrieving results for "' + search + '": '
+ JSON.stringify(result));
});
}
function cleanTitle(title) {
title = title.replace(/ \([A-Z]*?\)/g, '');
var match = /(.*?\([0-9]*\))/.exec(title);
return match ? match[1].trim() : null;
}
function sanitizeForFileName(title) {
title = title.replace(/:/g, ' -');
return title.replace(/[\/*?"<>|]/g, '').trim();
}
function parseFileName(name) {
var file = {};
file.original = name;
file.multi = /multi/i.test(name);
file.vo = !file.multi && /VO/.test(name);
if (/2160p/i.test(name) || /4k/i.test(name))
file.quality = 2160;
if (/1080p/i.test(name))
file.quality = 1080;
if (/720p/i.test(name))
file.quality = 720;
if (/dts/i.test(name))
file.dts = true;
if (/bluray/i.test(name))
file.bluray = true;
if (/atmos/i.test(name))
file.atmos = true;
name = name.substring(0, name.length - movieExt.length);
name = name.replace(/\./g, ' ');
name = name.replace(/_/g, ' ');
name = name.replace(/(\[|\])/g, '');
// name (year)
var match = /(.*?\(([0-9]*)\))/.exec(name);
if (match) {
name = match[1];
file.year = match[2];
}
// name year
match = /(.*? )([0-9]{4}) /.exec(name);
if (!file.name && match) {
name = match[1];
file.year = match[2];
}
// clean
var cleanSeparators = [
/ \([0-9]*\).*/,
/2160p.*/i,
/1080p.*/i,
/720p.*/i,
/ multi .*/i,
/bluray.*/i,
/x264.*/i,
/x265.*/i,
/hevc.*/i,
/ac3.*/i
];
_.each(cleanSeparators, function(sep) {
name = name.replace(sep, '');
});
name = sanitizeForFileName(name);
file.name = name.trim();
return file;
}
main();