ids.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Ids = exports.randomtail = exports.IdsSource = 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. var IdsSource;
  14. (function (IdsSource) {
  15. IdsSource[IdsSource["Manual"] = 0] = "Manual";
  16. IdsSource[IdsSource["User"] = 1] = "User";
  17. IdsSource[IdsSource["AliyunMarket"] = 2] = "AliyunMarket";
  18. IdsSource[IdsSource["TencentCloudMarket"] = 3] = "TencentCloudMarket";
  19. IdsSource[IdsSource["HuaweiCloudMarket"] = 4] = "HuaweiCloudMarket";
  20. IdsSource[IdsSource["Xiniu"] = 6] = "Xiniu";
  21. IdsSource[IdsSource["Website"] = 7] = "Website";
  22. })(IdsSource = exports.IdsSource || (exports.IdsSource = {}));
  23. const randomtail = () => {
  24. return `_=${crypto_1.randomBytes(8).toString('hex')}`;
  25. };
  26. exports.randomtail = randomtail;
  27. class Ids {
  28. constructor(config) {
  29. this.config = config;
  30. }
  31. async request(config) {
  32. const { endpoint, accessKeyId, accessKeySecret } = this.config;
  33. config.baseURL = endpoint;
  34. config.url = config.url || '/';
  35. const timestamp = Math.floor(Date.now() / 1000);
  36. const data = config.url.toLowerCase() + timestamp;
  37. const signature = crypto_1.createHmac('sha256', accessKeySecret).update(data).digest('base64');
  38. config.headers = config.headers || {};
  39. config.headers['Authorization'] = `IDS-HMAC-SHA256 Credential=${accessKeyId}/${timestamp},Signature=${signature}`;
  40. const res = await request_1.request(config);
  41. if (res.data.status != 1) {
  42. console.error(res.data);
  43. throw new Error(`[${res.data.status}] ${res.data.info}`);
  44. }
  45. return res.data;
  46. }
  47. getCrawlAuthors(channel) {
  48. return this.request({
  49. method: 'POST',
  50. url: '/api/ids/getCrawlAuthors',
  51. data: {
  52. code: channel
  53. }
  54. });
  55. }
  56. getCrawlArticleRules(params) {
  57. const { ids, channel } = params;
  58. return this.request({
  59. method: 'POST',
  60. url: '/api/ids/getCrawlArticleRules',
  61. data: {
  62. code: channel,
  63. sn_codes: ids
  64. }
  65. });
  66. }
  67. putArticle(data, rule) {
  68. data.external_read_num = data.external_read_num || 0;
  69. data.external_see_num = data.external_see_num || 0;
  70. data.external_like_num = data.external_like_num || 0;
  71. data.external_comment_num = data.external_comment_num || 0;
  72. return this.request({
  73. method: 'POST',
  74. url: `/api/ids/putArticleData?crawl=${rule}&${exports.randomtail()}`,
  75. data
  76. });
  77. }
  78. putProvider(data) {
  79. return this.request({
  80. method: 'POST',
  81. url: `/api/ids/putSourceData?${exports.randomtail()}`,
  82. data
  83. });
  84. }
  85. putSolution(data) {
  86. data.ext_sold = data.ext_sold || 0;
  87. data.ext_score = data.ext_score || 0;
  88. if (data.extra && typeof data.extra === 'object') {
  89. data.extra = JSON.stringify(data.extra);
  90. }
  91. return this.request({
  92. method: 'POST',
  93. url: `/api/ids/putSolutionData?${exports.randomtail()}`,
  94. data
  95. });
  96. }
  97. }
  98. exports.Ids = Ids;