| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-02-18 22:32:02 |
| 4 | * @LastEditTime: 2025-02-27 22:43:33 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: 任务模块的工具服务 |
| 7 | */ |
| 8 | import { Injectable } from '@nestjs/common'; |
| 9 | import { InjectModel } from '@nestjs/mongoose'; |
| 10 | import { Model } from 'mongoose'; |
| 11 | import { Task } from '../../db/schema/task.schema'; |
| 12 | import { UserTask } from '../../db/schema/user-task.schema'; |
| 13 | import { TaskMaterial } from 'src/db/schema/taskMaterial.schema'; |
| 14 | import axios from 'axios'; |
| 15 | import * as natural from 'natural'; |
| 16 | import { URL } from 'url'; |
| 17 | |
| 18 | @Injectable() |
| 19 | export class TaskUtilService { |
| 20 | constructor( |
| 21 | @InjectModel(Task.name) private taskModel: Model<Task>, |
| 22 | @InjectModel(UserTask.name) private userTaskModel: Model<UserTask>, |
| 23 | @InjectModel(TaskMaterial.name) |
| 24 | private taskMaterialModel: Model<TaskMaterial>, |
| 25 | ) {} |
| 26 | |
| 27 | /** |
| 28 | * 小红书内容相似度检测 |
| 29 | * @param content 内容 |
| 30 | * @param articleUrl 文章链接 |
| 31 | * @returns |
| 32 | */ |
| 33 | async articleXhsContentCheck( |
| 34 | content: string, |
| 35 | articleUrl: string, |
| 36 | ): Promise<{ |
| 37 | status: 1 | 0; |
| 38 | extent: number; |
| 39 | }> { |
| 40 | try { |
| 41 | // 解析URL |
| 42 | // https://www.xiaohongshu.com/explore/68063228000000001202f814?xsec_token=AB1nbuHhtoNuSMgYHIwZMaZqY1eo-IsGJkdrW04BZHKZQ=&xsec_source=pc_feed |
| 43 | |
| 44 | const inUrl = new URL(articleUrl); |
| 45 | const xsecToken = inUrl.searchParams.get('xsec_token'); |
| 46 | const noteId = inUrl.pathname.split('/').pop(); |
| 47 | |
| 48 | const body = { |
| 49 | note_id: noteId, |
| 50 | xsec_token: xsecToken, |
| 51 | }; |
| 52 | |
| 53 | const response = await axios.post<{ |
| 54 | code: number; // 200 |
| 55 | msg: string; |
| 56 | data: { |
| 57 | xsec_token: string; |
| 58 | title: string; |
| 59 | desc: string; |
| 60 | }; |
| 61 | }>(`https://platapi.yikart.cn/api/xhs/note_detail_v2`, body); |
| 62 | let { desc } = response.data.data; |
| 63 | |
| 64 | // desc 根据"[话题]"分割取出第一个 |
| 65 | const descArr = desc.split('[话题]'); |
| 66 | if (descArr.length > 1) desc = descArr[0]; |
| 67 | const res: number = natural.JaroWinklerDistance(content, desc); |
| 68 | return { |
| 69 | status: 1, |
| 70 | extent: res, |
| 71 | }; |
| 72 | } catch (error) { |
| 73 | console.log('------- 小红书内容相似度检测失败 -------', error); |
| 74 | return { |
| 75 | status: 0, |
| 76 | extent: 0, |
| 77 | }; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 |