Skip to content

Commit

Permalink
fix(balh): 修复其他地区无法播放仅限大陆番剧 (#1300)
Browse files Browse the repository at this point in the history
* fix(balh): 修复其他地区无法播放仅限大陆番剧

* wip: 整理代码

---------

Co-authored-by: ipcjs <gipcjs@gmail.com>
  • Loading branch information
Howard20181 and ipcjs authored Jan 3, 2024
1 parent 3eb0ec6 commit b9551bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppSeasonInfo, BiliBiliApi } from "../../api/bilibili"
import { BiliBiliApi } from "../../api/bilibili"
import { BiliPlusApi } from "../../api/biliplus"
import { Converters } from "../../util/converters"
import { cookieStorage } from "../../util/cookie"
Expand Down Expand Up @@ -413,10 +413,10 @@ export function removeEpAreaLimit(ep: StringAnyObject) {
ep.epRights.allow_dm = 1
}
if (ep.rights) {
ep.rights.area_limit = false
ep.rights.area_limit = 0
ep.rights.allow_dm = 1
}
if (ep.badge === '受限') {
if (ep.badge === '受限' || ep.badge_info.text === '受限') {
ep.badge = ''
ep.badge_info = { "bg_color": "#FB7299", "bg_color_night": "#BB5B76", "text": "" }
ep.badge_type = 0
Expand Down Expand Up @@ -505,6 +505,8 @@ export function area_limit_for_vue() {
// data.initEpList.forEach(removeEpAreaLimit)
// data.rights.area_limit = false
// data.rights.allow_dm = 1
} else if (data.seasonInfo.mediaInfo.episodes.length > 0) {
data.seasonInfo.mediaInfo.episodes.forEach(removeEpAreaLimit)
} else if (data.seasonInfo && !data.seasonInfo.mediaInfo.rights.can_watch) {
// 新版没有Playable的是预告 PV,不能直接跳过,can_watch=false 才替换
return;
Expand Down
42 changes: 17 additions & 25 deletions packages/unblock-area-limit/src/feature/bili/area_limit_xhr_.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { Converters, uposMap } from '../../util/converters';
import { _ } from '../../util/react'
import { Async, Promise } from '../../util/async';
import { r, _t } from '../../feature/r'
import { util_error, util_info, util_log, util_warn, util_debug, logHub } from '../../util/log'
import { util_error, util_warn, log } from '../../util/log'
import { cookieStorage } from '../../util/cookie'
import { balh_config, isClosed } from '../../feature/config'
import { Func } from '../../util/utils';
import { util_page } from '../../feature/page'
import { access_key_param_if_exist, platform_android_param_if_app_only } from '../../api/bilibili-utils'
import { BiliPlusApi, generateMobiPlayUrlParams, getMobiPlayUrl, fixMobiPlayUrlJson, fixThailandPlayUrlJson } from '../../api/biliplus'
import { generateMobiPlayUrlParams, getMobiPlayUrl, fixMobiPlayUrlJson, fixThailandPlayUrlJson } from '../../api/biliplus'
import { ui } from '../../util/ui'
import { Strings } from '../../util/strings'
import { util_init } from '../../util/initiator'
Expand Down Expand Up @@ -43,23 +42,23 @@ export const area_limit_xhr = (() => {
/// {@endtemplate}
transformResponse: ({ url, response, xhr, container }) => {
if (url.match(RegExps.url('api.bilibili.com/pgc/view/web/season?'))) {
log('/pgc/view/web/season:', xhr.responseText)
let json = JSON.parse(xhr.responseText)
if (json.code === 0 && json.result) {
// processSeasonInfo(json.result)
json.result.episodes.forEach(removeEpAreaLimit)
json.result.rights.area_limit = false
json.result.rights.area_limit = 0
json.result.rights.ban_area_show = 0
return json
}
} else if (url.match(RegExps.url('bangumi.bilibili.com/view/web_api/season/user/status'))
|| url.match(RegExps.url('api.bilibili.com/pgc/view/web/season/user/status'))) {
log('/season/user/status:', xhr.responseText)
let json = JSON.parse(xhr.responseText)
let rewriteResult = false
if (json.code === 0 && json.result) {
areaLimit(json.result.area_limit !== 0)
if (json.result.area_limit !== 0) {
json.result.area_limit = 0 // 取消区域限制
json.result.ban_area_show = 0
rewriteResult = true
}
if (balh_config.blocked_vip) {
Expand Down Expand Up @@ -211,24 +210,26 @@ export const area_limit_xhr = (() => {
log('/x/player/playurl', 'origin', `block: ${container.__block_response}`, xhr.response)
// todo : 当前只实现了r.const.mode.REPLACE, 需要支持其他模式
// 2018-10-14: 等B站全面启用新版再说(;¬_¬)
} else if (url.match(RegExps.url('api.bilibili.com/pgc/player/web/playurl'))
} else if (url.match(RegExps.url('api.bilibili.com/pgc/player/web/playurl')) || url.match(RegExps.url('api.bilibili.com/pgc/player/web/v2/playurl'))
&& !Strings.getSearchParam(url, 'balh_ajax')) {
log('/pgc/player/web/playurl', 'origin', `block: ${container.__block_response}`, xhr.response)
if (!container.__redirect) { // 请求没有被重定向, 则需要检测结果是否有区域限制
let json = typeof xhr.response === 'object' ? xhr.response : JSON.parse(xhr.responseText)
const reqUrl = new URL(url, document.location.href)
const isV1 = reqUrl.pathname === '/pgc/player/web/playurl'
let json = typeof xhr.response === 'object' ? xhr.response : JSON.parse(xhr.responseText)
if (!container.__redirect || (!isV1 && isAreaLimitForPlayUrl(json.result))) { // 请求没有被重定向, 则需要检测结果是否有区域限制
if (balh_config.blocked_vip || json.code || isAreaLimitForPlayUrl(json.result)) {
areaLimit(true)
// 2022-09-17 ipcjs: 为什么这里用的是请求url, 而不是响应url?...
let requestUrl = container.__url
let requestUrl = isV1 ? container.__url : `//api.bilibili.com/pgc/player/web/playurl${reqUrl.search}`
if (isBangumiPage()) {
requestUrl += `&module=bangumi`
}
return bilibiliApis._playurl.asyncAjax(requestUrl)
.then(data => {
if (!data.code) {
data = { code: 0, result: data, message: "0" }
data = isV1
? { code: 0, result: data, message: "0" }
: { code: 0, message: "success", result: { video_info: data } }
}
log('/pgc/player/web/playurl', 'proxy', data)
return data
})
} else {
Expand Down Expand Up @@ -288,7 +289,6 @@ export const area_limit_xhr = (() => {
} else if (url.match(RegExps.url('api.bilibili.com/pgc/player/web/playurl'))
&& !Strings.getSearchParam(url, 'balh_ajax')
&& needRedirect()) {
log('/pgc/player/web/playurl')
// debugger
container.__redirect = true // 标记该请求被重定向
if (isBangumiPage()) {
Expand All @@ -297,13 +297,8 @@ export const area_limit_xhr = (() => {
return bilibiliApis._playurl.asyncAjax(url)
.then(data => {
if (!data.code) {
data = {
code: 0,
result: data,
message: "0",
}
data = { code: 0, result: data, message: "0" }
}
log('/pgc/player/web/playurl', 'proxy(redirect)', data)
return data
})
}
Expand Down Expand Up @@ -337,7 +332,6 @@ export const area_limit_xhr = (() => {
let oriResultTransformer
let oriResultTransformerWhenProxyError
let one_api;
// log(param)
if (param.url.match(RegExps.urlPath('/web_api/get_source'))) {
one_api = bilibiliApis._get_source;
oriResultTransformer = p => p
Expand Down Expand Up @@ -372,7 +366,6 @@ export const area_limit_xhr = (() => {
param.data = undefined
}
if (isBangumiPage()) {
log(`playurl add 'module=bangumi' param`)
param.url += `&module=bangumi`
}
// 加上这个参数, 防止重复拦截这个url
Expand All @@ -385,7 +378,6 @@ export const area_limit_xhr = (() => {
}
oriResultTransformer = p => p
.then(json => {
log(json)
if (isNewPlayurl && !json.code) {
json = json.result
}
Expand Down Expand Up @@ -536,7 +528,7 @@ export const area_limit_xhr = (() => {
}

function isBangumiPage() {
const mediaInfo = window.__INITIAL_STATE__?.mediaInfo
const mediaInfo = window.__INITIAL_STATE__?.mediaInfo || window.__NEXT_DATA__?.props.pageProps.dehydratedState?.queries[0]?.state.data.seasonInfo?.mediaInfo
return isBangumi(mediaInfo?.season_type || mediaInfo?.ssType)
}

Expand Down Expand Up @@ -606,7 +598,7 @@ export const area_limit_xhr = (() => {
}

function isAreaLimitForPlayUrl(json) {
return (json.errorcid && json.errorcid == '8986943') || (json.durl && json.durl.length === 1 && json.durl[0].length === 15126 && json.durl[0].size === 124627);
return (json.errorcid && json.errorcid == '8986943') || (json.durl && json.durl.length === 1 && json.durl[0].length === 15126 && json.durl[0].size === 124627) || !json.video_info;
}

var bilibiliApis = (function () {
Expand Down

0 comments on commit b9551bb

Please sign in to comment.