This repository has been archived by the owner on Nov 28, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organizer.js
184 lines (145 loc) · 5.05 KB
/
organizer.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
#!/usr/bin/env node
var fs = require('fs'),
path = require('path'),
mkdirp = require('mkdirp'),
program = require('commander'),
_ = require('underscore');
_.str = require('underscore.string');
_.mixin(_.str.exports());
var videoExtensions = ['.mkv', '.mp4', '.avi', '.mpg'];
var Folder = (function() {
var inPath, outPath;
inPath = '';
outPath = '';
Folder.files = [];
Folder.episodes = [];
function Folder(inp, outp) {
this.inPath = path.resolve(path.normalize(inp));
this.outPath = path.resolve(path.normalize(outp));
this.parse();
this.parseFiles();
}
Folder.prototype.parse = function() {
this.files = fs.readdirSync(this.inPath);
this.reduceToVideoFiles();
return this.cleanup();
};
Folder.prototype.reduceToVideoFiles = function() {
return this.files = _.filter(this.files, function(file) {
return videoExtensions.indexOf( path.extname(file) ) >= 0;
});
};
Folder.prototype.cleanup = function() {
var _this = this;
return this.files = _.map(this.files, function(file) {
return path.join(_this.inPath, file);
});
};
Folder.prototype.parseFiles = function() {
return this.episodes = _.map(this.files, function(file) {
return new Episode(file);
});
};
Folder.prototype.renameFiles = function(folderStructure, copyFiles) {
var episode, newFile, newPath, oldFile, oldPath, verb, _i, _len, _ref, _results;
_ref = this.episodes;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
episode = _ref[_i];
oldPath = episode.getPath();
newPath = folderStructure ? path.join(this.outPath, episode.generateNewRelPath()) : path.join(this.outPath, episode.generateNewFilename());
verb = copyFiles ? 'Copying' : 'Moving';
console.log("" + verb + ": " + (path.relative(process.cwd(), oldPath)) + " -> " + (path.relative(process.cwd(), newPath)));
if (!fs.existsSync(path.dirname(newPath))) {
mkdirp.sync(path.dirname(newPath));
}
if (fs.existsSync(newPath)) {
_results.push(console.info("File already exists"));
} else {
if (copyFiles) {
newFile = fs.createWriteStream(newPath);
oldFile = fs.createReadStream(oldPath);
_results.push(oldFile.once('open', function(fd) {
return oldFile.pipe(newFile);
}));
} else {
_results.push(fs.renameSync(oldPath, newPath));
}
}
}
return _results;
};
Folder.prototype.getPath = function() {
return this.inPath;
};
Folder.prototype.getFiles = function() {
return this.files;
};
Folder.prototype.getEpisodes = function() {
return this.episodes;
};
return Folder;
})();
var Episode = (function() {
var episodeRegex;
episodeRegex = /(?:.*_)*([\w .-]*)[._]{1}(?:S?(\d+)(?:E|x)(\d+)|(\d{4}\.\d{2}\.\d{2})|(?:\((\d{2})\..*?\.(\d{2})))/i;
function Episode(p) {
this.path = p;
this.filename = path.basename(this.path);
this.parse();
this.cleanup();
}
Episode.prototype.parse = function() {
var res;
res = this.filename.match(episodeRegex);
if (res.length !== 7) {
console.error("## Error parsing " + this.filename);
console.error("## res.length == " + res.length);
}
this.showName = res[1];
this.season = res[2] * 1 | res[5] * 1;
this.episode = res[3] * 1 | res[6] * 1;
return this.date = res[4] || false;
};
Episode.prototype.cleanup = function() {
return this.showName = _.map(_.words(this.showName, /\./), function(e) {
return _.capitalize(e);
}).join(' ');
};
Episode.prototype.generateNewFilename = function() {
if (this.date) {
return "" + this.showName + "." + this.date + (path.extname(this.filename));
} else {
return "" + this.showName + ".S" + (_.pad(this.season, 2, '0')) + "E" + (_.pad(this.episode, 2, '0')) + (path.extname(this.filename));
}
};
Episode.prototype.generateNewRelPath = function() {
if (this.season) {
return path.join(this.showName, "Season " + this.season, this.generateNewFilename());
} else {
return path.join(this.showName, this.generateNewFilename());
}
};
Episode.prototype.getPath = function() {
return this.path;
};
Episode.prototype.getFilename = function() {
return this.filename;
};
return Episode;
})();
program.version('0.0.1').option('-i, --in [dir]', 'Input directory').option('-o, --out [dir]', 'Output directory').option('-c, --copy', 'Copy files instead of renaming/moving them').option('-f, --folder', 'Create a folder structure "Show Name/Season #/"').parse(process.argv);
program["in"] = program["in"] || '.';
program.out = program.out || '.';
program.copy = program.copy || false;
program.folder = program.folder || false;
if (!fs.existsSync(path.resolve(program["in"]))) {
console.error("Input directory does not exist!");
return 0;
}
if (!fs.existsSync(path.resolve(program.out))) {
console.error("Output directory does not exist!");
return 0;
}
folder = new Folder(program["in"], program.out);
folder.renameFiles(program.folder, program.copy);