1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import { request, RequestConfig } from './request'
- import { createHmac } from 'crypto'
- export interface IdsConfig {
- endpoint: string
- accessKeyId: string
- accessKeySecret: string
- }
- export enum IdsChannel {
- Csdn = 1,
- Wechat = 2,
- Zhihu = 3,
- Juejin = 4
- }
- export type IdsCrawlRule = 'fulldata' | 'extdata'
- export interface IdsArticle {
- author_id: string | number
- cover_url: string
- code: IdsChannel
- title: string
- author_name?: string
- sn_code: string
- original_url?: string
- source_address?: string
- is_headline?: 0 | 1
- label?: string
- published_at: Date
- body: string
- is_original?: 0 | 1
- external_read_num?: number
- external_see_num?: number
- external_like_num?: number
- external_comment_num?: number
- }
- export class Ids {
- config: IdsConfig
- constructor(config: IdsConfig) {
- this.config = config
- }
- private async request(config: RequestConfig) {
- const { endpoint, accessKeyId, accessKeySecret } = this.config
- config.baseURL = endpoint
- config.url = config.url || '/'
- const timestamp = Math.floor(Date.now() / 1000)
- const data = config.url.toLowerCase() + timestamp
- const signature = createHmac('sha256', accessKeySecret).update(data).digest('base64')
- config.headers = config.headers || {}
- config.headers['Authorization'] = `IDS-HMAC-SHA256 Credential=${accessKeyId}/${timestamp},Signature=${signature}`
- const res = await request(config)
- if (res.data.status != 1) {
- console.error(res.data)
- throw new Error(`[${res.data.status}] ${res.data.info}`)
- }
- return res.data
- }
- getCrawlAuthors(channel: IdsChannel) {
- return this.request({
- method: 'POST',
- url: '/api/ids/getCrawlAuthors',
- data: {
- code: channel
- }
- })
- }
- getCrawlArticleRules(params: { ids: string[]; channel: IdsChannel }) {
- const { ids, channel } = params
- return this.request({
- method: 'POST',
- url: '/api/ids/getCrawlArticleRules',
- data: {
- code: channel,
- sn_codes: ids
- }
- })
- }
- putArticle(data: IdsArticle, rule: IdsCrawlRule) {
- return this.request({
- method: 'POST',
- url: `/api/ids/putArticleData?crawl=${rule}`,
- data
- })
- }
- }
|