Skip to content

Commit

Permalink
Filter PV
Browse files Browse the repository at this point in the history
  • Loading branch information
szdytom committed Dec 14, 2023
1 parent a7c2da5 commit e8d5cac
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nyanpasu",
"type": "module",
"version": "0.1.1",
"version": "0.1.2",
"description": "BiliBili 番剧视频和弹幕元数据解析脚本",
"main": "src/index.mjs",
"scripts": {
Expand Down
66 changes: 50 additions & 16 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,20 @@ class AnimeEpisode {
}
return episodeRoot;
}

toString() {
return `Episode ${this.id}: ${this.title}`;
}
};

function parseDescriptor(source) {
let enableLogging = true;
function info(msg) {
if (enableLogging) {
console.log(`${msg}`);
}
}

function parseDescriptor(source, includePv, minDuration) {
const data = source.props?.pageProps?.dehydratedState?.queries[0]?.state?.data?.seasonInfo?.mediaInfo
if (data == null) {
throw new Error('Cannot parse descriptor: media info not found');
Expand All @@ -144,9 +155,11 @@ function parseDescriptor(source) {
rating: data.rating,
});

let duration_warn = false;
let episode_id = 1;
for (let i = 0; i < data.episodes.length; ++i) {
let edata = data.episodes[i];
let episode = new AnimeEpisode(i + 1, edata.long_title, {
let episode = new AnimeEpisode(episode_id, edata.long_title, {
aid: edata.aid,
bvid: edata.bvid,
cid: edata.cid,
Expand All @@ -158,25 +171,33 @@ function parseDescriptor(source) {
displayTitle: edata.playerEpTitle,
skip: edata.skip,
});
anime.episodes.push(episode);
}

return anime;
}
if (!includePv && edata.badge != null && edata.badge.search('预告') != -1) {
info(`Filtered pv (specify --include-pv to keep): ${episode.toString()}`);
} else if (edata.duration < minDuration * 1000) {
info(`Filtered duration: ${episode.toString()}`);
} else if (edata.duration < 180000 && minDuration === 0) { // 3 minute
duration_warn = true;
} else {
episode_id += 1;
anime.episodes.push(episode);
}
}

let enableLogging = true;
function info(msg) {
if (enableLogging) {
console.log(`${msg}`);
if (duration_warn) {
info('Some of the videos are shorter than 3 minutes, you may want to specify --min-duration to filter them out');
}

return anime;
}

async function processDescriptor(rawDesc) {
let anime = parseDescriptor(rawDesc);
async function processDescriptor(rawDesc, includePv, minDuration) {
let anime = parseDescriptor(rawDesc, includePv, minDuration);
info(`Title: ${anime.title}`);
info(`Count: ${anime.episodes.length} episodes`);

for (let episode of anime.episodes) {
info(` * Episode ${episode.id}: ${episode.title}`);
info(` * ${episode.toString()}`);
}

let doc = (new xmldom.DOMImplementation()).createDocument(DESC_XMLNS, 'xml');
Expand Down Expand Up @@ -226,7 +247,19 @@ async function main() {
type: 'boolean',
default: false,
alias: 'q',
}).usage('Uasge: <url>').help().alias('help', 'h').argv;
}).option('include-pv', {
description: 'Include pv in the playlist',
type: 'boolean',
default: false,
}).option('min-duration', {
description: 'Filter episodes with duration too small (unit: second)',
type: 'number',
default: 0,
}).option('suppress-tmux-warning', {
description: 'Suppress warning of not inside a TMUX session',
type: 'boolean',
default: false,
}).usage('Uasge: <url>').version('0.1.2').help().alias('help', 'h').argv;
const url = args._[0];
enableLogging = !args.quiet;
if (args.skipUrl && args.noCache) {
Expand Down Expand Up @@ -257,9 +290,10 @@ async function main() {
await fs.writeFile('cache.json', JSON.stringify(rawDesc));
}
}
await processDescriptor(rawDesc);

if (process.env.TMUX == null && process.env.STY == null) {
await processDescriptor(rawDesc, args.includePv, args.minDuration);

if (!args.suppressTmuxWarning && process.env.TMUX == null && process.env.STY == null) {
info('It seems that you are NOT inside a tmux or screen session!!');
}
}
Expand Down

0 comments on commit e8d5cac

Please sign in to comment.