ids.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Ids = exports.IdsChannel = void 0;
  4. const request_1 = require("./request");
  5. const crypto_1 = require("crypto");
  6. var IdsChannel;
  7. (function (IdsChannel) {
  8. IdsChannel[IdsChannel["Csdn"] = 1] = "Csdn";
  9. IdsChannel[IdsChannel["Wechat"] = 2] = "Wechat";
  10. IdsChannel[IdsChannel["Zhihu"] = 3] = "Zhihu";
  11. IdsChannel[IdsChannel["Juejin"] = 4] = "Juejin";
  12. })(IdsChannel = exports.IdsChannel || (exports.IdsChannel = {}));
  13. class Ids {
  14. constructor(config) {
  15. this.config = config;
  16. }
  17. async request(config) {
  18. const { endpoint, accessKeyId, accessKeySecret } = this.config;
  19. config.baseURL = endpoint;
  20. config.url = config.url || '/';
  21. const timestamp = Math.floor(Date.now() / 1000);
  22. const data = config.url.toLowerCase() + timestamp;
  23. const signature = crypto_1.createHmac('sha256', accessKeySecret).update(data).digest('base64');
  24. config.headers = config.headers || {};
  25. config.headers['Authorization'] = `IDS-HMAC-SHA256 Credential=${accessKeyId}/${timestamp},Signature=${signature}`;
  26. const res = await request_1.request(config);
  27. if (res.data.status != 1) {
  28. console.error(res.data);
  29. throw new Error(`[${res.data.status}] ${res.data.info}`);
  30. }
  31. return res.data;
  32. }
  33. getCrawlAuthors(channel) {
  34. return this.request({
  35. method: 'POST',
  36. url: '/api/ids/getCrawlAuthors',
  37. data: {
  38. code: channel
  39. }
  40. });
  41. }
  42. getCrawlArticleRules(params) {
  43. const { ids, channel } = params;
  44. return this.request({
  45. method: 'POST',
  46. url: '/api/ids/getCrawlArticleRules',
  47. data: {
  48. code: channel,
  49. sn_codes: ids
  50. }
  51. });
  52. }
  53. putArticle(data, rule) {
  54. return this.request({
  55. method: 'POST',
  56. url: '/api/ids/putArticleData',
  57. params: {
  58. crawl: rule
  59. },
  60. data
  61. });
  62. }
  63. }
  64. exports.Ids = Ids;