Skip to content

Commit

Permalink
fix: bili live api
Browse files Browse the repository at this point in the history
  • Loading branch information
Innei committed Jul 22, 2023
1 parent d14362d commit f74ce26
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/app/api/bilibili/check_live/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import axios from 'axios'
import type { NextRequest } from 'next/server'
import type { BLUser } from './types/user'

import { NextServerResponse } from '~/lib/edge-function.server'
import { getQueryClient } from '~/lib/query-client.server'

const headers = {
referer: `https://link.bilibili.com/p/center/index?visit_id=22ast2mb9zhc`,
referer: `https://live.bilibili.com/`,
'User-Agent': `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Shiro`,
}

const requestHeader = new Headers()

for (const [key, value] of Object.entries(headers)) {
requestHeader.set(key, value)
}
export const runtime = 'edge'

export const revalidate = 10

export const GET = async (req: NextRequest) => {
const liveId = req.nextUrl.searchParams.get('liveId')
if (!liveId) {
Expand All @@ -19,39 +27,39 @@ export const GET = async (req: NextRequest) => {
const res = await queryClient.fetchQuery({
queryKey: ['bilibili-live', liveId],
queryFn: async () => {
return axios
.get(
`https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id=${liveId}&protocol=0,1&format=0,1,2&codec=0,1&qn=0&platform=web&ptype=8&dolby=5`,
{
headers,
},
)
.then((res) => res.data)
return fetch(
`https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id=${liveId}&protocol=0,1&format=0,1,2&codec=0,1&qn=0&platform=web&ptype=8&dolby=5`,
{
headers: requestHeader,
},
)
.then((res) => res.json())
.catch(() => null)
},
})

const response = new NextServerResponse()

if (!res?.data) {
return response.end()
}

if (!res?.data?.playurl_info) {
return response.end()
}
const userInfo = await axios
.get(
`https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid=${liveId}`,
{
headers,
},
)
const userInfo = await fetch(
`https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid=${liveId}`,
{
headers: requestHeader,
},
)
.then((res) => res.json())
.catch(() => null)

if (!userInfo?.data) {
if (!userInfo) {
return
}

const info = (userInfo.data as BLUser).data.info
const info = (userInfo as BLUser).data.info
return response.json({ ...info })
}

1 comment on commit f74ce26

@vercel
Copy link

@vercel vercel bot commented on f74ce26 Jul 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

shiro – ./

shiro-innei.vercel.app
springtide.vercel.app
shiro-git-main-innei.vercel.app
innei.in

Please sign in to comment.