Initial implementation of filesets
parent
7b1cb06e94
commit
31b8602f52
@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const rfr = require("rfr");
|
||||
const inExtensions = rfr("lib/path/in-extensions");
|
||||
const subtractExtension = rfr("lib/path/subtract-extension");
|
||||
|
||||
module.exports = function(filename, extension, candidateFilenames, candidateExtensions) {
|
||||
let basename = subtractExtension(filename, extension);
|
||||
|
||||
return candidateFilenames.map((candidate) => {
|
||||
return {
|
||||
filename: candidate,
|
||||
extensionMatch: inExtensions(candidate, candidateExtensions)
|
||||
}
|
||||
}).filter((candidate) => {
|
||||
return candidate.extensionMatch !== false;
|
||||
}).filter((candidate) => {
|
||||
return (subtractExtension(candidate.filename, candidate.extensionMatch) === basename);
|
||||
}).map((candidate) => {
|
||||
return candidate.filename;
|
||||
});
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
'use strict';
|
||||
|
||||
const xtend = require("xtend");
|
||||
const rfr = require("rfr");
|
||||
|
||||
let plugins = [
|
||||
rfr("lib/filesets/plugins/youtube-dl"),
|
||||
rfr("lib/filesets/plugins/generic-subtitles")
|
||||
];
|
||||
|
||||
function attemptPairing(item, allItems) {
|
||||
for (let i = 0; i < plugins.length; i++) {
|
||||
let result = plugins[i](item, allItems);
|
||||
|
||||
if (result.length > 0) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
module.exports = function(items, options) {
|
||||
let indexedItems = items.reduce((total, item) => {
|
||||
total[item.name] = item;
|
||||
return total;
|
||||
}, {});
|
||||
|
||||
let pairedItems = [];
|
||||
let finalItems = [];
|
||||
|
||||
items.forEach((item) => {
|
||||
if (pairedItems.indexOf(item.name) !== -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
let pairing = attemptPairing(item, indexedItems).map((pairedItemName) => {
|
||||
return indexedItems[pairedItemName];
|
||||
});
|
||||
|
||||
if (pairing.length > 0) {
|
||||
pairedItems.push(item.name);
|
||||
|
||||
pairing.forEach((pairedItem) => {
|
||||
pairedItems.push(pairedItem.name);
|
||||
});
|
||||
|
||||
if (options.sorter != null) {
|
||||
pairing.sort(options.sorter);
|
||||
}
|
||||
|
||||
finalItems.push(xtend(item, {
|
||||
setItems: pairing
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
// Add all unpaired items to the final list.
|
||||
items.forEach((item) => {
|
||||
if (pairedItems.indexOf(item.name) === -1 && item.setItems == null) {
|
||||
finalItems.push(item);
|
||||
}
|
||||
})
|
||||
|
||||
if (options.sorter != null) {
|
||||
finalItems.sort(options.sorter);
|
||||
}
|
||||
|
||||
return finalItems;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
const rfr = require("rfr");
|
||||
const inExtensions = rfr("lib/path/in-extensions");
|
||||
const findMatchingBasenames = rfr("lib/filesets/find-matching-basenames");
|
||||
|
||||
module.exports = function(item, allItems) {
|
||||
let matchExtensions = [
|
||||
".mp4",
|
||||
".mkv",
|
||||
".flv",
|
||||
".avi",
|
||||
".ogv",
|
||||
".mpeg",
|
||||
".wmv"
|
||||
]
|
||||
|
||||
let setExtensions = [
|
||||
".vtt",
|
||||
".srt",
|
||||
".sub"
|
||||
];
|
||||
|
||||
let match = inExtensions(item.name, matchExtensions);
|
||||
|
||||
if (match) {
|
||||
return findMatchingBasenames(item.name, match, Object.keys(allItems), setExtensions);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
'use strict';
|
||||
|
||||
const rfr = require("rfr");
|
||||
const inExtensions = rfr("lib/path/in-extensions");
|
||||
const findMatchingBasenames = rfr("lib/filesets/find-matching-basenames");
|
||||
|
||||
module.exports = function(item, allItems) {
|
||||
let matchExtensions = [
|
||||
".mp4",
|
||||
".mkv",
|
||||
".flv"
|
||||
]
|
||||
|
||||
let subtitleSetExtensions = [
|
||||
".[a-z]{2}.vtt",
|
||||
".[a-z]{2}.srt"
|
||||
]
|
||||
|
||||
let setExtensions = [
|
||||
".annotations.xml",
|
||||
".info.json",
|
||||
".description",
|
||||
".jpg"
|
||||
].concat(subtitleSetExtensions);
|
||||
|
||||
let match = inExtensions(item.name, matchExtensions);
|
||||
|
||||
if (match) {
|
||||
let setFiles = findMatchingBasenames(item.name, match, Object.keys(allItems), setExtensions);
|
||||
|
||||
let nonSubtitles = setFiles.filter((setItem) => {
|
||||
return !(inExtensions(setItem, subtitleSetExtensions));
|
||||
});
|
||||
|
||||
if (nonSubtitles.length > 0) {
|
||||
return setFiles;
|
||||
} else {
|
||||
// If we have either nothing or only subtitles, let the generic video file plugin handle it.
|
||||
return [];
|
||||
}
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(filename, extension) {
|
||||
let matcher = new RegExp(extension.replace(/\./g, "\.") + "$");
|
||||
return matcher.test(filename);
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const rfr = require("rfr");
|
||||
const extensionMatches = rfr("lib/path/extension-matches");
|
||||
|
||||
module.exports = function inExtensions(filename, extensions) {
|
||||
let matches = extensions.filter((extension) => {
|
||||
return extensionMatches(filename, extension);
|
||||
});
|
||||
|
||||
if (matches.length > 0) {
|
||||
return matches[0];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(filename, extension) {
|
||||
let matcher = new RegExp(extension.replace(/\./g, "\.") + "$");
|
||||
return filename.replace(matcher, "");
|
||||
}
|
Loading…
Reference in New Issue