-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
55 lines (45 loc) · 1.61 KB
/
main.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
// tvdb: https://www.thetvdb.com/?tab=seasonall&id=72514
const fs = require('fs');
const path = require('path');
const CSV = require('csv-string');
const Finder = require('fs-finder');
const chalk = require('chalk');
const argv = require('minimist')(process.argv.slice(2));
const tvdbPath = path.resolve('./tvdb.csv');
const libraryPath = path.resolve(argv.library);
if (!argv.run) {
console.log('DRY RUN START');
}
console.log(`Loading ${libraryPath}`);
console.log(`Loading ${tvdbPath}`);
console.log('');
const files = Finder.from(libraryPath).findFiles();
const content = fs.readFileSync(tvdbPath, 'utf8');
const tvdb = CSV.parse(content);
for (const episodeRow of tvdb) {
const year = episodeRow[0].split(' x ')[0];
const number = episodeRow[0].split(' x ')[1];
const name = episodeRow[1];
const plex = `S${year}E${number} - ${name}`;
const matchingFilePath = files.find((file) => {
return new RegExp(name, 'i').test(file);
});
if (matchingFilePath) {
const pathSegments = matchingFilePath.split('/');
const fileName = pathSegments[pathSegments.length - 1];
const fileExt = path.extname(fileName);
const newFileName = `${plex}${fileExt}`;
const newFilePath = matchingFilePath.replace(fileName, newFileName);
if (argv.run === true) {
console.log(`Renaming ${chalk.blue(matchingFilePath)} to ${chalk.green(newFilePath)}`);
console.log('');
fs.renameSync(matchingFilePath, newFilePath);
} else {
console.log(`Renaming ${chalk.blue(matchingFilePath)} to ${chalk.green(newFilePath)}`);
console.log('');
}
}
}
if (!argv.run) {
console.log('DRY RUN END');
}