-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
124 lines (100 loc) · 3.31 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import pup from "puppeteer";
import qs from "qs";
import express from "express";
import { Tagged, Untagged } from "./tagged";
import { match } from "ts-pattern";
const downloadButton = "//button[contains(., 'Download')]";
type zipFileUrlObjec = {
url: string;
method: string;
headers: Record<string, string>;
postData: Record<string, string>;
};
namespace Link {
export interface Zip extends Tagged<"Zip"> {
urlObject: zipFileUrlObjec;
}
export const Zip = (spec: Untagged<Zip>): Zip => ({ ...spec, _tag: "Zip" });
export interface SingleFile extends Tagged<"SingleFile"> {
url: string;
headers: Record<string, string>;
}
export const SingleFile = (spec: Untagged<SingleFile>): SingleFile => ({
...spec,
_tag: "SingleFile",
});
}
type Link = Link.Zip | Link.SingleFile;
const getZipFileUrlObject = async (
url: string,
onFoundLink: (link: Link) => void
) => {
const browser = await pup.launch({ headless: false });
const page = await browser.newPage();
await page.setViewport({ width: 2000, height: 1024 });
await page.setRequestInterception(true);
page.on("request", (interceptedRequest) => {
if (interceptedRequest.isInterceptResolutionHandled()) return;
const url_ = interceptedRequest.url();
if (url_.includes("zip")) {
const obj = {
url: interceptedRequest.url(),
method: interceptedRequest.method(),
headers: interceptedRequest.headers(),
postData: interceptedRequest.postData(),
};
onFoundLink(
Link.Zip({
urlObject: {
...obj,
postData: qs.parse(obj.postData!.toString()) as Record<
string,
string
>,
},
})
);
interceptedRequest.abort();
browser.close();
} else if (url_.includes("download")) {
const headers = interceptedRequest.headers();
onFoundLink(Link.SingleFile({ url: url_, headers }));
interceptedRequest.abort();
browser.close();
} else if (
url_.endsWith(".png") ||
url_.endsWith(".svg") ||
url_.endsWith(".woff") ||
url_.endsWith(".jpg")
) {
interceptedRequest.abort();
} else {
interceptedRequest.continue();
}
});
await page.goto(url);
await page.waitForXPath(downloadButton);
let button = (await page.$x(downloadButton))[0];
await (await button.toElement("button")).click();
};
const mkPythonGetRequest = (url: string, headers: Record<string, string>) =>
`requests.get('${url}', headers=${JSON.stringify(headers)})`;
const mkPythonPostRequest = (urlObject: zipFileUrlObjec) =>
`requests.post('${urlObject.url}', headers=${JSON.stringify(
urlObject.headers
)}, data=${JSON.stringify(urlObject.postData)})`;
const app = express();
app.use(express.urlencoded({ extended: true }));
app.post<never, any, { url: string }>("/download", (req, res) => {
getZipFileUrlObject(req.body.url, (link) =>
match(link)
.with({ _tag: "SingleFile" }, ({ url, headers }) =>
res.send({ url, headers, METHOD: "GET" })
)
.with({ _tag: "Zip" }, ({ urlObject }) =>
res.send({ ...urlObject, METHOD: "POST" })
)
);
});
// "https://uwin365.sharepoint.com/:f:/s/cshfrg-TeamFormation/Egwn-uzzJtNEsTM3cKOpM2gBQpcF3_qrZcs-DzIg_j7hsg"
app.listen(3000, () => console.log("listening on 3000"));