Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

尝试解决拉取不到bgm数据的问题 #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions imorz/bangumi-bgmlist.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ function getOnAirYearMonth() {
if (date == undefined) throw "on-air date not found";
let [_, year, month] = date.parentElement.textContent
.match(/(\d{4})年(\d{1,2})月/);
month = month.padStart(2, '0');
return [year, month];
return [+year, +month];
}

// return full bgm list on given on-air date
async function getBgmList(year, month) {
const url = BGMLIST_URL.replace('$Y', year).replace('$M', month);
const url = BGMLIST_URL.replace('$Y', year.toString()).replace('$M', month.toString().padStart(2, '0'));
const resp = await fetch(url, FETCH_PARAMS);
if (!resp.ok) throw "fail to fetch bgmlist: " + resp.status;
let list = await resp.json();
Expand Down Expand Up @@ -70,7 +69,7 @@ function addInfoRow(title, links) {
}

function addOnAirSites(bgm, sites) {
const links = bgm.sites.map(({site, id, url}) => {
const links = bgm.sites.map(({ site, id, url }) => {
const info = sites[site];
if (!info) return;
if (!url)
Expand All @@ -87,10 +86,12 @@ window.addEventListener('DOMContentLoaded', async () => {
try {
const bgmId = location.pathname.match(/\/subject\/(\d+)/)[1];
const [year, month] = getOnAirYearMonth();
const bgm = (await getBgmList(year, month)).get(bgmId);
let bgm = (await getBgmList(year, month)).get(bgmId)
?? (await getBgmList(year, month - 1)).get(bgmId)
?? (await getBgmList(year, month + 1)).get(bgmId);

if (!bgm)
throw `#${bgmId} not found in bgmlist-${year}-${month}`;
throw `#${bgmId} not found in bgmlist-${year}-${month}|${month - 1}|${month + 1}`;

const sites = await getSiteInfo();
addOnAirSites(bgm, sites);
Expand Down