Skip to content

Commit

Permalink
0
Browse files Browse the repository at this point in the history
  • Loading branch information
lovetingyuan committed Mar 17, 2024
1 parent 20d0530 commit 185b556
Show file tree
Hide file tree
Showing 23 changed files with 266 additions and 188 deletions.
4 changes: 2 additions & 2 deletions src/api/hot-videos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export type VideoItem = ReturnType<typeof getVideo>

// https://api.bilibili.com/x/web-interface/popular?ps=20&pn=1

export function useHotVideos() {
export function useHotVideos(t: number) {
const { data, mutate, size, setSize, isValidating, isLoading, error } =
useSWRInfinite(
index => {
return `/x/web-interface/popular?ps=30&pn=${index + 1}`
return `/x/web-interface/popular?ps=30&pn=${index + 1}&_t=${t}`
},
// fetcher2,
{
Expand Down
48 changes: 39 additions & 9 deletions src/api/play-url.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import useSWR from 'swr'
import { z } from 'zod'

import request from './fetcher'
import { PlayUrlResponseSchema } from './play-url.schema'

type Res = z.infer<typeof PlayUrlResponseSchema>

export function usePlayUrl(
bvid: string,
cid?: string | number,
highQuality = true,
) {
export function usePlayUrl(bvid: string, cid?: number) {
const search = new URLSearchParams()
// https://socialsisteryi.github.io/bilibili-API-collect/docs/video/videostream_url.html
if (bvid && cid) {
const query = {
bvid,
cid,
type: 'mp4',
qn: highQuality ? 64 : 16,
qn: 64,
fnval: 1,
try_look: 1,
platform: 'pc',
high_quality: highQuality ? 1 : 0,
high_quality: 1,
}
Object.entries(query).forEach(([k, v]) => {
search.append(k, v + '')
Expand All @@ -35,9 +32,40 @@ export function usePlayUrl(
},
)
return data?.durl
? data.durl[0]?.backup_url?.[0] || data.durl[0]?.url || ''
: null
}

export function getDownloadUrl(bvid: string, cid?: number) {
const search = new URLSearchParams()
if (!bvid || !cid) {
return
}
// https://socialsisteryi.github.io/bilibili-API-collect/docs/video/videostream_url.html

const query = {
bvid,
cid,
type: 'mp4',
qn: 64,
fnval: 4048,
platform: 'html5',
high_quality: 1,
try_look: 1,
}
Object.entries(query).forEach(([k, v]) => {
search.append(k, v + '')
})
return request<Res>(`/x/player/wbi/playurl?${search}`).then(res => {
return res.durl?.[0]?.url
})
}

export function useVideoDownloadUrl(bvid: string, cid?: string | number) {
export function useVideoDownloadUrl(
bvid: string,
cid: string | number,
onSuccess: (d?: Res['durl']) => void,
) {
const search = new URLSearchParams()
// https://socialsisteryi.github.io/bilibili-API-collect/docs/video/videostream_url.html
if (bvid && cid) {
Expand All @@ -55,11 +83,13 @@ export function useVideoDownloadUrl(bvid: string, cid?: string | number) {
search.append(k, v + '')
})
}

const { data } = useSWR<Res>(
bvid && cid ? `/x/player/wbi/playurl?${search}` : null,
{
dedupingInterval: 2 * 60 * 1000 * 1000,
onSuccess: d => {
onSuccess?.(d?.durl)
},
},
)
return data?.durl
Expand Down
25 changes: 25 additions & 0 deletions src/api/video-info.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ export const VideoInfoResponseSchema = z.object({
mission_id: z.number(),
no_cache: z.boolean(),
owner: z.object({ mid: z.number(), name: z.string(), face: z.string() }),
rights: z.object({
// bp: z.number(),
// elec: z.number(),
// download: z.number(),
// movie: z.number(),
// pay: z.number(),
// hd5: z.number(),
// no_reprint: z.number(),
// autoplay: z.number(),
// ugc_pay: z.number(),
is_cooperation: z.union([z.literal(0), z.literal(1)]),
// ugc_pay_preview: z.number(),
// no_background: z.number(),
// clean_mode: z.number(),
is_stein_gate: z.union([z.literal(0), z.literal(1)]),
is_360: z.union([z.literal(0), z.literal(1)]),
// no_share: z.number(),
// arc_pay: z.number(),
// free_watch: z.number(),
}),
argue_info: z.object({
argue_msg: z.string(),
argue_type: z.number(),
argue_link: z.string(),
}),
pages: z
.object({
cid: z.number(),
Expand Down
4 changes: 4 additions & 0 deletions src/api/video-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const getVideoInfo = (data: VideoInfoResponse) => {
danmuNum: data.stat.danmaku,
videos: data.videos, // 如果是分片视频或者互动视频,这个videos会是大于1的数字
tag: data.tname,
interactive: data.rights.is_stein_gate === 1,
cooperation: data.rights.is_cooperation === 1,
argument: data.argue_info.argue_msg,
argumentLink: data.argue_info.argue_link,
// location: data.pub_location,
pages: data.pages.map(v => {
// 如果是分片视频,那么length会是分片数量,否则是1
Expand Down
5 changes: 0 additions & 5 deletions src/components/ErrorFallback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ import { Button, Image, Linking, Text, View } from 'react-native'
import { colors } from '@/constants/colors.tw'

import { site } from '../constants'
// import { showFatalError } from '../utils'

export default function ErrorFallback(props: { message?: string }) {
// React.useEffect(() => {
// showFatalError(props)
// }, [props])

if (__DEV__) {
// eslint-disable-next-line no-console
console.error(props.message)
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useRNETheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default function useRNETheme() {
useTWC(colors.white.text).color,
useTWC(colors.black.text).color,
]
// console.log(white, black)
const isDark = useIsDark()
const getRNETheme = React.useCallback(() => {
return createTheme({
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useRouteTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default function useRouteTheme() {
const [routeTheme, setRouteTheme] = React.useState(getRouteTheme)

useAppStateChange(() => {
// console.log('change route color mode')
setRouteTheme(getRouteTheme())
})
React.useEffect(() => {
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/useUpdateNavigationOptions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useNavigation } from '@react-navigation/native'
import { NativeStackNavigationOptions } from '@react-navigation/native-stack'
import React from 'react'

import type { NavigationProps } from '@/types'

export default function useUpdateNavigationOptions(
options: Partial<NativeStackNavigationOptions>,
) {
const navigation = useNavigation<NavigationProps['navigation']>()
React.useEffect(() => {
navigation.setOptions(options)
}, [navigation, options])
}
22 changes: 12 additions & 10 deletions src/routes/Collect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { NativeStackScreenProps } from '@react-navigation/native-stack'
import { Text } from '@rneui/themed'
import { FlashList } from '@shopify/flash-list'
import React from 'react'
import { Alert, Linking } from 'react-native'

import VideoListItem from '@/components/VideoItem'
import { colors } from '@/constants/colors.tw'
import useUpdateNavigationOptions from '@/hooks/useUpdateNavigationOptions'
import { useStore } from '@/store'
import { CollectVideoInfo, RootStackParamList } from '@/types'
import { CollectVideoInfo } from '@/types'

type Props = NativeStackScreenProps<RootStackParamList, 'Collect'>

function CollectList(props: Props) {
function CollectList() {
const { $collectedVideos, set$collectedVideos } = useStore()
React.useEffect(() => {
props.navigation.setOptions({
headerTitle: `我的收藏(${$collectedVideos.length})`,
})
}, [props.navigation, $collectedVideos.length])
const headerTitle = `我的收藏(${$collectedVideos.length})`
useUpdateNavigationOptions(
React.useMemo(() => {
return {
headerTitle,
}
}, [headerTitle]),
)

const buttons = (video: CollectVideoInfo) => {
return [
{
Expand Down
17 changes: 10 additions & 7 deletions src/routes/Dynamic/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React from 'react'
import { Image, View } from 'react-native'

import { colors } from '@/constants/colors.tw'
import useUpdateNavigationOptions from '@/hooks/useUpdateNavigationOptions'

import {
type DynamicItemAllType,
Expand Down Expand Up @@ -76,7 +77,7 @@ const Loading = React.memo(LoadingComp)

export default React.memo(Dynamic)

function Dynamic({ navigation, route }: Props) {
function Dynamic({ route }: Props) {
const upId = route.params?.user?.mid // || specialUser?.mid
const dynamicListRef = React.useRef<any>(null)
const { data: userInfo } = useUserInfo(upId)
Expand Down Expand Up @@ -117,13 +118,15 @@ function Dynamic({ navigation, route }: Props) {
/>
)
}, [])
useUpdateNavigationOptions(
React.useMemo(() => {
return {
headerTitle,
headerRight,
}
}, [headerTitle]),
)

React.useEffect(() => {
navigation.setOptions({
headerTitle,
headerRight,
})
}, [navigation, headerTitle])
const renderItem = ({ item }: { item: DynamicItemAllType }) => {
return <DynamicItem item={item} />
}
Expand Down
35 changes: 18 additions & 17 deletions src/routes/Follow/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { NativeStackScreenProps } from '@react-navigation/native-stack'
import { Text } from '@rneui/themed'
import React from 'react'
import {
Expand All @@ -9,10 +8,12 @@ import {
View,
} from 'react-native'

import useUpdateNavigationOptions from '@/hooks/useUpdateNavigationOptions'

import useIsDark from '../../hooks/useIsDark'
import useMounted from '../../hooks/useMounted'
import { useStore } from '../../store'
import type { RootStackParamList, UpInfo } from '../../types'
import type { UpInfo } from '../../types'
import FollowItem from './FollowItem'

const tvL = require('../../../assets/tv-l.png')
Expand All @@ -39,9 +40,7 @@ function TvImg() {

export default React.memo(FollowList)

type Props = NativeStackScreenProps<RootStackParamList, 'Follow'>

function FollowList(props: Props) {
function FollowList() {
// eslint-disable-next-line no-console
__DEV__ && console.log('Follow page')
const { $followedUps, _updatedCount, $upUpdateMap, livingUps } = useStore()
Expand All @@ -51,18 +50,20 @@ function FollowList(props: Props) {
const { width } = useWindowDimensions()
const columns = Math.floor(width / 90)
const count = $followedUps.length

React.useEffect(() => {
props.navigation.setOptions({
headerTitle:
'关注的UP' +
(count
? _updatedCount
? ` (${_updatedCount}/${count})`
: ` (${count})`
: ''),
})
}, [props.navigation, count, _updatedCount])
const headerTitle =
'关注的UP' +
(count
? _updatedCount
? ` (${_updatedCount}/${count})`
: ` (${count})`
: '')
useUpdateNavigationOptions(
React.useMemo(() => {
return {
headerTitle,
}
}, [headerTitle]),
)

const renderItem = React.useCallback(
({
Expand Down
23 changes: 12 additions & 11 deletions src/routes/History/index.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { NativeStackScreenProps } from '@react-navigation/native-stack'
import { Text } from '@rneui/themed'
import { FlashList } from '@shopify/flash-list'
import React from 'react'
import { Linking } from 'react-native'

import VideoListItem from '@/components/VideoItem'
import { colors } from '@/constants/colors.tw'
import useUpdateNavigationOptions from '@/hooks/useUpdateNavigationOptions'
import { useStore } from '@/store'
import { CollectVideoInfo, HistoryVideoInfo, RootStackParamList } from '@/types'
import { CollectVideoInfo, HistoryVideoInfo } from '@/types'

type Props = NativeStackScreenProps<RootStackParamList, 'History'>

function HistoryList(props: Props) {
function HistoryList() {
const { $watchedVideos } = useStore()
React.useEffect(() => {
const count = Object.keys($watchedVideos).length
props.navigation.setOptions({
headerTitle: `观看历史(${count})`,
})
}, [props.navigation, $watchedVideos])
const headerTitle = `观看历史(${Object.keys($watchedVideos).length})`
useUpdateNavigationOptions(
React.useMemo(() => {
return {
headerTitle,
}
}, [headerTitle]),
)

const buttons = (video: CollectVideoInfo) => {
return [
{
Expand Down
Loading

0 comments on commit 185b556

Please sign in to comment.