ids.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 genNonceString = () => crypto_1.randomBytes(8).toString('hex');
  24. const randomtail = () => `_=${genNonceString()}`;
  25. exports.randomtail = randomtail;
  26. class Ids {
  27. constructor(config) {
  28. this.config = config;
  29. }
  30. async request(config) {
  31. const { endpoint, accessKeyId, accessKeySecret } = this.config;
  32. config.baseURL = endpoint;
  33. config.url = config.url || '/';
  34. config.params = config.params || {};
  35. config.params.nonce = genNonceString();
  36. const timestamp = Math.floor(Date.now() / 1000);
  37. const data = config.url.toLowerCase() + timestamp;
  38. const signature = crypto_1.createHmac('sha256', accessKeySecret).update(data).digest('base64');
  39. config.headers = config.headers || {};
  40. config.headers['Authorization'] = `IDS-HMAC-SHA256 Credential=${accessKeyId}/${timestamp},Signature=${signature}`;
  41. const res = await request_1.request(config);
  42. if (res.data.status != 1) {
  43. console.error(res.data);
  44. throw new Error(`[${res.data.status}] ${res.data.info}`);
  45. }
  46. return res.data;
  47. }
  48. getCrawlAuthors(channel) {
  49. return this.request({
  50. method: 'POST',
  51. url: '/api/ids/getCrawlAuthors',
  52. data: {
  53. code: channel
  54. }
  55. });
  56. }
  57. getCrawlArticleRules(params) {
  58. const { ids, channel } = params;
  59. return this.request({
  60. method: 'POST',
  61. url: '/api/ids/getCrawlArticleRules',
  62. data: {
  63. code: channel,
  64. sn_codes: ids
  65. }
  66. });
  67. }
  68. putArticle(data, rule) {
  69. data.external_read_num = data.external_read_num || 0;
  70. data.external_see_num = data.external_see_num || 0;
  71. data.external_like_num = data.external_like_num || 0;
  72. data.external_comment_num = data.external_comment_num || 0;
  73. return this.request({
  74. method: 'POST',
  75. url: `/api/ids/putArticleData`,
  76. params: {
  77. crawl: rule
  78. },
  79. data
  80. });
  81. }
  82. putProvider(data) {
  83. return this.request({
  84. method: 'POST',
  85. url: `/api/ids/putSourceData`,
  86. data
  87. });
  88. }
  89. putSolution(data) {
  90. data.ext_sold = data.ext_sold || 0;
  91. data.ext_score = data.ext_score || 0;
  92. if (data.extra && typeof data.extra === 'object') {
  93. data.extra = JSON.stringify(data.extra);
  94. }
  95. return this.request({
  96. method: 'POST',
  97. url: `/api/ids/putSolutionData`,
  98. data
  99. });
  100. }
  101. createUser(data) {
  102. return this.request({
  103. method: 'POST',
  104. url: `/api/ids/createUser`,
  105. data
  106. });
  107. }
  108. }
  109. exports.Ids = Ids;