Skip to content

Commit

Permalink
fix: 修复正则表达式和path-to-regexp解析抖音url错误的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
duan602728596 committed Oct 16, 2024
1 parent b125bcc commit 10a5b9a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/48tools/src/pages/Toutiao/Douyin/Douyin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ function Douyin(props: {}): ReactElement {
filePath,
durl: uri,
qid: item.qid,
headers: {},
headers: {
Referer: 'https://www.douyin.com/'
},
resStatus302: true
});
});
Expand Down
27 changes: 20 additions & 7 deletions packages/48tools/src/pages/Toutiao/Douyin/function/parser.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { match, type Match, type MatchFunction } from 'path-to-regexp';
import { requestDouyinUrl, type DouyinHtmlResponseType } from '@48tools-api/toutiao/douyin';

type VideoParam = Partial<{ videoId: string }>;
type VideoParamType = 'video' | 'note';
type VideoParam = Partial<{ videoId: string; videoType: VideoParamType }>;
type UserParam = Partial<{ userId: string }>;

const vdouinRegexp: RegExp = /v\.douyin\.com/i; // 抖音分享短链接
const iesdouyinRegexp: RegExp = /www.iesdouyin.com/i; // 抖音分享长链接
const iesdouyinhrefHtmlRegexp: RegExp = /<a href="https?:\/\/www\.iesdouyin\.com\//i; // 是一个链接
const shareVideo: RegExp = /share\/\\(video|note\\)/i; // 分享视频
const shareVideo: RegExp = /share\/(video|note)/i; // 分享视频
const douyinRegexp: RegExp = /www\.douyin\.com/i; // 抖音域名
const douyinVideoRegexp: RegExp = /\/\\(video|note\\)\/[0-9]+/i; // 抖音视频
const douyinVideoRegexp: RegExp = /\/(video|note)\/[0-9]+/i; // 抖音视频
const videoIdRegexp: RegExp = /^[0-9]+$/; // 视频id
const douyinUserRegexp: RegExp = /\/user\//i; // 抖音用户
const douyinVideoUrlMatch: MatchFunction<VideoParam> = match('/\\(video|note\\)/:videoId');
const douyinVideoUrlMatch: MatchFunction<VideoParam> = match('/:videoType/:videoId');
const douyinUserUrlMatch: MatchFunction<UserParam> = match('/user/:userId');
const douyinShareVideoUrlMatch: MatchFunction<VideoParam> = match('/share/\\(video|note\\)/:videoId');
const douyinShareVideoUrlMatch: MatchFunction<VideoParam> = match('/share/:videoType/:videoId');
const douyinShareUserUrlMatch: MatchFunction<UserParam> = match('/share/user/:userId');

export enum DouyinUrlType {
Expand All @@ -27,6 +28,18 @@ export interface ParseResult {
id: string;
}

/**
* 判断字符串是否是video或note
* @param { VideoParamType | undefined } t
*/
function isVideoParamType(t: VideoParamType | undefined): t is VideoParamType {
if (!t) return false;

const tLow: string = t.toLowerCase();

return tLow === 'video' || tLow === 'note';
}

/**
* URL解析地址
* @param { URL } urlParse - 地址的解析
Expand All @@ -53,7 +66,7 @@ async function douyinUrlParse(urlParse: URL, url: string, cookie: string | undef
if (shareVideo.test(href)) {
const matchResult: Match<VideoParam> = douyinShareVideoUrlMatch(new URL(href).pathname);

if (typeof matchResult === 'object') {
if (typeof matchResult === 'object' && isVideoParamType(matchResult.params.videoType)) {
type = DouyinUrlType.Video;
id = matchResult.params.videoId;
}
Expand All @@ -73,7 +86,7 @@ async function douyinUrlParse(urlParse: URL, url: string, cookie: string | undef
// /video/:videoId
const matchResult: Match<VideoParam> = douyinVideoUrlMatch(urlParse.pathname);

if (typeof matchResult === 'object') {
if (typeof matchResult === 'object' && isVideoParamType(matchResult.params.videoType)) {
type = DouyinUrlType.Video;
id = matchResult.params.videoId;
}
Expand Down

0 comments on commit 10a5b9a

Please sign in to comment.