| 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 | 'xhslink.com': AccountType.RedNote, |
| 19 | 'twitter.com': AccountType.Twitter, |
| 20 | 'x.com': AccountType.Twitter, |
| 21 | 'mobile.twitter.com': AccountType.Twitter, |
| 22 | 't.co': AccountType.Twitter, |
| 23 | 'kuaishou.com': AccountType.Kwai, |
| 24 | 'v.kuaishou.com': AccountType.Kwai, |
| 25 | 'c.kuaishou.com': AccountType.Kwai, |
| 26 | 'pinterest.com': AccountType.Pinterest, |
| 27 | 'pin.it': AccountType.Pinterest, |
| 28 | 'instagram.com': AccountType.Instagram, |
| 29 | 'facebook.com': AccountType.Facebook, |
| 30 | 'm.facebook.com': AccountType.Facebook, |
| 31 | 'fb.watch': AccountType.Facebook, |
| 32 | 'threads.com': AccountType.Threads, |
| 33 | 'threads.net': AccountType.Threads, |
| 34 | 'linkedin.com': AccountType.LinkedIn, |
| 35 | 'channels.weixin.qq.com': AccountType.WeChatChannels, |
| 36 | 'mp.weixin.qq.com': AccountType.WeChatOfficial, |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * 从 URL 中识别平台类型 |
| 41 | * @param workLink 作品链接 |
| 42 | * @returns 平台类型,无法识别时返回 null |
| 43 | */ |
| 44 | export function detectAccountTypeFromUrl(workLink: string): AccountType | null { |
| 45 | try { |
| 46 | const url = new URL(workLink) |
| 47 | const hostname = url.hostname.replace(/^www\./, '') |
| 48 | |
| 49 | // 精确匹配 |
| 50 | if (DOMAIN_TO_ACCOUNT_TYPE[hostname]) { |
| 51 | return DOMAIN_TO_ACCOUNT_TYPE[hostname] |
| 52 | } |
| 53 | |
| 54 | // Pinterest 子域名匹配(如 br.pinterest.com) |
| 55 | if (hostname.endsWith('.pinterest.com')) { |
| 56 | return AccountType.Pinterest |
| 57 | } |
| 58 | |
| 59 | return null |
| 60 | } |
| 61 | catch { |
| 62 | return null |
| 63 | } |
| 64 | } |
| 65 |