-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
22 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters