|
import axios from 'axios'; |
|
import fetch from 'node-fetch'; |
|
import iconv from 'iconv-lite'; |
|
import fs from 'fs-extra'; |
|
import { wrapper } from 'axios-cookiejar-support'; |
|
import { CookieJar, Cookie } from 'tough-cookie'; |
|
import {SocksProxyAgent} from 'socks-proxy-agent'; |
|
const proxyAgent = new SocksProxyAgent(`socks5://127.0.0.1:9909`); |
|
|
|
export const jar = fs.existsSync("./cookies.json") |
|
? CookieJar.fromJSON(JSON.parse(fs.readFileSync("./cookies.json").toString())) |
|
: new CookieJar(); |
|
jar.rejectPublicSuffixes = false; |
|
jar.looseMode = true; |
|
jar.prefixSecurity ='unsafe-disabled'; |
|
jar.allowSpecialUseDomain = true; |
|
|
|
const client = wrapper(axios.create({ jar })); |
|
|
|
export function reset_cookie_jar() { |
|
jar.removeAllCookiesSync(); |
|
} |
|
|
|
client.interceptors.request.use(function (config) { |
|
if (config.method === 'get') { |
|
console.log('Get ', config.url); |
|
} else if (config.method === 'post') { |
|
console.log('Post ', config.url, config.data); |
|
} |
|
config.headers['User-Agent']='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'; |
|
config.maxRedirects = 5; |
|
|
|
return config; |
|
}, function (error) { |
|
return Promise.reject(error); |
|
}); |
|
|
|
export function gb2312UrlEncode(str) { |
|
const encoded = iconv.encode(str, 'GB2312'); |
|
let result = ''; |
|
for (let i = 0; i < encoded.length; i++) { |
|
result += `%${encoded[i].toString(16)}`; |
|
} |
|
return result.toUpperCase(); |
|
|
|
} |
|
|
|
export function chineseToUnicode(str, toUpper = false) { |
|
let result = ""; |
|
for (let i = 0; i < str.length; i++) { |
|
let charCode = str.charCodeAt(i).toString(16).padStart(4, '0'); |
|
result += "%u" + (toUpper ? charCode.toUpperCase() : charCode); |
|
} |
|
return result; |
|
} |
|
|
|
export function unicodeToChinese(str) { |
|
return unescape(str); |
|
} |
|
|
|
|
|
export const get = async (url, from_gb2312 = false) => { |
|
const res = await axios.get(url, { |
|
httpAgent: proxyAgent, |
|
httpsAgent: proxyAgent, |
|
}); |
|
return res; |
|
} |
|
|
|
export const post = async (url, data) => { |
|
const res = await axios.post(url, data, { |
|
httpAgent: proxyAgent, |
|
httpsAgent: proxyAgent, |
|
}).catch((e) => { |
|
debugger; |
|
}); |
|
return res; |
|
}; |
|
|
|
export const downloadFile = async (url, filePath, maxRetries) => { |
|
let t = 0; |
|
while (t < maxRetries) { |
|
try { |
|
const res = await axios({ |
|
httpAgent: proxyAgent, |
|
httpsAgent: proxyAgent, |
|
method: "get", |
|
url: url, |
|
responseType: "arraybuffer", |
|
}); |
|
if (!res.data.length) { |
|
throw Error("0byte"); |
|
} |
|
if (res.status !== 200) { |
|
throw Error("error status"); |
|
} |
|
if (res.data.toString().indexOf('The system is busy. Please retry later') != -1) { |
|
throw Error("busy"); |
|
} |
|
fs.writeFileSync(filePath, res.data); |
|
return; |
|
} catch (e) { |
|
console.error("Error downloading file:", e, url); |
|
if (fs.existsSync(filePath)) fs.unlinkSync(filePath); |
|
} |
|
++t; |
|
console.log('retry', t); |
|
await sleep(3000); |
|
} |
|
throw new Error('max retries exceeded'); |
|
}; |
|
|
|
export function parseCookies(cookie_raw) { |
|
return cookie_raw.split(";").reduce((m, i) => { |
|
i = i.trim(); |
|
const idx = i.indexOf("="); |
|
const name = i.substr(0, idx); |
|
const value = i.substr(idx + 1); |
|
m[name] = value; |
|
return m; |
|
}, {}); |
|
} |
|
|
|
export const sleep = (t) => new Promise((resolve) => setTimeout(resolve, t)); |