Skip to content

Commit

Permalink
Refactored Episoder #190
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffi committed Feb 20, 2015
1 parent ac1d8d5 commit f383a3c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 44 deletions.
61 changes: 17 additions & 44 deletions lib/utils/episoder.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,66 @@
//From https://github.com/rohanorton/episoder
//From https://github.com/rohanorton/episoder
var path = require("path"),
titleCase = require("to-title-case");

// filename: string
// options: object
function parseFilename(filename, options) {
var ext = path.extname(filename).toLowerCase(),
// the following regex should match:
// Community S01E04.mp4
// Community s01e04.mp4
// Community 1x04.mp4
// Community 1-04.mp4
re = /(.*)\D(\d{1,2})[ex\-](\d{1,2})/i,
searchResults = filename.match(re),
show,
season,
episode,
offset,
episodeObject = {};
// Community Season 01 Episode 04.mp4
var re = /(\w+)\s*\D(?:Season\s*)?(\d{1,2})(?:[ex\-]|\s*Episode\s*)(\d{1,2})/i;
var searchResults = filename.match(re);

if (options === undefined) {
if (!options) {
options = {};
}

offset = options.offset || 0;
if (searchResults === null) {
// this regex should match:
// Community Season 1 Episode 4.mp4
// (case insensitive)
re = /(.*)Season.*?(\d{1,2}).*Episode\D*?(\d{1,2})/i;
searchResults = filename.match(re);
}

if (searchResults === null) {
if (!searchResults) {
// this regex should match:
// Community 104.mp4
re = /(.*)\D(\d)(\d\d)\D/;
searchResults = filename.match(re);
}

if (searchResults === null && options.season) {
if (!searchResults && options.season) {
// this regex should match:
// Community 04.mp4
// but only if we've specified a season with season flag
re = /(.*)\D(\d+)\D/;
searchResults = filename.match(re);
}

if (searchResults === null && options.season && options.show) {
if (!searchResults && options.season && options.show) {
// this regex should match:
// 04.mp4
// but only if we've specified a season and show with flags
re = /(\d+)\D/;
searchResults = filename.match(re);
}

try {
show = options.show || searchResults[1];
} catch (e) {
if (!searchResults) {
return null;
}
show = titleCase(show
// remove hanging characters
.replace(/^[\-.\s]+|[\-.\s]+$/g, "")
.trim());

if (options.episode) {
episode = options.episode + offset;
if (searchResults !== null) {
searchResults.pop();
}
} else {
try {
episode = Number(searchResults.pop()) + offset;
} catch (e) {
return null;
}
}
var show = options.show || searchResults[1];
show = titleCase(show.replace(/^[\-.\s]+|[\-.\s]+$/g, "").trim());

var season = options.season || parseInt(searchResults[2], 10);

season = options.season || Number(searchResults.pop());
var offset = options.offset || 0;
var episode = (options.episode || parseInt(searchResults[3], 10)) + offset;

episodeObject = {
var ext = path.extname(filename).toLowerCase();
return {
originalFilename: filename,
show: show,
season: season,
episode: episode,
extension: ext
};
return episodeObject;
}

exports.parseFilename = parseFilename;
5 changes: 5 additions & 0 deletions spec/lib/episoder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ describe('Episoder', function () {
var result = episoder.parseFilename('Test S01E04', { offset: 2 });
assert.equal(result.episode, 6);
});

it('should not fail for show name only in filename', function() {
var result = episoder.parseFilename('Community');
assert.equal(result, null);
});
});

0 comments on commit f383a3c

Please sign in to comment.