123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Ids = exports.randomtail = exports.IdsSource = exports.IdsChannel = void 0;
- const request_1 = require("./request");
- const crypto_1 = require("crypto");
- var IdsChannel;
- (function (IdsChannel) {
- IdsChannel[IdsChannel["Csdn"] = 1] = "Csdn";
- IdsChannel[IdsChannel["Wechat"] = 2] = "Wechat";
- IdsChannel[IdsChannel["Zhihu"] = 3] = "Zhihu";
- IdsChannel[IdsChannel["Juejin"] = 4] = "Juejin";
- })(IdsChannel = exports.IdsChannel || (exports.IdsChannel = {}));
- var IdsSource;
- (function (IdsSource) {
- IdsSource[IdsSource["Manual"] = 0] = "Manual";
- IdsSource[IdsSource["User"] = 1] = "User";
- IdsSource[IdsSource["AliyunMarket"] = 2] = "AliyunMarket";
- IdsSource[IdsSource["TencentCloudMarket"] = 3] = "TencentCloudMarket";
- IdsSource[IdsSource["HuaweiCloudMarket"] = 4] = "HuaweiCloudMarket";
- IdsSource[IdsSource["Xiniu"] = 6] = "Xiniu";
- IdsSource[IdsSource["Website"] = 7] = "Website";
- })(IdsSource = exports.IdsSource || (exports.IdsSource = {}));
- const genNonceString = () => crypto_1.randomBytes(8).toString('hex');
- const randomtail = () => `_=${genNonceString()}`;
- exports.randomtail = randomtail;
- class Ids {
- constructor(config) {
- this.config = config;
- }
- async request(config) {
- const { endpoint, accessKeyId, accessKeySecret } = this.config;
- config.baseURL = endpoint;
- config.url = config.url || '/';
- config.params = config.params || {};
- config.params.nonce = genNonceString();
- const timestamp = Math.floor(Date.now() / 1000);
- const data = config.url.toLowerCase() + timestamp;
- const signature = crypto_1.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_1.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) {
- return this.request({
- method: 'POST',
- url: '/api/ids/getCrawlAuthors',
- data: {
- code: channel
- }
- });
- }
- getCrawlArticleRules(params) {
- const { ids, channel } = params;
- return this.request({
- method: 'POST',
- url: '/api/ids/getCrawlArticleRules',
- data: {
- code: channel,
- sn_codes: ids
- }
- });
- }
- putArticle(data, rule) {
- data.external_read_num = data.external_read_num || 0;
- data.external_see_num = data.external_see_num || 0;
- data.external_like_num = data.external_like_num || 0;
- data.external_comment_num = data.external_comment_num || 0;
- return this.request({
- method: 'POST',
- url: `/api/ids/putArticleData`,
- params: {
- crawl: rule
- },
- data
- });
- }
- putProvider(data) {
- return this.request({
- method: 'POST',
- url: `/api/ids/putSourceData`,
- data
- });
- }
- putSolution(data) {
- data.ext_sold = data.ext_sold || 0;
- data.ext_score = data.ext_score || 0;
- if (data.extra && typeof data.extra === 'object') {
- data.extra = JSON.stringify(data.extra);
- }
- return this.request({
- method: 'POST',
- url: `/api/ids/putSolutionData`,
- data
- });
- }
- createUser(data) {
- return this.request({
- method: 'POST',
- url: `/api/ids/createUser`,
- data
- });
- }
- }
- exports.Ids = Ids;
|