From 6ec9914fe6e31852422653fd464b26dda17335b3 Mon Sep 17 00:00:00 2001 From: ipcjs Date: Sat, 9 Apr 2022 06:47:27 +0800 Subject: [PATCH 1/3] Make BgmListIntegrator Grate Again! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 老的数据接口不能用了, 将新接口的数据转换成老接口的数据( --- prevails/bgmlist_integrator.user.js | 166 +++++++++++++++++++--------- 1 file changed, 116 insertions(+), 50 deletions(-) diff --git a/prevails/bgmlist_integrator.user.js b/prevails/bgmlist_integrator.user.js index 1d0e1cc7..0515e49f 100644 --- a/prevails/bgmlist_integrator.user.js +++ b/prevails/bgmlist_integrator.user.js @@ -26,6 +26,7 @@ const addOnSources = { //////////////////////////////////////////////////////////////// }; +const DEBUG = false; const TIME_ZONE = 'CN'; // valid value: 'CN', 'JP' @@ -86,7 +87,7 @@ class Bangumi { const $re = $(this.a).clone(); $re.find('img').removeAttr('class'); $re.find('span').remove(); - $re.attr('title', this.bgm.titleCN + '\n'+ this.bgm.titleJP + '\n播放时间:\n' + this.getTime()); + $re.attr('title', this.bgm.titleCN + '\n' + this.bgm.titleJP + '\n播放时间:\n' + this.getTime()); $re.attr('alt', this.bgm.titleCN + '
' + this.bgm.titleJP); $re.data('onAirSite', this.bgm.onAirSite); return $re; @@ -114,7 +115,7 @@ class Bangumi { } const myBangumis = $('#prgSubjectList > [subject_type=2] > .thumbTip') - .toArray().map(i => new Bangumi(i.getAttribute('subject_id'), i)).filter(i => i.bgm); + .toArray().map(i => new Bangumi(i.getAttribute('subject_id'), i)).filter(i => i.bgm); $('.tooltip').hide(); $('.week:eq(1)').remove(); @@ -137,7 +138,7 @@ $week.each(function () { $div.html(''); const weekDay = WEEK_DAY.indexOf(this.classList[2]); //
  • myBangumis.filter(i => i.bgm['weekDay' + TIME_ZONE] === weekDay && i.isInRange(lastWeekRange)) - .forEach(i => $div.append(i.get$Html())); + .forEach(i => $div.append(i.get$Html())); }); function rmTbWindow() { @@ -184,64 +185,129 @@ function getLast(obj) { return obj[last]; } -function createIndexOnBgmId(bgmlistOriginJson) { - const origin = JSON.parse(bgmlistOriginJson); - const bgmlist = {}; - for (let i in origin) { - bgmlist[origin[i].bgmId] = origin[i]; - } - return bgmlist; +const LANG_TO_REGIONS = { + 'ja': ['JP'], + 'zh-Hans': ['CN'], + 'zh-Hant': ['TW', 'MO', 'HK'], + 'en': [], } -function update({path, version}) { - GM_xmlhttpRequest({ - method: 'GET', - url: path, - data: {"__t": Date.now()}, - onload: function(response) { - if (response.status === 200) { - GM_setValue('bgmlist', createIndexOnBgmId(response.responseText)); - GM_setValue('path', path); - GM_setValue('version', version); - showTbWindow('bgmlist 数据更新成功! 请刷新页面
    ', - 'left:80%;top:20px;width:18%;'); - setTimeout(rmTbWindow, 5000); - } else { - showTbWindow(`Error, status code: ${response.status}
    `, - 'left:80%;top:20px;width:18%;'); - setTimeout(rmTbWindow, 5000); +async function update({ path, version }) { + const items = (await Promise.all(path.split(',').map(path => request(path)))).reduce((r, it) => r.concat(it.items), []); + + const siteInfoMap = await request('https://bgmlist.com/api/v1/bangumi/site'); + // 不需要bangumi的站点信息, 删掉它 + delete siteInfoMap.bangumi; + + const bgmlist = {}; + for (let item of items) { + for (const site of item.sites) { + if (site.site == 'bangumi') { + const titleTranslate = { + [item.lang]: [item.title], + ...item.titleTranslate, + } + const allSites = [ + ...item.sites + .filter(it => it.site !== 'bangumi') + .map((site) => ({ + site: site.site, + id: site.id, + url: site.url, + begin: site.begin, + end: site.end ?? '', + broadcast: site.broadcast, + regions: site.regions ?? siteInfoMap[site.site]?.regions ?? [], + })), + { + site: 'origin', + id: undefined, + url: undefined, + begin: item.begin, + end: item.end ?? '', + broadcast: item.broadcast, + // 为空的regions是特殊值, 表示该site支持所有区域 + regions: LANG_TO_REGIONS[item.lang] ?? [], + }, + ] + const cnSites = allSites.filter(it => it.regions.length == 0 || it.regions.some(r => ['CN', 'TW', 'MO', 'HK'].includes(r))); + const jpSites = allSites.filter(it => it.regions.length == 0 || it.regions.includes('JP')); + const getBeginDate = (sites) => sites.filter(it => it.broadcast || it.begin).map(it => new Date(it.broadcast?.split('/')[1] ?? it.begin)).sort((a, b) => a - b)[0]; + const cnDate = getBeginDate(cnSites); + const jpDate = getBeginDate(jpSites); + const getWeek = (date) => WEEK_DAY.indexOf(date.toDateString().substr(0, 3)) + const getTime = (date) => `${date.getHours().toString().padStart(2, '0')}${date.getMinutes().toString().padStart(2, '0')}`; + + // 将新的播放信息转换成旧的播放信息... + // 参考: https://github.com/bangumi-data/bangumi-data/blob/master/CONTRIBUTING.md/#%E7%95%AA%E7%BB%84%E6%95%B0%E6%8D%AE + bgmlist[site.id] = { + _source: DEBUG ? item : undefined, + titleCN: [...(titleTranslate['zh-Hans'] ?? []), ...(titleTranslate['zh-Hant'] ?? [])]?.join('/') ?? '', + titleJP: titleTranslate['ja']?.join('/') ?? '', + titleEn: titleTranslate['en']?.join('/') ?? '', + weekDayJP: getWeek(jpDate), + weekDayCN: getWeek(cnDate ?? jpDate), + timeJP: getTime(jpDate), + timeCN: cnDate != null ? getTime(cnDate) : '', + onAirSite: item.sites.map((site) => site.url ?? siteInfoMap[site.site]?.urlTemplate.replace('{{id}}', site.id)).filter(it => it), + officalSite: item.officalSite, + bgmId: +site.id, + showDate: item.begin, + endDate: item.end, + newBgm: false, + }; + break; } } - }); + } + GM_setValue('bgmlist', bgmlist); + GM_setValue('path', path); + GM_setValue('version', version); + + showTbWindow('bgmlist 数据更新成功! 请刷新页面
    ', + 'left:80%;top:20px;width:18%;'); + setTimeout(rmTbWindow, 5000); } function checkUpdate() { const lastCheckUpdate = GM_getValue('lastCheckUpdate') || 0; - if (new Date().getTime() - lastCheckUpdate < CHECK_UPDATE_INTERVAL) { + if (new Date().getTime() - lastCheckUpdate < CHECK_UPDATE_INTERVAL && !DEBUG) { return; } - GM_xmlhttpRequest({ - method: 'GET', - url: 'https://bgmlist.com/tempapi/archive.json', - data: {"__t": Date.now()}, - onload: function (response) { - if (response.status === 200) { - const archive = JSON.parse(response.responseText); - const data = archive.data; - const last = getLast(getLast(data)); - const oldPath = GM_getValue('path'); - const oldVersion = GM_getValue('version'); - if (!oldPath || !oldVersion || last.path > oldPath || last.version > oldVersion) { - update(last); - } - GM_setValue('lastCheckUpdate', new Date().getTime()); - } else { - showTbWindow(`Error, status code: ${response.status}
    `, - 'left:80%;top:20px;width:18%;'); - setTimeout(rmTbWindow, 5000); + request('https://bgmlist.com/api/v1/bangumi/season/?start=2020q1') + .then((archive) => { + const version = archive.version + + // 拉取最近两年的数据 + const path = archive.items.slice(-8).map(it => `https://bgmlist.com/api/v1/bangumi/archive/${it}`).join(','); + const oldPath = GM_getValue('path'); + const oldVersion = GM_getValue('version'); + if (!oldPath || !oldVersion || path != oldPath || version != oldVersion || DEBUG) { + update({ path: path, version: version }); } - } - }); + GM_setValue('lastCheckUpdate', new Date().getTime()); + }) } setTimeout(checkUpdate, 500); + +function request(url, { showError = true } = {}) { + return new Promise((resolve, reject) => { + GM_xmlhttpRequest({ + method: 'GET', + url: url, + onload: function (response) { + if (response.status === 200) { + resolve(JSON.parse(response.responseText)); + } else { + if (showError) { + showTbWindow(`Error, status code: ${response.status}
    `, + 'left:80%;top:20px;width:18%;'); + setTimeout(rmTbWindow, 5000); + } + reject(response); + } + } + }); + }); +} From 92b348c141d75d91a1feb3464bb3b657900d185f Mon Sep 17 00:00:00 2001 From: ipcjs Date: Sat, 9 Apr 2022 06:53:17 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=A7=84=E8=8C=83=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prevails/bgmlist_integrator.user.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/prevails/bgmlist_integrator.user.js b/prevails/bgmlist_integrator.user.js index 0515e49f..c2d4823c 100644 --- a/prevails/bgmlist_integrator.user.js +++ b/prevails/bgmlist_integrator.user.js @@ -192,8 +192,8 @@ const LANG_TO_REGIONS = { 'en': [], } -async function update({ path, version }) { - const items = (await Promise.all(path.split(',').map(path => request(path)))).reduce((r, it) => r.concat(it.items), []); +async function update({ paths, version }) { + const items = (await Promise.all(paths.split(',').map(path => request(path)))).reduce((r, it) => r.concat(it.items), []); const siteInfoMap = await request('https://bgmlist.com/api/v1/bangumi/site'); // 不需要bangumi的站点信息, 删掉它 @@ -261,7 +261,7 @@ async function update({ path, version }) { } } GM_setValue('bgmlist', bgmlist); - GM_setValue('path', path); + GM_setValue('paths', paths); GM_setValue('version', version); showTbWindow('bgmlist 数据更新成功! 请刷新页面
    ', @@ -279,11 +279,11 @@ function checkUpdate() { const version = archive.version // 拉取最近两年的数据 - const path = archive.items.slice(-8).map(it => `https://bgmlist.com/api/v1/bangumi/archive/${it}`).join(','); - const oldPath = GM_getValue('path'); + const paths = archive.items.slice(-8).map(it => `https://bgmlist.com/api/v1/bangumi/archive/${it}`).join(','); + const oldPaths = GM_getValue('paths'); const oldVersion = GM_getValue('version'); - if (!oldPath || !oldVersion || path != oldPath || version != oldVersion || DEBUG) { - update({ path: path, version: version }); + if (!oldPaths || !oldVersion || paths != oldPaths || version != oldVersion || DEBUG) { + update({ paths: paths, version: version }); } GM_setValue('lastCheckUpdate', new Date().getTime()); }) From d0a8a8c14efbe4c0e537d32cf2eb3339cda8dca1 Mon Sep 17 00:00:00 2001 From: ipcjs Date: Sat, 6 Sep 2025 14:31:56 +0800 Subject: [PATCH 3/3] fix(BI): fix style error --- prevails/bgmlist_integrator.user.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/prevails/bgmlist_integrator.user.js b/prevails/bgmlist_integrator.user.js index c2d4823c..05c3d3ee 100644 --- a/prevails/bgmlist_integrator.user.js +++ b/prevails/bgmlist_integrator.user.js @@ -27,6 +27,7 @@ const addOnSources = { }; const DEBUG = false; +const CACHE_VERSION = 1; const TIME_ZONE = 'CN'; // valid value: 'CN', 'JP' @@ -66,7 +67,7 @@ class Bangumi { this.bgm = bgmlist[this.id]; this.a = a; if (addOnSources && addOnSources[this.id]) { - this.bgm.onAirSite = addOnSources[this.id].concat(this.bgm.onAirSite); + this.bgm.onAirSite = addOnSources[this.id].map(url => ({ title: url.replace(/https?:\/\/.+?\./, '').split('/')[0], url: url })).concat(this.bgm.onAirSite); } } getTime() { @@ -85,7 +86,7 @@ class Bangumi { } get$Html() { const $re = $(this.a).clone(); - $re.find('img').removeAttr('class'); + $re.find('img').removeAttr('class').attr('width', 48); $re.find('span').remove(); $re.attr('title', this.bgm.titleCN + '\n' + this.bgm.titleJP + '\n播放时间:\n' + this.getTime()); $re.attr('alt', this.bgm.titleCN + '
    ' + this.bgm.titleJP); @@ -166,14 +167,14 @@ $week.find('.thumbTip').click(function () { `, style); return false; }); -GM_addStyle('#TB_window.userscript_bgmlist_integrator{display:block;width:' + TB_WINDOW_WIDTH + 'px;}'); +GM_addStyle('#TB_window.userscript_bgmlist_integrator{display:block;width:' + TB_WINDOW_WIDTH + 'px;padding:8px;}'); const CHECK_UPDATE_INTERVAL = 1000 * 60 * 60 * 8; // 8h @@ -249,7 +250,10 @@ async function update({ paths, version }) { weekDayCN: getWeek(cnDate ?? jpDate), timeJP: getTime(jpDate), timeCN: cnDate != null ? getTime(cnDate) : '', - onAirSite: item.sites.map((site) => site.url ?? siteInfoMap[site.site]?.urlTemplate.replace('{{id}}', site.id)).filter(it => it), + onAirSite: item.sites.map((site) => ({ + title: siteInfoMap[site.site]?.title ?? site.site, + url: site.url ?? siteInfoMap[site.site]?.urlTemplate.replace('{{id}}', site.id) + })).filter(it => it.url), officalSite: item.officalSite, bgmId: +site.id, showDate: item.begin, @@ -263,6 +267,7 @@ async function update({ paths, version }) { GM_setValue('bgmlist', bgmlist); GM_setValue('paths', paths); GM_setValue('version', version); + GM_setValue('cacheVersion', CACHE_VERSION) showTbWindow('bgmlist 数据更新成功! 请刷新页面
    ', 'left:80%;top:20px;width:18%;'); @@ -270,8 +275,9 @@ async function update({ paths, version }) { } function checkUpdate() { + const forceUpdate = (GM_getValue('cacheVersion') || 0) !== CACHE_VERSION; const lastCheckUpdate = GM_getValue('lastCheckUpdate') || 0; - if (new Date().getTime() - lastCheckUpdate < CHECK_UPDATE_INTERVAL && !DEBUG) { + if (new Date().getTime() - lastCheckUpdate < CHECK_UPDATE_INTERVAL && !forceUpdate && !DEBUG) { return; } request('https://bgmlist.com/api/v1/bangumi/season/?start=2020q1') @@ -282,7 +288,7 @@ function checkUpdate() { const paths = archive.items.slice(-8).map(it => `https://bgmlist.com/api/v1/bangumi/archive/${it}`).join(','); const oldPaths = GM_getValue('paths'); const oldVersion = GM_getValue('version'); - if (!oldPaths || !oldVersion || paths != oldPaths || version != oldVersion || DEBUG) { + if (!oldPaths || !oldVersion || paths != oldPaths || version != oldVersion || forceUpdate || DEBUG) { update({ paths: paths, version: version }); } GM_setValue('lastCheckUpdate', new Date().getTime());