feat(stats): separate YouTube channels in the Library (#253)

This commit is contained in:
sudacode authored and GitHub committed 2026-09-20 19:57:32 -07:00
1 parent 76e43d7e6d
commit 8097ed27e5
35 files changed
+888 -81

No files matched your search

+37 -5
View File
@@ -2,6 +2,7 @@ import { selectAll, selectOne, type SqlRow, type SyncDb } from './libsql-driver'
import { insertRow, tableExists, type SyncMergeSummary } from './shared';
const ANIME_COPY_COLUMNS = [
'media_kind',
'normalized_title_key',
'canonical_title',
'anilist_id',
@@ -98,11 +99,28 @@ export function mergeAnime(
summary: SyncMergeSummary,
): Map<number, number> {
const map = new Map<number, number>();
const byAnilist = local.query('SELECT anime_id FROM imm_anime WHERE anilist_id = ?');
const byTitleKey = local.query('SELECT anime_id FROM imm_anime WHERE normalized_title_key = ?');
const byAnilist = local.query(
"SELECT anime_id FROM imm_anime WHERE anilist_id = ? AND media_kind = 'anime'",
);
const byTitleKey = local.query(
'SELECT anime_id FROM imm_anime WHERE normalized_title_key = ? AND media_kind = ?',
);
// A pre-classification channel can be repaired, but a genuine anime sharing
// its title must remain a separate entry.
const legacyChannel = local.query(`SELECT anime_id FROM imm_anime
WHERE normalized_title_key = ? AND media_kind = 'anime' AND (
normalized_title_key LIKE 'youtube channel %'
OR CASE WHEN json_valid(metadata_json)
THEN json_extract(metadata_json, '$.source') = 'youtube-channel' ELSE 0 END
)`);
const releaseChannelAnilistId = local.query(
"UPDATE imm_anime SET anilist_id = NULL WHERE media_kind = 'youtube' AND anilist_id = ?",
);
const fillMissing = local.query(
`UPDATE imm_anime
SET
media_kind = ?,
anilist_id = CASE WHEN ? = 'youtube' THEN NULL ELSE anilist_id END,
title_romaji = COALESCE(title_romaji, ?),
title_english = COALESCE(title_english, ?),
title_native = COALESCE(title_native, ?),
@@ -116,12 +134,24 @@ export function mergeAnime(
`SELECT anime_id, ${ANIME_COPY_COLUMNS.join(', ')} FROM imm_anime`,
)) {
const remoteId = Number(row.anime_id);
const existing = ((row.anilist_id !== null ? byAnilist.get(row.anilist_id) : undefined) ??
byTitleKey.get(row.normalized_title_key)) as SqlRow | undefined;
if (row.media_kind === 'anime' && row.anilist_id !== null) {
// AniList identifiers belong to anime, including when an older peer
// incorrectly attached one to a channel.
releaseChannelAnilistId.run(row.anilist_id);
}
const existing = ((row.media_kind === 'anime' && row.anilist_id !== null
? byAnilist.get(row.anilist_id)
: undefined) ??
byTitleKey.get(row.normalized_title_key, row.media_kind) ??
(row.media_kind === 'youtube' ? legacyChannel.get(row.normalized_title_key) : undefined)) as
| SqlRow
| undefined;
if (existing) {
const localId = Number(existing.anime_id);
map.set(remoteId, localId);
fillMissing.run(
row.media_kind,
row.media_kind,
row.title_romaji,
row.title_english,
row.title_native,
@@ -133,7 +163,9 @@ export function mergeAnime(
}
// No local row matched by anilist_id (checked first in `existing` above)
// or title key, so the remote anilist_id — if any — is free to insert as-is.
const values = ANIME_COPY_COLUMNS.map((column) => row[column]);
const values = ANIME_COPY_COLUMNS.map((column) =>
column === 'anilist_id' && row.media_kind !== 'anime' ? null : row[column],
);
map.set(remoteId, insertRow(local, 'imm_anime', ANIME_COPY_COLUMNS, values));
summary.animeAdded += 1;
}
@@ -98,3 +98,122 @@ for (const legacyOccurrences of [false, true]) {
}
});
}
test('sync preserves the YouTube media kind when adding a channel', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-sync-youtube-'));
try {
const localPath = buildDb(dir, 'local.sqlite', {
word: '猫',
seenMs: BASE_MS,
legacyOccurrences: false,
});
const remotePath = buildDb(dir, 'remote.sqlite', {
word: '犬',
seenMs: BASE_MS,
legacyOccurrences: false,
});
const remote = new Database(remotePath);
remote.exec("UPDATE imm_anime SET media_kind = 'youtube'");
remote.close();
mergeSnapshotIntoDb(localPath, remotePath);
const local = new Database(localPath);
try {
const row = local
.prepare(
"SELECT media_kind FROM imm_anime WHERE normalized_title_key = 'key-remote.sqlite'",
)
.get();
assert.ok(row && typeof row === 'object' && 'media_kind' in row);
assert.equal(row.media_kind, 'youtube');
} finally {
local.close();
}
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
test('sync separates same-title kinds and repairs only identifiable legacy channels', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-sync-kinds-'));
const localPath = buildDb(dir, 'local', {
word: 'local',
seenMs: BASE_MS,
legacyOccurrences: false,
});
const remotePath = buildDb(dir, 'remote', {
word: 'remote',
seenMs: BASE_MS,
legacyOccurrences: false,
});
try {
const local = new Database(localPath);
local.exec(`UPDATE imm_anime SET normalized_title_key = 'shared', anilist_id = 42;
INSERT INTO imm_anime(anime_id, normalized_title_key, canonical_title, metadata_json, title_english, LAST_UPDATE_DATE)
VALUES (2, 'legacy', 'Local channel', '{"source":"youtube-channel"}', 'Keep local', '9999999999999');
UPDATE imm_anime SET anilist_id = 99 WHERE anime_id = 2;
INSERT INTO imm_anime(anime_id, normalized_title_key, canonical_title, media_kind, anilist_id)
VALUES (3, 'other shared', 'Channel with bad ID', 'youtube', 77);`);
local.close();
const remote = new Database(remotePath);
remote.exec(`UPDATE imm_anime SET normalized_title_key = 'shared', media_kind = 'youtube', anilist_id = 42;
INSERT INTO imm_anime(anime_id, normalized_title_key, canonical_title, media_kind, title_english, description, LAST_UPDATE_DATE)
VALUES (2, 'legacy', 'Remote channel', 'youtube', 'Remote title', 'Fill missing', '1');
INSERT INTO imm_anime(anime_id, normalized_title_key, canonical_title, media_kind, anilist_id)
VALUES (3, 'other shared', 'Real anime', 'anime', 77);`);
remote.close();
mergeSnapshotIntoDb(localPath, remotePath);
mergeSnapshotIntoDb(localPath, remotePath);
const db = new Database(localPath);
try {
const rows = db
.prepare(
'SELECT media_kind FROM imm_anime WHERE normalized_title_key = ? ORDER BY media_kind',
)
.all('shared');
assert.deepEqual(rows, [{ media_kind: 'anime' }, { media_kind: 'youtube' }]);
assert.deepEqual(
db
.prepare(
"SELECT media_kind FROM imm_anime WHERE normalized_title_key = 'other shared' ORDER BY media_kind",
)
.all(),
[{ media_kind: 'anime' }, { media_kind: 'youtube' }],
);
assert.equal(
(
db.prepare('SELECT media_kind FROM imm_anime WHERE anilist_id = 77').get() as {
media_kind: string;
}
).media_kind,
'anime',
);
assert.deepEqual(
db
.prepare(
'SELECT media_kind, anilist_id, title_english, description FROM imm_anime WHERE anime_id = 2',
)
.all()[0],
{
media_kind: 'youtube',
anilist_id: null,
title_english: 'Keep local',
description: 'Fill missing',
},
);
assert.equal(
(
db
.prepare(
"SELECT a.media_kind FROM imm_videos v JOIN imm_anime a ON a.anime_id = v.anime_id WHERE v.video_key = 'video-remote'",
)
.get() as { media_kind: string }
).media_kind,
'youtube',
);
} finally {
db.close();
}
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});