| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- import { config } from '../config.ts';
- import type { ContentSample } from './tikhub.service.ts';
- import { archiveProviderResponse, cacheProviderCreators } from './local-creator-db.service.ts';
- export interface JustOneCreator {
- userId: string;
- platform: string; // 'xiaohongshu' | 'douyin'
- nickname: string;
- redId?: string;
- location: string;
- fansCount: number;
- imagePrice: number;
- videoPrice: number;
- minPrice: number;
- cooperationStatus: string;
- personalTags: string[];
- contentTags: string[];
- sourceProvider?: string;
- gender?: string;
- likedCollectCount?: number;
- contentType?: string;
- city?: string;
- geoLocation?: string;
- xiaohongshuUrl?: string;
- profileUrl?: string;
- coreUserId?: string;
- secUid?: string;
- uniqueId?: string;
- cooperationMethod?: string;
- contentSamples?: ContentSample[];
- }
- interface SearchParams {
- keyword: string;
- platform?: string;
- minFans?: number;
- maxFans?: number;
- minPrice?: number;
- maxPrice?: number;
- gender?: string;
- location?: string;
- page?: number;
- pageSize?: number;
- }
- /**
- * JustOne API 商业达人搜索服务
- * 文档: https://docs.justoneapi.com/zh/usage
- * 用于蒲公英/星图类商业达人数据聚合搜索
- */
- export async function searchCreators(params: SearchParams): Promise<JustOneCreator[]> {
- if (!config.justOneApi.apiKey) {
- console.warn('[JustOne] No API key configured, returning empty results');
- return [];
- }
- try {
- const queryParams = new URLSearchParams();
- queryParams.set('token', config.justOneApi.apiKey);
- queryParams.set('keyword', params.keyword);
- queryParams.set('page', String(params.page ?? 1));
- // 根据平台选择不同的接口路径和参数格式
- const endpoint = getPlatformEndpoint(params.platform);
- const isDouyin = params.platform === 'douyin' || params.platform === '抖音';
- if (isDouyin) {
- queryParams.set('searchType', 'CONTENT');
- if (params.minFans || params.maxFans) {
- const min = Math.floor((params.minFans || 0) / 10000);
- const max = params.maxFans ? Math.floor(params.maxFans / 10000) : 10000;
- const safeMax = Math.min(10000, Math.max(max, min + 1));
- if (min > 0 || safeMax < 10000) {
- queryParams.set('followerRange', `${min}-${safeMax}`);
- }
- }
- // 不传 kolPriceRange,避免单价过滤导致空结果
- } else {
- queryParams.set('searchType', 'NOTE');
- if (params.minFans) queryParams.set('fansNumberLower', String(params.minFans));
- if (params.maxFans) queryParams.set('fansNumberUpper', String(params.maxFans));
- if (params.gender) {
- const genderMap: Record<string, string> = { '女': 'FEMALE', '男': 'MALE', female: 'FEMALE', male: 'MALE' };
- queryParams.set('gender', genderMap[params.gender] || 'ALL');
- }
- }
- const requestUrl = `${config.justOneApi.baseUrl}${endpoint}?${queryParams.toString()}`;
- console.log('[JustOne] 请求 URL:', requestUrl.replace(config.justOneApi.apiKey, '***'));
- const response = await fetch(requestUrl, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- },
- });
- if (!response.ok) {
- const errorText = await response.text();
- console.error(`[JustOne] API error: ${response.status} - ${errorText}`);
- return [];
- }
- const data = await response.json();
- await archiveProviderResponse({
- provider: 'justone',
- endpoint,
- requestParams: Object.fromEntries(queryParams.entries()),
- responseBody: data,
- });
- console.log('[JustOne] 原始响应 code:', data.code, 'message:', data.message);
- // code 301 = FAILED, RETRY——重试一次
- if (data.code === 301) {
- console.log('[JustOne] 收到 301 RETRY,3秒后重试...');
- await new Promise(r => setTimeout(r, 3000));
- const retryResp = await fetch(requestUrl, { method: 'GET', headers: { 'Content-Type': 'application/json' } });
- if (!retryResp.ok) {
- console.error(`[JustOne] 重试失败: ${retryResp.status}`);
- return [];
- }
- const retryData = await retryResp.json();
- await archiveProviderResponse({
- provider: 'justone',
- endpoint,
- requestParams: { ...Object.fromEntries(queryParams.entries()), retry: true },
- responseBody: retryData,
- });
- console.log('[JustOne] 重试响应 code:', retryData.code, 'message:', retryData.message);
- if (retryData.code !== 0) {
- console.error('[JustOne] 重试仍失败, code:', retryData.code);
- return [];
- }
- const creators = parseCreatorList(retryData);
- await cacheProviderCreators({
- provider: 'justone',
- endpoint,
- requestParams: { ...Object.fromEntries(queryParams.entries()), retry: true },
- creators,
- });
- return creators;
- }
- if (data.code !== 0 && data.code !== undefined) {
- console.error('[JustOne] 业务错误 code:', data.code, data.message);
- return [];
- }
- const creators = parseCreatorList(data);
- await cacheProviderCreators({
- provider: 'justone',
- endpoint,
- requestParams: Object.fromEntries(queryParams.entries()),
- creators,
- });
- return creators;
- } catch (error) {
- console.error('[JustOne] Search failed:', error);
- return [];
- }
- }
- /**
- * 从 JustOne API 响应中解析创作者列表
- * - 小红书蒲公英: data.data.kols,字段直接在条目上
- * - 抖音星图: data.data.authors,字段全在 attribute_datas 字符串字典里
- */
- function parseCreatorList(data: Record<string, unknown>): JustOneCreator[] {
- const d = data.data as Record<string, unknown> | undefined;
- if (!d) {
- console.log('[JustOne] data.data 为空');
- return [];
- }
- console.log('[JustOne] data.data keys:', JSON.stringify(Object.keys(d)));
- // 小红书蒲公英: kols 字段
- if (Array.isArray(d.kols) && d.kols.length >= 0) {
- const list = d.kols as Record<string, unknown>[];
- console.log(`[JustOne] XHS kols 数量: ${list.length}`);
- if (list.length > 0) console.log('[JustOne] XHS 第一条 keys:', JSON.stringify(Object.keys(list[0])));
- const creators = list.map(parseXhsCreator);
- console.log('[JustOne] XHS 解析完成,数量:', creators.length);
- if (creators.length > 0) console.log('[JustOne] XHS 第一条解析结果:', JSON.stringify(creators[0]));
- return creators;
- }
- // 抖音星图: authors 字段
- if (Array.isArray(d.authors) && d.authors.length >= 0) {
- const list = d.authors as Record<string, unknown>[];
- console.log(`[JustOne] Douyin authors 数量: ${list.length}`);
- if (list.length > 0) console.log('[JustOne] Douyin 第一条 keys:', JSON.stringify(Object.keys(list[0])));
- const creators = list.map(parseDouyinXingtuCreator);
- console.log('[JustOne] Douyin 解析完成,数量:', creators.length);
- if (creators.length > 0) console.log('[JustOne] Douyin 第一条解析结果:', JSON.stringify(creators[0]));
- return creators;
- }
- // 通用回退(按小红书处理)
- const rawList = d.bloggerList || d.list || d.items || d.result || [];
- const listArr = Array.isArray(rawList) ? rawList as Record<string, unknown>[] : [];
- console.log(`[JustOne] 通用字段列表数量: ${listArr.length}`);
- return listArr.map(parseXhsCreator);
- }
- /** 解析小红书蒲公英创作者(字段直接在条目上) */
- function parseXhsCreator(item: Record<string, unknown>): JustOneCreator {
- // contentTags 是对象数组 [{taxonomy1Tag: '护肤', taxonomy2Tags: [...]}],提取一级标签
- const rawContentTags = item.contentTags;
- const contentTags: string[] = Array.isArray(rawContentTags)
- ? rawContentTags
- .map((t: unknown) => {
- if (typeof t === 'string') return t;
- if (typeof t === 'object' && t !== null) {
- const obj = t as Record<string, unknown>;
- return typeof obj.taxonomy1Tag === 'string' ? obj.taxonomy1Tag : null;
- }
- return null;
- })
- .filter((s): s is string => s !== null)
- : [];
- // featureTags 是字符串数组,直接作为 personalTags
- const featureTags = toStringArray(item.featureTags || item.personalTags || item.personal_tags || []);
- // fansCount:fansCount 始终为 0,实际应用 fansNum
- const fansCount = Number(item.fansNum || item.fansCount || item.fans_count || item.followerCount || 0);
- // 价格:图文 picturePrice,视频 videoPrice,最低 lowerPrice
- const imagePrice = Number(item.picturePrice || item.imagePrice || 0);
- const videoPrice = Number(item.videoPrice || item.video_price || 0);
- const minPrice = Number(item.lowerPrice || item.picturePrice || item.imagePrice || 0);
- // 合作状态:cooperateState=1 表示可合作
- const cooperateState = Number(item.cooperateState || 0);
- const cooperationStatus = cooperateState === 1 ? 'active' : String(item.cooperationStatus || '');
- const genderRaw = String(item.gender || '');
- const genderMap: Record<string, string> = { '女': 'female', '男': 'male', 'FEMALE': 'female', 'MALE': 'male' };
- return {
- userId: String(item.userId || item.user_id || ''),
- platform: 'xiaohongshu',
- nickname: String(item.name || item.nickname || item.nickName || ''),
- redId: String(item.redId || item.red_id || ''),
- location: String(item.location || item.city || ''),
- fansCount,
- imagePrice,
- videoPrice,
- minPrice,
- cooperationStatus,
- personalTags: featureTags,
- contentTags,
- gender: genderMap[genderRaw] || genderRaw,
- likedCollectCount: Number(item.likedCollectCount || item.likeCollectCount || item.interactionCount || 0),
- contentType: contentTags.join('、'),
- city: String(item.city || item.location || ''),
- geoLocation: String(item.location || item.city || ''),
- xiaohongshuUrl: item.userId ? `https://www.xiaohongshu.com/user/profile/${String(item.userId || item.user_id)}` : '',
- cooperationMethod: '',
- };
- }
- /** 解析抖音星图创作者(数据全在 attribute_datas 字符串字典里) */
- function parseDouyinXingtuCreator(item: Record<string, unknown>): JustOneCreator {
- const attr = (item.attribute_datas || {}) as Record<string, string>;
- const fansCount = Number(attr.follower || attr.fans_count || 0);
- const prices = parseDouyinTaskPrices(item.task_infos);
- const genderCode = attr.gender || '0';
- const genderMap: Record<string, string> = { '1': 'male', '2': 'female', '0': '' };
- // content_theme_labels_180d 是 JSON 字符串数组(实际响应中常为空 "[]")
- // 若为空则退而从 last_10_items 视频标题中提取 hashtag 作为 contentTags
- let contentTags: string[] = [];
- try {
- const raw = attr.content_theme_labels_180d || '[]';
- const parsed = JSON.parse(raw) as unknown[];
- if (Array.isArray(parsed) && parsed.length > 0) {
- contentTags = parsed.map(String);
- }
- } catch { /* ignore */ }
- if (contentTags.length === 0) {
- try {
- const items = JSON.parse(attr.last_10_items || '[]') as Record<string, unknown>[];
- const tags: string[] = [];
- for (const it of items.slice(0, 5)) {
- const title = String(it.item_title || '');
- for (const word of title.split(/\s+/)) {
- if (word.startsWith('#')) tags.push(word.slice(1));
- }
- }
- // 去重,最多保留 10 个
- contentTags = [...new Set(tags)].slice(0, 10);
- } catch { /* ignore */ }
- }
- contentTags = [...new Set([...contentTags, ...parseDouyinTagsRelation(attr.tags_relation)])].slice(0, 12);
- // author_thin_mid_word_association_index 是 JSON 对象 {词: 权重},取 key 作为标签
- let personalTags: string[] = [];
- try {
- const raw = attr.author_thin_mid_word_association_index || '{}';
- const parsed = JSON.parse(raw) as Record<string, unknown>;
- personalTags = Object.keys(parsed);
- } catch { /* ignore */ }
- return {
- userId: String(item.star_id || attr.id || attr.core_user_id || ''),
- platform: 'douyin',
- nickname: String(attr.nick_name || attr.nickname || attr.name || ''),
- location: String(attr.city || attr.province || ''),
- fansCount,
- imagePrice: prices.imagePrice,
- videoPrice: prices.videoPrice,
- minPrice: prices.minPrice,
- cooperationStatus: attr.author_status === '1' ? 'active' : '',
- personalTags,
- contentTags,
- gender: genderMap[genderCode] || '',
- likedCollectCount: Number(attr.total_favorited || attr.total_favorite || 0),
- contentType: contentTags.join('、'),
- city: String(attr.city || ''),
- geoLocation: String(attr.province || attr.city || ''),
- coreUserId: String(attr.core_user_id || ''),
- cooperationMethod: prices.videoPrice > 0 ? '报备视频' : '',
- contentSamples: parseDouyinRecentItems(attr.last_10_items),
- };
- }
- function parseDouyinTaskPrices(rawTaskInfos: unknown): { imagePrice: number; videoPrice: number; minPrice: number } {
- const priceInfos: number[] = [];
- if (Array.isArray(rawTaskInfos)) {
- for (const task of rawTaskInfos) {
- if (typeof task !== 'object' || task === null) continue;
- const infos = (task as Record<string, unknown>).price_infos;
- if (!Array.isArray(infos)) continue;
- for (const info of infos) {
- if (typeof info !== 'object' || info === null) continue;
- const price = Number((info as Record<string, unknown>).price || 0);
- const videoTypeStatus = Number((info as Record<string, unknown>).video_type_status ?? 1);
- if (price > 0 && videoTypeStatus === 1) priceInfos.push(price);
- }
- }
- }
- const sortedPrices = [...new Set(priceInfos)].sort((a, b) => a - b);
- const minPrice = sortedPrices[0] || 0;
- const videoPrice = sortedPrices.find((price) => price >= 1000) || minPrice;
- return {
- imagePrice: 0,
- videoPrice,
- minPrice,
- };
- }
- function parseDouyinRecentItems(rawItems?: string): ContentSample[] {
- try {
- const items = JSON.parse(rawItems || '[]') as Record<string, unknown>[];
- if (!Array.isArray(items)) return [];
- return items.slice(0, 10).map((item) => ({
- noteId: String(item.item_id || ''),
- title: String(item.item_title || ''),
- content: String(item.item_title || ''),
- publishTime: Number(item.item_publish_time || item.item_create_time || 0) > 0
- ? new Date(Number(item.item_publish_time || item.item_create_time) * 1000).toISOString()
- : '',
- likeCount: Number(item.like_cnt || 0),
- commentCount: Number(item.comment_cnt || 0),
- collectCount: 0,
- shareCount: Number(item.share_cnt || 0),
- type: 'video',
- }));
- } catch {
- return [];
- }
- }
- function parseDouyinTagsRelation(raw?: string): string[] {
- try {
- const parsed = JSON.parse(raw || '{}') as Record<string, unknown>;
- const tags: string[] = [];
- for (const [category, children] of Object.entries(parsed)) {
- tags.push(category);
- if (Array.isArray(children)) tags.push(...children.map(String));
- }
- return tags.filter(Boolean);
- } catch {
- return [];
- }
- }
- function toStringArray(val: unknown): string[] {
- if (!Array.isArray(val)) return [];
- return val
- .map(v => {
- if (typeof v === 'string') return v;
- if (typeof v !== 'object' || v === null) return null;
- const obj = v as Record<string, unknown>;
- const text = obj.name || obj.tagName || obj.label || obj.value || obj.text || obj.title;
- if (text && typeof text === 'string') return text;
- for (const k of Object.keys(obj)) {
- if (typeof obj[k] === 'string' && (obj[k] as string).length > 0) return obj[k] as string;
- }
- return null;
- })
- .filter((s): s is string => s !== null && s.length > 0);
- }
- /**
- * 获取创作者详情(粉丝画像、互动数据等)
- */
- export async function getCreatorProfile(userId: string, platform?: string): Promise<Record<string, unknown> | null> {
- if (!config.justOneApi.apiKey) {
- return null;
- }
- try {
- const queryParams = new URLSearchParams();
- queryParams.set('token', config.justOneApi.apiKey);
- queryParams.set('userId', userId);
- const profileEndpoint = getProfileEndpoint(platform);
- const response = await fetch(
- `${config.justOneApi.baseUrl}${profileEndpoint}?${queryParams.toString()}`,
- {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- );
- if (!response.ok) {
- return null;
- }
- const data = await response.json();
- return data.data || data;
- } catch (error) {
- console.error('[JustOne] Profile fetch failed:', error);
- return null;
- }
- }
- /**
- * 根据平台返回对应的搜索接口路径
- * 文档: https://docs.justoneapi.com/zh/usage
- */
- function getPlatformEndpoint(platform?: string): string {
- switch (platform) {
- case 'xiaohongshu':
- case '小红书':
- return '/api/xiaohongshu-pgy/api/solar/cooperator/blogger/v2/v1';
- case 'douyin':
- case '抖音':
- return '/api/douyin-xingtu/gw/api/gsearch/search_for_author_square/v1';
- default:
- return '/api/xiaohongshu-pgy/api/solar/cooperator/blogger/v2/v1';
- }
- }
- function getProfileEndpoint(platform?: string): string {
- switch (platform) {
- case 'xiaohongshu':
- case '小红书':
- return '/api/xiaohongshu-pgy/api/solar/cooperator/blogger/profile/v1';
- case 'douyin':
- case '抖音':
- return '/api/douyin-xingtu/gw/api/gsearch/search_for_author_square/v1';
- default:
- return '/api/xiaohongshu-pgy/api/solar/cooperator/blogger/profile/v1';
- }
- }
|