ids.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.Ids = exports.randomtail = exports.IdsSource = exports.IdsChannel = void 0;
  7. const request_1 = require("./request");
  8. const crypto_1 = require("crypto");
  9. const querystring_1 = __importDefault(require("querystring"));
  10. var IdsChannel;
  11. (function (IdsChannel) {
  12. IdsChannel[IdsChannel["Csdn"] = 1] = "Csdn";
  13. IdsChannel[IdsChannel["Wechat"] = 2] = "Wechat";
  14. IdsChannel[IdsChannel["Zhihu"] = 3] = "Zhihu";
  15. IdsChannel[IdsChannel["Juejin"] = 4] = "Juejin";
  16. })(IdsChannel = exports.IdsChannel || (exports.IdsChannel = {}));
  17. var IdsSource;
  18. (function (IdsSource) {
  19. IdsSource[IdsSource["Manual"] = 0] = "Manual";
  20. IdsSource[IdsSource["User"] = 1] = "User";
  21. IdsSource[IdsSource["AliyunMarket"] = 2] = "AliyunMarket";
  22. IdsSource[IdsSource["TencentCloudMarket"] = 3] = "TencentCloudMarket";
  23. IdsSource[IdsSource["HuaweiCloudMarket"] = 4] = "HuaweiCloudMarket";
  24. IdsSource[IdsSource["Xiniu"] = 6] = "Xiniu";
  25. IdsSource[IdsSource["Website"] = 7] = "Website";
  26. })(IdsSource = exports.IdsSource || (exports.IdsSource = {}));
  27. const genNonceString = () => crypto_1.randomBytes(8).toString('hex');
  28. const randomtail = () => `_=${genNonceString()}`;
  29. exports.randomtail = randomtail;
  30. class Ids {
  31. constructor(config) {
  32. this.config = config;
  33. }
  34. async request(config) {
  35. const { endpoint, accessKeyId, accessKeySecret } = this.config;
  36. const querystr = querystring_1.default.stringify({
  37. nonce: genNonceString(),
  38. ...config.params
  39. });
  40. config.baseURL = endpoint;
  41. config.url = (config.url || '/') + (querystr ? `?${querystr}` : '');
  42. config.params = {};
  43. const timestamp = Math.floor(Date.now() / 1000);
  44. const data = config.url.toLowerCase() + timestamp;
  45. const signature = crypto_1.createHmac('sha256', accessKeySecret).update(data).digest('base64');
  46. config.headers = config.headers || {};
  47. config.headers['Authorization'] = `IDS-HMAC-SHA256 Credential=${accessKeyId}/${timestamp},Signature=${signature}`;
  48. const res = await request_1.request(config);
  49. if (res.data.status != 1) {
  50. console.error(res.data);
  51. throw new Error(`[${res.data.status}] ${res.data.info}`);
  52. }
  53. return res.data;
  54. }
  55. getCrawlAuthors(channel) {
  56. return this.request({
  57. method: 'POST',
  58. url: '/api/ids/getCrawlAuthors',
  59. data: {
  60. code: channel
  61. }
  62. });
  63. }
  64. getCrawlArticleRules(params) {
  65. const { ids, channel } = params;
  66. return this.request({
  67. method: 'POST',
  68. url: '/api/ids/getCrawlArticleRules',
  69. data: {
  70. code: channel,
  71. sn_codes: ids
  72. }
  73. });
  74. }
  75. putArticle(data, rule) {
  76. data.external_read_num = data.external_read_num || 0;
  77. data.external_see_num = data.external_see_num || 0;
  78. data.external_like_num = data.external_like_num || 0;
  79. data.external_comment_num = data.external_comment_num || 0;
  80. return this.request({
  81. method: 'POST',
  82. url: `/api/ids/putArticleData`,
  83. params: {
  84. crawl: rule
  85. },
  86. data
  87. });
  88. }
  89. putProvider(data, config) {
  90. return this.request({
  91. ...config,
  92. method: 'POST',
  93. url: `/api/ids/putSourceData`,
  94. data
  95. });
  96. }
  97. putSolution(data, config) {
  98. data.ext_sold = data.ext_sold || 0;
  99. data.ext_score = data.ext_score || 0;
  100. if (data.extra && typeof data.extra === 'object') {
  101. data.extra = JSON.stringify(data.extra);
  102. }
  103. return this.request({
  104. ...config,
  105. method: 'POST',
  106. url: `/api/ids/putSolutionData`,
  107. data
  108. });
  109. }
  110. createUser(data, config) {
  111. return this.request({
  112. ...config,
  113. method: 'POST',
  114. url: `/api/ids/createUser`,
  115. data
  116. });
  117. }
  118. login(data, config) {
  119. return this.request({
  120. ...config,
  121. method: 'POST',
  122. url: `/api/ids/login`,
  123. data
  124. });
  125. }
  126. }
  127. exports.Ids = Ids;