-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.js
More file actions
66 lines (62 loc) · 2.14 KB
/
parser.js
File metadata and controls
66 lines (62 loc) · 2.14 KB
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
var log = require('winston');
/**
* Parse filename/directory to extract information for name and season.
*
* @param {string} the file name
* @return {object} the show info, or null if parsing failed
**/
module.exports.parse = function(file) {
var res;
var info = {};
// For shows that have normal S??E?? (with extr E?? for 2 episodes in 1)
if (res = /(.*)\.S(\d\d)E(\d\d)(E(\d\d))?\..*/i.exec(file)) {
info.show = res[1];
info.season = res[2];
info.episode = res[5] || res[3];
log.info('debug', 'Matched on (.*)\\.S(\\d\\d)E(\\d\\d)(E(\\d\\d))?\\..*');
}
else
// for shows that have only one season (no season number in the title)
if (res = /(.*)\.E(\d\d)\..*/i.exec(file)) {
info.show = res[1];
info.season = res[2];
info.episode = res[3];
log.info('debug', 'Matched on (.*)\\.E(\\d\\d)');
}
else if (res = /(.*)\.(\d{1,2})(\d\d)\..*/i.exec(file)) {
info.show = res[1];
info.season = res[2];
info.episode = res[3];
log.info('debug', 'Matched on (.*)\\.(\\d{1,2})(\\d\\d)\\..*');
}
else
// special case for shows that run daily, with year.month.day format
if (res = /(The\.Colbert\.Report|The\.Daily\.Show)\.(\d{4}.\d{2})\.(\d{2})/i.exec(file)) {
info.show = res[1];
info.season = res[2];
info.episode = res[3];
log.info('debug', 'Matched on (The\\.Colbert\\.Report|The\\.Daily\\.Show)\\.(\\d{4}.\\d{2})\\.(\\d{2})');
}
// extract file type from extension
if (res = /\.(...)$/.exec(file)) {
switch (res[1].toLowerCase()) {
case 'mkv':
info.type = "x264";
break;
case 'avi':
info.type = "XviD";
break;
case 'mp4':
info.type = "MP4";
break;
}
}
// Make sure every attribute has been found
return ['show', 'season', 'episode', 'type'].every(function(attr) {
if (info[attr] === undefined) {
log.info('error', 'Could not parse ' + file + '. Missing ' + attr + ' !');
return false;
}
return true;
}) ? info : null;
};