Skip to content

Commit

Permalink
perf(balh): [workaround] 泰区番剧信息优先使用 season_id 获取 (#1298)
Browse files Browse the repository at this point in the history
* perf(balh): [workaround] 泰区番剧信息优先使用 season_id 获取

pchpub/BiliRoaming-Rust-Server#122

* 保存 DB 实例

* wip: db变量改成Promise, 保证只打开一次

* wip: IndexedDb->BalhDb

---------

Co-authored-by: ipcjs <gipcjs@gmail.com>
  • Loading branch information
Howard20181 and ipcjs authored Jan 6, 2024
1 parent 2bc87a7 commit 5692a08
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
10 changes: 9 additions & 1 deletion packages/unblock-area-limit/src/api/bilibili.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Async, Promise } from "../util/async"
import { generateMobiPlayUrlParams } from "./biliplus"
import { Converters } from "../util/converters"
import { BalhDb } from "../util/balh-db"

interface SeasonInfo {
code: number
Expand Down Expand Up @@ -787,7 +788,14 @@ export class BiliBiliApi {
getEpisodeInfoByEpId(ep_id: string) {
return Async.ajax<EpisodeInfo>('//api.bilibili.com/pgc/season/episode/web/info?' + `ep_id=${ep_id}`)
}
getSeasonInfoByEpSsIdOnThailand(ep_id: string | undefined, season_id: string | undefined) {
async getSeasonInfoByEpSsIdOnThailand(ep_id: string | undefined, season_id: string | undefined) {
if (ep_id) {
const ssid = await BalhDb.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<SeasonInfoOnThailand>(`${this.server}/intl/gateway/v2/ogv/view/app/season?` + newParams)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BiliBiliApi } from "../../api/bilibili"
import { BiliPlusApi } from "../../api/biliplus"
import { BalhDb } from "../../util/balh-db"
import { Converters } from "../../util/converters"
import { cookieStorage } from "../../util/cookie"
import { util_init } from "../../util/initiator"
Expand Down Expand Up @@ -111,7 +112,6 @@ interface TemplateArgs {
appOnly: boolean,
}


async function fixThailandSeason(ep_id: string, season_id: string) {
// 部分泰区番剧通过 bangumi 无法取得数据或者数据不完整
// 通过泰区 api 补全
Expand All @@ -136,6 +136,9 @@ async function fixThailandSeason(ep_id: string, season_id: string) {
ep.index = ep.title
ep.index_title = ep.long_title
origin.result.episodes?.push(ep)
if (season_id !== '5551')
BalhDb.setSsId(ep.id, season_id)//
.catch((e) => util_warn('setSsId failed', e))
})
origin.result.total = origin.result.modules[0].data.episodes.length
}
Expand Down
54 changes: 54 additions & 0 deletions packages/unblock-area-limit/src/util/balh-db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const DB_NAME = 'balh';
const DB_VERSION = 1;

let dbPromise: Promise<IDBDatabase> | undefined
export function openDb() {
return new Promise<IDBDatabase>((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 })
}
})
}

function getDb() {
return dbPromise ??= openDb()
}

async function getObjectStore(store_name: string, mode: IDBTransactionMode) {
const db = await getDb()
var tx = db.transaction(store_name, mode);
return tx.objectStore(store_name);
}

export namespace BalhDb {
export async function setSsId(ep_id: number, season_id: string) {
var store = await getObjectStore('ep_id_season_id', 'readwrite')
store.put({ ep_id: ep_id, season_id: season_id })
}

export function getSsId(ep_id: number): Promise<string> {
return new Promise(async (resolve, reject) => {
var store = await getObjectStore('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)
}
})
}
}

0 comments on commit 5692a08

Please sign in to comment.