justone.service.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. import { config } from '../config.ts';
  2. import type { ContentSample } from './tikhub.service.ts';
  3. import { archiveProviderResponse, cacheProviderCreators } from './local-creator-db.service.ts';
  4. export interface JustOneCreator {
  5. userId: string;
  6. platform: string; // 'xiaohongshu' | 'douyin'
  7. nickname: string;
  8. redId?: string;
  9. location: string;
  10. fansCount: number;
  11. imagePrice: number;
  12. videoPrice: number;
  13. minPrice: number;
  14. cooperationStatus: string;
  15. personalTags: string[];
  16. contentTags: string[];
  17. sourceProvider?: string;
  18. gender?: string;
  19. likedCollectCount?: number;
  20. contentType?: string;
  21. city?: string;
  22. geoLocation?: string;
  23. xiaohongshuUrl?: string;
  24. profileUrl?: string;
  25. coreUserId?: string;
  26. secUid?: string;
  27. uniqueId?: string;
  28. cooperationMethod?: string;
  29. contentSamples?: ContentSample[];
  30. }
  31. interface SearchParams {
  32. keyword: string;
  33. platform?: string;
  34. minFans?: number;
  35. maxFans?: number;
  36. minPrice?: number;
  37. maxPrice?: number;
  38. gender?: string;
  39. location?: string;
  40. page?: number;
  41. pageSize?: number;
  42. }
  43. /**
  44. * JustOne API 商业达人搜索服务
  45. * 文档: https://docs.justoneapi.com/zh/usage
  46. * 用于蒲公英/星图类商业达人数据聚合搜索
  47. */
  48. export async function searchCreators(params: SearchParams): Promise<JustOneCreator[]> {
  49. if (!config.justOneApi.apiKey) {
  50. console.warn('[JustOne] No API key configured, returning empty results');
  51. return [];
  52. }
  53. try {
  54. const queryParams = new URLSearchParams();
  55. queryParams.set('token', config.justOneApi.apiKey);
  56. queryParams.set('keyword', params.keyword);
  57. queryParams.set('page', String(params.page ?? 1));
  58. // 根据平台选择不同的接口路径和参数格式
  59. const endpoint = getPlatformEndpoint(params.platform);
  60. const isDouyin = params.platform === 'douyin' || params.platform === '抖音';
  61. if (isDouyin) {
  62. queryParams.set('searchType', 'CONTENT');
  63. if (params.minFans || params.maxFans) {
  64. const min = Math.floor((params.minFans || 0) / 10000);
  65. const max = params.maxFans ? Math.floor(params.maxFans / 10000) : 10000;
  66. const safeMax = Math.min(10000, Math.max(max, min + 1));
  67. if (min > 0 || safeMax < 10000) {
  68. queryParams.set('followerRange', `${min}-${safeMax}`);
  69. }
  70. }
  71. // 不传 kolPriceRange,避免单价过滤导致空结果
  72. } else {
  73. queryParams.set('searchType', 'NOTE');
  74. if (params.minFans) queryParams.set('fansNumberLower', String(params.minFans));
  75. if (params.maxFans) queryParams.set('fansNumberUpper', String(params.maxFans));
  76. if (params.gender) {
  77. const genderMap: Record<string, string> = { '女': 'FEMALE', '男': 'MALE', female: 'FEMALE', male: 'MALE' };
  78. queryParams.set('gender', genderMap[params.gender] || 'ALL');
  79. }
  80. }
  81. const requestUrl = `${config.justOneApi.baseUrl}${endpoint}?${queryParams.toString()}`;
  82. console.log('[JustOne] 请求 URL:', requestUrl.replace(config.justOneApi.apiKey, '***'));
  83. const response = await fetch(requestUrl, {
  84. method: 'GET',
  85. headers: {
  86. 'Content-Type': 'application/json',
  87. },
  88. });
  89. if (!response.ok) {
  90. const errorText = await response.text();
  91. console.error(`[JustOne] API error: ${response.status} - ${errorText}`);
  92. return [];
  93. }
  94. const data = await response.json();
  95. await archiveProviderResponse({
  96. provider: 'justone',
  97. endpoint,
  98. requestParams: Object.fromEntries(queryParams.entries()),
  99. responseBody: data,
  100. });
  101. console.log('[JustOne] 原始响应 code:', data.code, 'message:', data.message);
  102. // code 301 = FAILED, RETRY——重试一次
  103. if (data.code === 301) {
  104. console.log('[JustOne] 收到 301 RETRY,3秒后重试...');
  105. await new Promise(r => setTimeout(r, 3000));
  106. const retryResp = await fetch(requestUrl, { method: 'GET', headers: { 'Content-Type': 'application/json' } });
  107. if (!retryResp.ok) {
  108. console.error(`[JustOne] 重试失败: ${retryResp.status}`);
  109. return [];
  110. }
  111. const retryData = await retryResp.json();
  112. await archiveProviderResponse({
  113. provider: 'justone',
  114. endpoint,
  115. requestParams: { ...Object.fromEntries(queryParams.entries()), retry: true },
  116. responseBody: retryData,
  117. });
  118. console.log('[JustOne] 重试响应 code:', retryData.code, 'message:', retryData.message);
  119. if (retryData.code !== 0) {
  120. console.error('[JustOne] 重试仍失败, code:', retryData.code);
  121. return [];
  122. }
  123. const creators = parseCreatorList(retryData);
  124. await cacheProviderCreators({
  125. provider: 'justone',
  126. endpoint,
  127. requestParams: { ...Object.fromEntries(queryParams.entries()), retry: true },
  128. creators,
  129. });
  130. return creators;
  131. }
  132. if (data.code !== 0 && data.code !== undefined) {
  133. console.error('[JustOne] 业务错误 code:', data.code, data.message);
  134. return [];
  135. }
  136. const creators = parseCreatorList(data);
  137. await cacheProviderCreators({
  138. provider: 'justone',
  139. endpoint,
  140. requestParams: Object.fromEntries(queryParams.entries()),
  141. creators,
  142. });
  143. return creators;
  144. } catch (error) {
  145. console.error('[JustOne] Search failed:', error);
  146. return [];
  147. }
  148. }
  149. /**
  150. * 从 JustOne API 响应中解析创作者列表
  151. * - 小红书蒲公英: data.data.kols,字段直接在条目上
  152. * - 抖音星图: data.data.authors,字段全在 attribute_datas 字符串字典里
  153. */
  154. function parseCreatorList(data: Record<string, unknown>): JustOneCreator[] {
  155. const d = data.data as Record<string, unknown> | undefined;
  156. if (!d) {
  157. console.log('[JustOne] data.data 为空');
  158. return [];
  159. }
  160. console.log('[JustOne] data.data keys:', JSON.stringify(Object.keys(d)));
  161. // 小红书蒲公英: kols 字段
  162. if (Array.isArray(d.kols) && d.kols.length >= 0) {
  163. const list = d.kols as Record<string, unknown>[];
  164. console.log(`[JustOne] XHS kols 数量: ${list.length}`);
  165. if (list.length > 0) console.log('[JustOne] XHS 第一条 keys:', JSON.stringify(Object.keys(list[0])));
  166. const creators = list.map(parseXhsCreator);
  167. console.log('[JustOne] XHS 解析完成,数量:', creators.length);
  168. if (creators.length > 0) console.log('[JustOne] XHS 第一条解析结果:', JSON.stringify(creators[0]));
  169. return creators;
  170. }
  171. // 抖音星图: authors 字段
  172. if (Array.isArray(d.authors) && d.authors.length >= 0) {
  173. const list = d.authors as Record<string, unknown>[];
  174. console.log(`[JustOne] Douyin authors 数量: ${list.length}`);
  175. if (list.length > 0) console.log('[JustOne] Douyin 第一条 keys:', JSON.stringify(Object.keys(list[0])));
  176. const creators = list.map(parseDouyinXingtuCreator);
  177. console.log('[JustOne] Douyin 解析完成,数量:', creators.length);
  178. if (creators.length > 0) console.log('[JustOne] Douyin 第一条解析结果:', JSON.stringify(creators[0]));
  179. return creators;
  180. }
  181. // 通用回退(按小红书处理)
  182. const rawList = d.bloggerList || d.list || d.items || d.result || [];
  183. const listArr = Array.isArray(rawList) ? rawList as Record<string, unknown>[] : [];
  184. console.log(`[JustOne] 通用字段列表数量: ${listArr.length}`);
  185. return listArr.map(parseXhsCreator);
  186. }
  187. /** 解析小红书蒲公英创作者(字段直接在条目上) */
  188. function parseXhsCreator(item: Record<string, unknown>): JustOneCreator {
  189. // contentTags 是对象数组 [{taxonomy1Tag: '护肤', taxonomy2Tags: [...]}],提取一级标签
  190. const rawContentTags = item.contentTags;
  191. const contentTags: string[] = Array.isArray(rawContentTags)
  192. ? rawContentTags
  193. .map((t: unknown) => {
  194. if (typeof t === 'string') return t;
  195. if (typeof t === 'object' && t !== null) {
  196. const obj = t as Record<string, unknown>;
  197. return typeof obj.taxonomy1Tag === 'string' ? obj.taxonomy1Tag : null;
  198. }
  199. return null;
  200. })
  201. .filter((s): s is string => s !== null)
  202. : [];
  203. // featureTags 是字符串数组,直接作为 personalTags
  204. const featureTags = toStringArray(item.featureTags || item.personalTags || item.personal_tags || []);
  205. // fansCount:fansCount 始终为 0,实际应用 fansNum
  206. const fansCount = Number(item.fansNum || item.fansCount || item.fans_count || item.followerCount || 0);
  207. // 价格:图文 picturePrice,视频 videoPrice,最低 lowerPrice
  208. const imagePrice = Number(item.picturePrice || item.imagePrice || 0);
  209. const videoPrice = Number(item.videoPrice || item.video_price || 0);
  210. const minPrice = Number(item.lowerPrice || item.picturePrice || item.imagePrice || 0);
  211. // 合作状态:cooperateState=1 表示可合作
  212. const cooperateState = Number(item.cooperateState || 0);
  213. const cooperationStatus = cooperateState === 1 ? 'active' : String(item.cooperationStatus || '');
  214. const genderRaw = String(item.gender || '');
  215. const genderMap: Record<string, string> = { '女': 'female', '男': 'male', 'FEMALE': 'female', 'MALE': 'male' };
  216. return {
  217. userId: String(item.userId || item.user_id || ''),
  218. platform: 'xiaohongshu',
  219. nickname: String(item.name || item.nickname || item.nickName || ''),
  220. redId: String(item.redId || item.red_id || ''),
  221. location: String(item.location || item.city || ''),
  222. fansCount,
  223. imagePrice,
  224. videoPrice,
  225. minPrice,
  226. cooperationStatus,
  227. personalTags: featureTags,
  228. contentTags,
  229. gender: genderMap[genderRaw] || genderRaw,
  230. likedCollectCount: Number(item.likedCollectCount || item.likeCollectCount || item.interactionCount || 0),
  231. contentType: contentTags.join('、'),
  232. city: String(item.city || item.location || ''),
  233. geoLocation: String(item.location || item.city || ''),
  234. xiaohongshuUrl: item.userId ? `https://www.xiaohongshu.com/user/profile/${String(item.userId || item.user_id)}` : '',
  235. cooperationMethod: '',
  236. };
  237. }
  238. /** 解析抖音星图创作者(数据全在 attribute_datas 字符串字典里) */
  239. function parseDouyinXingtuCreator(item: Record<string, unknown>): JustOneCreator {
  240. const attr = (item.attribute_datas || {}) as Record<string, string>;
  241. const fansCount = Number(attr.follower || attr.fans_count || 0);
  242. const prices = parseDouyinTaskPrices(item.task_infos);
  243. const genderCode = attr.gender || '0';
  244. const genderMap: Record<string, string> = { '1': 'male', '2': 'female', '0': '' };
  245. // content_theme_labels_180d 是 JSON 字符串数组(实际响应中常为空 "[]")
  246. // 若为空则退而从 last_10_items 视频标题中提取 hashtag 作为 contentTags
  247. let contentTags: string[] = [];
  248. try {
  249. const raw = attr.content_theme_labels_180d || '[]';
  250. const parsed = JSON.parse(raw) as unknown[];
  251. if (Array.isArray(parsed) && parsed.length > 0) {
  252. contentTags = parsed.map(String);
  253. }
  254. } catch { /* ignore */ }
  255. if (contentTags.length === 0) {
  256. try {
  257. const items = JSON.parse(attr.last_10_items || '[]') as Record<string, unknown>[];
  258. const tags: string[] = [];
  259. for (const it of items.slice(0, 5)) {
  260. const title = String(it.item_title || '');
  261. for (const word of title.split(/\s+/)) {
  262. if (word.startsWith('#')) tags.push(word.slice(1));
  263. }
  264. }
  265. // 去重,最多保留 10 个
  266. contentTags = [...new Set(tags)].slice(0, 10);
  267. } catch { /* ignore */ }
  268. }
  269. contentTags = [...new Set([...contentTags, ...parseDouyinTagsRelation(attr.tags_relation)])].slice(0, 12);
  270. // author_thin_mid_word_association_index 是 JSON 对象 {词: 权重},取 key 作为标签
  271. let personalTags: string[] = [];
  272. try {
  273. const raw = attr.author_thin_mid_word_association_index || '{}';
  274. const parsed = JSON.parse(raw) as Record<string, unknown>;
  275. personalTags = Object.keys(parsed);
  276. } catch { /* ignore */ }
  277. return {
  278. userId: String(item.star_id || attr.id || attr.core_user_id || ''),
  279. platform: 'douyin',
  280. nickname: String(attr.nick_name || attr.nickname || attr.name || ''),
  281. location: String(attr.city || attr.province || ''),
  282. fansCount,
  283. imagePrice: prices.imagePrice,
  284. videoPrice: prices.videoPrice,
  285. minPrice: prices.minPrice,
  286. cooperationStatus: attr.author_status === '1' ? 'active' : '',
  287. personalTags,
  288. contentTags,
  289. gender: genderMap[genderCode] || '',
  290. likedCollectCount: Number(attr.total_favorited || attr.total_favorite || 0),
  291. contentType: contentTags.join('、'),
  292. city: String(attr.city || ''),
  293. geoLocation: String(attr.province || attr.city || ''),
  294. coreUserId: String(attr.core_user_id || ''),
  295. cooperationMethod: prices.videoPrice > 0 ? '报备视频' : '',
  296. contentSamples: parseDouyinRecentItems(attr.last_10_items),
  297. };
  298. }
  299. function parseDouyinTaskPrices(rawTaskInfos: unknown): { imagePrice: number; videoPrice: number; minPrice: number } {
  300. const priceInfos: number[] = [];
  301. if (Array.isArray(rawTaskInfos)) {
  302. for (const task of rawTaskInfos) {
  303. if (typeof task !== 'object' || task === null) continue;
  304. const infos = (task as Record<string, unknown>).price_infos;
  305. if (!Array.isArray(infos)) continue;
  306. for (const info of infos) {
  307. if (typeof info !== 'object' || info === null) continue;
  308. const price = Number((info as Record<string, unknown>).price || 0);
  309. const videoTypeStatus = Number((info as Record<string, unknown>).video_type_status ?? 1);
  310. if (price > 0 && videoTypeStatus === 1) priceInfos.push(price);
  311. }
  312. }
  313. }
  314. const sortedPrices = [...new Set(priceInfos)].sort((a, b) => a - b);
  315. const minPrice = sortedPrices[0] || 0;
  316. const videoPrice = sortedPrices.find((price) => price >= 1000) || minPrice;
  317. return {
  318. imagePrice: 0,
  319. videoPrice,
  320. minPrice,
  321. };
  322. }
  323. function parseDouyinRecentItems(rawItems?: string): ContentSample[] {
  324. try {
  325. const items = JSON.parse(rawItems || '[]') as Record<string, unknown>[];
  326. if (!Array.isArray(items)) return [];
  327. return items.slice(0, 10).map((item) => ({
  328. noteId: String(item.item_id || ''),
  329. title: String(item.item_title || ''),
  330. content: String(item.item_title || ''),
  331. publishTime: Number(item.item_publish_time || item.item_create_time || 0) > 0
  332. ? new Date(Number(item.item_publish_time || item.item_create_time) * 1000).toISOString()
  333. : '',
  334. likeCount: Number(item.like_cnt || 0),
  335. commentCount: Number(item.comment_cnt || 0),
  336. collectCount: 0,
  337. shareCount: Number(item.share_cnt || 0),
  338. type: 'video',
  339. }));
  340. } catch {
  341. return [];
  342. }
  343. }
  344. function parseDouyinTagsRelation(raw?: string): string[] {
  345. try {
  346. const parsed = JSON.parse(raw || '{}') as Record<string, unknown>;
  347. const tags: string[] = [];
  348. for (const [category, children] of Object.entries(parsed)) {
  349. tags.push(category);
  350. if (Array.isArray(children)) tags.push(...children.map(String));
  351. }
  352. return tags.filter(Boolean);
  353. } catch {
  354. return [];
  355. }
  356. }
  357. function toStringArray(val: unknown): string[] {
  358. if (!Array.isArray(val)) return [];
  359. return val
  360. .map(v => {
  361. if (typeof v === 'string') return v;
  362. if (typeof v !== 'object' || v === null) return null;
  363. const obj = v as Record<string, unknown>;
  364. const text = obj.name || obj.tagName || obj.label || obj.value || obj.text || obj.title;
  365. if (text && typeof text === 'string') return text;
  366. for (const k of Object.keys(obj)) {
  367. if (typeof obj[k] === 'string' && (obj[k] as string).length > 0) return obj[k] as string;
  368. }
  369. return null;
  370. })
  371. .filter((s): s is string => s !== null && s.length > 0);
  372. }
  373. /**
  374. * 获取创作者详情(粉丝画像、互动数据等)
  375. */
  376. export async function getCreatorProfile(userId: string, platform?: string): Promise<Record<string, unknown> | null> {
  377. if (!config.justOneApi.apiKey) {
  378. return null;
  379. }
  380. try {
  381. const queryParams = new URLSearchParams();
  382. queryParams.set('token', config.justOneApi.apiKey);
  383. queryParams.set('userId', userId);
  384. const profileEndpoint = getProfileEndpoint(platform);
  385. const response = await fetch(
  386. `${config.justOneApi.baseUrl}${profileEndpoint}?${queryParams.toString()}`,
  387. {
  388. method: 'GET',
  389. headers: {
  390. 'Content-Type': 'application/json',
  391. },
  392. }
  393. );
  394. if (!response.ok) {
  395. return null;
  396. }
  397. const data = await response.json();
  398. return data.data || data;
  399. } catch (error) {
  400. console.error('[JustOne] Profile fetch failed:', error);
  401. return null;
  402. }
  403. }
  404. /**
  405. * 根据平台返回对应的搜索接口路径
  406. * 文档: https://docs.justoneapi.com/zh/usage
  407. */
  408. function getPlatformEndpoint(platform?: string): string {
  409. switch (platform) {
  410. case 'xiaohongshu':
  411. case '小红书':
  412. return '/api/xiaohongshu-pgy/api/solar/cooperator/blogger/v2/v1';
  413. case 'douyin':
  414. case '抖音':
  415. return '/api/douyin-xingtu/gw/api/gsearch/search_for_author_square/v1';
  416. default:
  417. return '/api/xiaohongshu-pgy/api/solar/cooperator/blogger/v2/v1';
  418. }
  419. }
  420. function getProfileEndpoint(platform?: string): string {
  421. switch (platform) {
  422. case 'xiaohongshu':
  423. case '小红书':
  424. return '/api/xiaohongshu-pgy/api/solar/cooperator/blogger/profile/v1';
  425. case 'douyin':
  426. case '抖音':
  427. return '/api/douyin-xingtu/gw/api/gsearch/search_for_author_square/v1';
  428. default:
  429. return '/api/xiaohongshu-pgy/api/solar/cooperator/blogger/profile/v1';
  430. }
  431. }