_lib.request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const iconv = require('iconv-lite');
  2. async function requestHtml(url, options) {
  3. try {
  4. let html = (await req(url, options)).content;
  5. // log(html);
  6. return html
  7. } catch (e) {
  8. log(`requestHtml error:${e.message}`);
  9. return ''
  10. }
  11. }
  12. async function requestJson(url, options) {
  13. try {
  14. let html = (await req(url, options)).content;
  15. return JSON.parse(html)
  16. } catch (e) {
  17. log(`requestJson error:${e.message}`);
  18. return {}
  19. }
  20. }
  21. async function getPublicIp() {
  22. let ip_obj = await requestJson('http://httpbin.org/ip');
  23. // log('ip_obj:',ip_obj);
  24. return ip_obj.origin
  25. }
  26. async function getHtml(config) {
  27. try {
  28. return await axios.request(typeof config === "string" ? config : {
  29. url: config.url,
  30. method: config.method || 'GET',
  31. headers: config.headers || {
  32. 'User-Agent': PC_UA
  33. },
  34. data: config.data || '',
  35. responseType: config.responseType || '',//'arraybuffer'
  36. })
  37. } catch (e) {
  38. return e.response
  39. }
  40. }
  41. async function req_(reqUrl, mt, headers, data) {
  42. let config = {
  43. method: mt || 'Get',
  44. url: reqUrl,
  45. headers: headers || {
  46. 'User-Agent': 'Mozilla/5.0 (Linux; Android 9; RMX1931 Build/PQ3A.190605.05081124; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36 QSTAPP/1.6.9 Html5Plus/1.0',
  47. },
  48. data: data || '',
  49. };
  50. let res = await axios.request(config);
  51. return res.data;
  52. }
  53. async function req_encoding(reqUrl, mt, headers, encoding, data) {
  54. let config = {
  55. method: mt || 'Get',
  56. url: reqUrl,
  57. headers: headers || {
  58. 'User-Agent': 'Mozilla/5.0 (Linux; Android 9; RMX1931 Build/PQ3A.190605.05081124; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36 QSTAPP/1.6.9 Html5Plus/1.0',
  59. },
  60. data: data || '',
  61. responseType: 'arraybuffer'
  62. };
  63. let res = await axios.request(config);
  64. if (encoding) {
  65. res.data = iconv.decode(res.data, encoding);
  66. }
  67. return res.data;
  68. }
  69. async function req_proxy(reqUrl, mt, headers, data) {
  70. let config = {
  71. method: mt || 'Get',
  72. url: reqUrl,
  73. headers: headers || {
  74. 'User-Agent': 'Mozilla/5.0 (Linux; Android 9; RMX1931 Build/PQ3A.190605.05081124; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36 QSTAPP/1.6.9 Html5Plus/1.0',
  75. },
  76. proxy: {
  77. protocol: 'http',
  78. host: '127.0.0.1',
  79. port: "7890"
  80. }
  81. };
  82. if (data) {
  83. config.data = data;
  84. }
  85. let res = await axios.request(config);
  86. return res.data;
  87. }
  88. $.exports = {
  89. requestHtml,
  90. requestJson,
  91. getPublicIp,
  92. getHtml,
  93. req_,
  94. req_encoding,
  95. req_proxy,
  96. // axios // 没法import系统库
  97. }