justone.service.ts 16 KB

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