3v324v23 commited on
Commit
977e79d
·
1 Parent(s): 5b0bc91
Files changed (5) hide show
  1. .gitattributes +19 -0
  2. .gitignore +5 -0
  3. package-lock.json +3 -0
  4. package.json +3 -0
  5. utils.js +124 -0
.gitattributes CHANGED
@@ -51,5 +51,24 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
51
  *.tiff filter=lfs diff=lfs merge=lfs -text
52
  # Image files - compressed
53
  *.jpg filter=lfs diff=lfs merge=lfs -text
 
54
  *.jpeg filter=lfs diff=lfs merge=lfs -text
55
  *.webp filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  *.tiff filter=lfs diff=lfs merge=lfs -text
52
  # Image files - compressed
53
  *.jpg filter=lfs diff=lfs merge=lfs -text
54
+ *.JPG filter=lfs diff=lfs merge=lfs -text
55
  *.jpeg filter=lfs diff=lfs merge=lfs -text
56
  *.webp filter=lfs diff=lfs merge=lfs -text
57
+ *. filter=lfs diff=lfs merge=lfs -text
58
+ *.pdf filter=lfs diff=lfs merge=lfs -text
59
+ *.FPT filter=lfs diff=lfs merge=lfs -text
60
+ *.dbf filter=lfs diff=lfs merge=lfs -text
61
+ *.cdx filter=lfs diff=lfs merge=lfs -text
62
+ *.CDX filter=lfs diff=lfs merge=lfs -text
63
+ *.epub filter=lfs diff=lfs merge=lfs -text
64
+ *.json filter=lfs diff=lfs merge=lfs -text
65
+ *.html filter=lfs diff=lfs merge=lfs -text
66
+ *.htm filter=lfs diff=lfs merge=lfs -text
67
+ *.mht filter=lfs diff=lfs merge=lfs -text
68
+ *.mhtml filter=lfs diff=lfs merge=lfs -text
69
+ *.doc filter=lfs diff=lfs merge=lfs -text
70
+ *.docm filter=lfs diff=lfs merge=lfs -text
71
+ *.docx filter=lfs diff=lfs merge=lfs -text
72
+ *.TIF filter=lfs diff=lfs merge=lfs -text
73
+ *.chm filter=lfs diff=lfs merge=lfs -text
74
+ *.csv filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ node_modules
2
+ cookies.json
3
+ .DS_Store
4
+ ._.DS_Store
5
+ .vscode
package-lock.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:75c29c58f88bb3f6c5d87b36cddec01bda197adba188fe532f29244583370138
3
+ size 80971
package.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:251c584a33aefdf917d52576d044483e7cf34e10f5fbef0185e2ea0c63964e53
3
+ size 801
utils.js ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import axios from 'axios';
2
+ import fetch from 'node-fetch';
3
+ import iconv from 'iconv-lite';
4
+ import fs from 'fs-extra';
5
+ import { wrapper } from 'axios-cookiejar-support';
6
+ import { CookieJar, Cookie } from 'tough-cookie';
7
+ import {SocksProxyAgent} from 'socks-proxy-agent';
8
+ const proxyAgent = new SocksProxyAgent(`socks5://127.0.0.1:9909`);
9
+
10
+ export const jar = fs.existsSync("./cookies.json")
11
+ ? CookieJar.fromJSON(JSON.parse(fs.readFileSync("./cookies.json").toString()))
12
+ : new CookieJar();
13
+ jar.rejectPublicSuffixes = false;
14
+ jar.looseMode = true;
15
+ jar.prefixSecurity ='unsafe-disabled';
16
+ jar.allowSpecialUseDomain = true;
17
+
18
+ const client = wrapper(axios.create({ jar }));
19
+
20
+ export function reset_cookie_jar() {
21
+ jar.removeAllCookiesSync();
22
+ }
23
+
24
+ client.interceptors.request.use(function (config) {
25
+ if (config.method === 'get') {
26
+ console.log('Get ', config.url);
27
+ } else if (config.method === 'post') {
28
+ console.log('Post ', config.url, config.data);
29
+ }
30
+ 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';
31
+ config.maxRedirects = 5;
32
+
33
+ return config;
34
+ }, function (error) {
35
+ return Promise.reject(error);
36
+ });
37
+
38
+ export function gb2312UrlEncode(str) {
39
+ const encoded = iconv.encode(str, 'GB2312');
40
+ let result = '';
41
+ for (let i = 0; i < encoded.length; i++) {
42
+ result += `%${encoded[i].toString(16)}`;
43
+ }
44
+ return result.toUpperCase();
45
+
46
+ }
47
+
48
+ export function chineseToUnicode(str, toUpper = false) {
49
+ let result = "";
50
+ for (let i = 0; i < str.length; i++) {
51
+ let charCode = str.charCodeAt(i).toString(16).padStart(4, '0');
52
+ result += "%u" + (toUpper ? charCode.toUpperCase() : charCode);
53
+ }
54
+ return result;
55
+ }
56
+
57
+ export function unicodeToChinese(str) {
58
+ return unescape(str);
59
+ }
60
+
61
+
62
+ export const get = async (url, from_gb2312 = false) => {
63
+ const res = await axios.get(url, {
64
+ httpAgent: proxyAgent,
65
+ httpsAgent: proxyAgent,
66
+ });
67
+ return res;
68
+ }
69
+
70
+ export const post = async (url, data) => {
71
+ const res = await axios.post(url, data, {
72
+ httpAgent: proxyAgent,
73
+ httpsAgent: proxyAgent,
74
+ }).catch((e) => {
75
+ debugger;
76
+ });
77
+ return res;
78
+ };
79
+
80
+ export const downloadFile = async (url, filePath, maxRetries) => {
81
+ let t = 0;
82
+ while (t < maxRetries) {
83
+ try {
84
+ const res = await axios({
85
+ httpAgent: proxyAgent,
86
+ httpsAgent: proxyAgent,
87
+ method: "get",
88
+ url: url,
89
+ responseType: "arraybuffer",
90
+ });
91
+ if (!res.data.length) {
92
+ throw Error("0byte");
93
+ }
94
+ if (res.status !== 200) {
95
+ throw Error("error status");
96
+ }
97
+ if (res.data.toString().indexOf('The system is busy. Please retry later') != -1) {
98
+ throw Error("busy");
99
+ }
100
+ fs.writeFileSync(filePath, res.data);
101
+ return;
102
+ } catch (e) {
103
+ console.error("Error downloading file:", e, url);
104
+ if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
105
+ }
106
+ ++t;
107
+ console.log('retry', t);
108
+ await sleep(3000);
109
+ }
110
+ throw new Error('max retries exceeded');
111
+ };
112
+
113
+ export function parseCookies(cookie_raw) {
114
+ return cookie_raw.split(";").reduce((m, i) => {
115
+ i = i.trim();
116
+ const idx = i.indexOf("=");
117
+ const name = i.substr(0, idx);
118
+ const value = i.substr(idx + 1);
119
+ m[name] = value;
120
+ return m;
121
+ }, {});
122
+ }
123
+
124
+ export const sleep = (t) => new Promise((resolve) => setTimeout(resolve, t));