Skip to content

Commit

Permalink
perf(balh): [workaround] 泰区番剧信息优先使用 season_id 获取
Browse files Browse the repository at this point in the history
  • Loading branch information
Howard20181 committed Jan 2, 2024
1 parent 815e83c commit a3c4ae9
Show file tree
Hide file tree
Showing 3 changed files with 75 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 } from "../util/async"
import { generateMobiPlayUrlParams } from "./biliplus"
import { Converters } from "../util/converters"
import { getSsId } from "../util/IndexedDB"

interface SeasonInfo {
code: number
Expand Down Expand Up @@ -627,7 +628,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, 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<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 { 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"
Expand Down Expand Up @@ -110,7 +111,6 @@ interface TemplateArgs {
appOnly: boolean,
}


async function fixThailandSeason(ep_id: string, season_id: string) {
// 部分泰区番剧通过 bangumi 无法取得数据或者数据不完整
// 通过泰区 api 补全
Expand All @@ -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
}
Expand Down
58 changes: 58 additions & 0 deletions packages/unblock-area-limit/src/util/IndexedDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const DB_NAME = 'balh';
const DB_VERSION = 1;

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 })
}
})
}

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<string> {
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)
}
})
}

0 comments on commit a3c4ae9

Please sign in to comment.