-
Notifications
You must be signed in to change notification settings - Fork 1
/
tv.js
63 lines (56 loc) · 2.45 KB
/
tv.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
var fs = require('fs');
var request = require('request');
var SReg = /[Ss](\d+)/;
var EReg = /[Ee](\d+)/;
var extReg = /(\..{3})\b/;
var fileArr = [];
var path = 'D:\\TV Shows\\Friends\\SEASON 3'; // path of the tv show folder
var id = '1668'; // themoviedb show id
fs.readdir(path, function (err, files) {
if (!err){
console.log(files);
fileArr = files;
for (var i = 0; i < fileArr.length; i++) {
var extension = extReg.exec(fileArr[i]);
if (extension){
extension = extension[1];
}
if (fileArr[i].search(extension) != -1){
var season = SReg.exec(fileArr[i]);
var episode = EReg.exec(fileArr[i]);
if (season && episode){
season = season[1];
episode = episode[1];
(function (oldname, season, episode, id, extension){
request({
method: 'GET',
url: 'http://api.themoviedb.org/3/tv/'+id+'/season/'+season+'/episode/'+episode+'?api_key=68ab7f92986673908521e2af900d2786',
headers: {
'Accept': 'application/json'
}}, function (error, response, body) {
if (error) {
return null;
}
else {
body = JSON.parse(body);
if(body.season_number < 10){
body.season_number = "0"+body.season_number;
}
if(body.episode_number < 10){
body.episode_number = "0"+body.episode_number;
}
var rename = "S"+body.season_number+"E"+body.episode_number+" - "+body.name;
console.log(rename);
rename = rename.replace(/[\/\\<>:\*\?\|"]/g, '');
fs.rename(path+"\\"+oldname, path+"\\"+rename+extension);
}
});
})(fileArr[i], season, episode, id, extension);
}
}
}
}
else{
console.log(err);
}
});