diff --git a/packages/unblock-area-limit/src/api/bilibili.ts b/packages/unblock-area-limit/src/api/bilibili.ts index 774bbbb5..6f53e442 100644 --- a/packages/unblock-area-limit/src/api/bilibili.ts +++ b/packages/unblock-area-limit/src/api/bilibili.ts @@ -1,6 +1,7 @@ import { Async } from "../util/async" import { generateMobiPlayUrlParams } from "./biliplus" import { Converters } from "../util/converters" +import { getSsId } from "../util/IndexedDB" interface SeasonInfo { code: number @@ -627,7 +628,14 @@ export class BiliBiliApi { getEpisodeInfoByEpId(ep_id: string) { return Async.ajax('//api.bilibili.com/pgc/season/episode/web/info?' + `ep_id=${ep_id}`) } - getSeasonInfoByEpSsIdOnThailand(ep_id: string, season_id: string) { + async getSeasonInfoByEpSsIdOnThailand(ep_id: string, season_id: string) { + if (ep_id != '') { + const ssid = await getSsId(parseInt(ep_id)) + if (ssid != '') { + season_id = ssid + ep_id = '' + } + } const params = '?' + (ep_id != '' ? `ep_id=${ep_id}` : `season_id=${season_id}`) + `&mobi_app=bstar_a&s_locale=zh_SG` const newParams = generateMobiPlayUrlParams(params, 'th') return Async.ajax(`${this.server}/intl/gateway/v2/ogv/view/app/season?` + newParams) diff --git a/packages/unblock-area-limit/src/feature/bili/area_limit_for_vue.ts b/packages/unblock-area-limit/src/feature/bili/area_limit_for_vue.ts index 0ae6a860..35cb5dec 100644 --- a/packages/unblock-area-limit/src/feature/bili/area_limit_for_vue.ts +++ b/packages/unblock-area-limit/src/feature/bili/area_limit_for_vue.ts @@ -1,5 +1,6 @@ import { AppSeasonInfo, BiliBiliApi } from "../../api/bilibili" import { BiliPlusApi } from "../../api/biliplus" +import { getObjectStore, openDb } from "../../util/IndexedDB" import { Converters } from "../../util/converters" import { cookieStorage } from "../../util/cookie" import { util_init } from "../../util/initiator" @@ -110,7 +111,6 @@ interface TemplateArgs { appOnly: boolean, } - async function fixThailandSeason(ep_id: string, season_id: string) { // 部分泰区番剧通过 bangumi 无法取得数据或者数据不完整 // 通过泰区 api 补全 @@ -128,12 +128,19 @@ async function fixThailandSeason(ep_id: string, season_id: string) { origin.result.episodes = [] if (origin.result.modules.length > 0) { + var store = getObjectStore(await openDb(), 'ep_id_season_id', 'readwrite') origin.result.modules[0].data.episodes.forEach((ep) => { ep.episode_status = ep.status ep.ep_id = ep.id ep.index = ep.title ep.index_title = ep.long_title origin.result.episodes?.push(ep) + if (season_id !== '5551') + try { + store.put({ ep_id: ep.id, season_id: season_id }) + } catch (e) { + log('addSsEpId error', e) + } }) origin.result.total = origin.result.modules[0].data.episodes.length } diff --git a/packages/unblock-area-limit/src/util/IndexedDB.ts b/packages/unblock-area-limit/src/util/IndexedDB.ts new file mode 100644 index 00000000..e468563b --- /dev/null +++ b/packages/unblock-area-limit/src/util/IndexedDB.ts @@ -0,0 +1,58 @@ +const DB_NAME = 'balh'; +const DB_VERSION = 1; + +export function openDb() { + return new Promise((resolve, reject) => { + var req = indexedDB.open(DB_NAME, DB_VERSION); + req.onsuccess = function (evt) { + resolve(this.result) + } + req.onerror = function (evt) { + reject(evt) + } + + req.onupgradeneeded = (evt: IDBVersionChangeEvent) => { + var storeEPIDCache = (evt.currentTarget as IDBOpenDBRequest)?.result.createObjectStore( + 'ep_id_season_id', { keyPath: 'ep_id' }) + storeEPIDCache.createIndex('season_id', 'season_id', { unique: false }) + } + }) +} + +export function getObjectStore(db: IDBDatabase, store_name: string, mode: IDBTransactionMode) { + var tx = db.transaction(store_name, mode); + return tx.objectStore(store_name); +} + +async function clearObjectStore(store_name: string) { + var store = getObjectStore(await openDb(), store_name, 'readwrite'); + var req = store.clear() + req.onerror = function (evt) { + console.error("clearObjectStore:", evt); + } +} + +export function getBlob(key: any, store: IDBObjectStore, success_callback: (blob: Blob) => void) { + var req: IDBRequest = store.get(key) + req.onsuccess = async function (evt) { + var value = (evt.target as IDBRequest)?.result + if (value) + success_callback(value) + } +} + +export function getSsId(ep_id: number): Promise { + return new Promise(async (resolve, reject) => { + var store = getObjectStore(await openDb(), 'ep_id_season_id', 'readonly'); + var req: IDBRequest = store.get(ep_id) + req.onsuccess = () => { + if (!req.result) + resolve('') + else + resolve(req.result.season_id) + } + req.onerror = (e) => { + reject(e) + } + }) +}