返回 AiToEarn
platform-url.util.ts
根目录 / project / aitoearn-backend / libs / common / src / utils / platform-url.util.ts
1 import { AccountType } from '../enums/account-type.enum'
2
3 const DOMAIN_TO_ACCOUNT_TYPE: Record<string, AccountType> = {
4 'tiktok.com': AccountType.TikTok,
5 'm.tiktok.com': AccountType.TikTok,
6 'vm.tiktok.com': AccountType.TikTok,
7 'vt.tiktok.com': AccountType.TikTok,
8 'youtube.com': AccountType.YouTube,
9 'm.youtube.com': AccountType.YouTube,
10 'youtu.be': AccountType.YouTube,
11 'douyin.com': AccountType.Douyin,
12 'v.douyin.com': AccountType.Douyin,
13 'iesdouyin.com': AccountType.Douyin,
14 'bilibili.com': AccountType.Bilibili,
15 'm.bilibili.com': AccountType.Bilibili,
16 'b23.tv': AccountType.Bilibili,
17 'xiaohongshu.com': AccountType.RedNote,
18 'rednote.com': AccountType.RedNote,
19 'xhslink.com': AccountType.RedNote,
20 'twitter.com': AccountType.Twitter,
21 'x.com': AccountType.Twitter,
22 'mobile.twitter.com': AccountType.Twitter,
23 't.co': AccountType.Twitter,
24 'kuaishou.com': AccountType.Kwai,
25 'v.kuaishou.com': AccountType.Kwai,
26 'c.kuaishou.com': AccountType.Kwai,
27 'pinterest.com': AccountType.Pinterest,
28 'pin.it': AccountType.Pinterest,
29 'instagram.com': AccountType.Instagram,
30 'facebook.com': AccountType.Facebook,
31 'm.facebook.com': AccountType.Facebook,
32 'fb.watch': AccountType.Facebook,
33 'threads.com': AccountType.Threads,
34 'threads.net': AccountType.Threads,
35 'linkedin.com': AccountType.LinkedIn,
36 'channels.weixin.qq.com': AccountType.WeChatChannels,
37 'mp.weixin.qq.com': AccountType.WeChatOfficial,
38 }
39
40 /**
41 * 从 URL 中识别平台类型
42 * @param workLink 作品链接
43 * @returns 平台类型,无法识别时返回 null
44 */
45 export function detectAccountTypeFromUrl(workLink: string): AccountType | null {
46 try {
47 const url = new URL(workLink)
48 const hostname = url.hostname.replace(/^www\./, '')
49
50 // 精确匹配
51 if (DOMAIN_TO_ACCOUNT_TYPE[hostname]) {
52 return DOMAIN_TO_ACCOUNT_TYPE[hostname]
53 }
54
55 // Pinterest 子域名匹配(如 br.pinterest.com)
56 if (hostname.endsWith('.pinterest.com')) {
57 return AccountType.Pinterest
58 }
59
60 return null
61 }
62 catch {
63 return null
64 }
65 }
66
66 lines TYPESCRIPT