-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
174 lines (149 loc) · 6.44 KB
/
app.js
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const express = require('express');
const app = express();
// for view rendering
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/views'));
// for webcrawler
const puppeteer = require('puppeteer');
const port = process.env.PORT || 8080;
const validUrl = require('valid-url');
const path = require('path');
var browser = null;
var firstLogin = true;
// generate random number
function between(min, max) {
return Math.floor(
Math.random() * (max - min) + min
)
}
// Get random puml file address (github raw usercontent) recently edited in github
async function getUrl() {
try {
// use existed browser
const page = await browser.newPage();
console.log("Step 1 page opened");
let githubUrl = null;
let rawContentUrl = null;
try {
// must guarantee that already logined
await page.goto("https://github.com");
await page.waitForSelector('body > div.position-relative.js-header-wrapper > header > div.Header-item.mr-0.mr-md-3.flex-order-1.flex-md-order-none > notification-indicator > a > svg', {visible: true, timeout: 10000 });
console.log("Step 2 logined");
// search puml file with random page
// sometimes this page is rendered without result list, retry should be set
const randomUrl = "https://github.com/search?l=&o=desc&p=" + between(1, 100) + "&q=language%3APlantUML&s=indexed&type=Code";
const fileItemSelector = "#code_search_results > div.code-list > div:nth-child(" + between(1, 10) + ") > div > div.f4.text-normal > a";
try {
await page.goto(randomUrl);
await page.waitForSelector(fileItemSelector, {visible: true, timeout: 5000 });
} catch (e) {
console.log("search page go wrong without result list, try again");
await page.goto(randomUrl);
await page.waitForSelector(fileItemSelector, {visible: true, timeout: 5000 });
}
await page.$eval(fileItemSelector, el => el.click());
await page.waitForSelector('#raw-url', {visible: true, timeout: 10000 })
githubUrl = page.url();
await page.$eval('#raw-url', el => el.click());
rawContentUrl = page.url();
console.log("Step 3 searched");
} catch (e) {
console.log("cur url: ", page.url());
console.log("exception: " + e);
throw e;
} finally {
await page.close();
console.log("Step 4 closed");
}
return [true, githubUrl, rawContentUrl];
} catch (e) {
console.log("exception: " + e);
return [false,
"https://github.com/LangInteger/one-shot-plantuml/blob/main/docs/500.puml",
"https://raw.githubusercontent.com/LangInteger/one-shot-plantuml/main/docs/500.puml"];
}
}
app.get('/getUrl', async function(req, res) {
const result = await getUrl();
res.send(result);
});
app.get('/', async function(req, res) {
let ret = await getUrl();
console.log("url: " + ret);
res.render('index', {url: ret[2], githubUrl: ret[1]});
});
// only login, and consider verification code
app.get('/login', async function(req, res) {
if (browser == null) {
browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox', '--user-agent=Mozilla/5.0 (Windows NT 10.0; WOW 64; Trident/7.0; rv:11.0) like Gecko']
// headless: false
});
} else {
// browser once created, so it is not first login
firstLogin = false;
}
const page = await browser.newPage();
console.log("Step 1 page opened");
try {
// login
await page.goto("https://github.com/login");
// it seems that there is no good clojure support
const username = process.env.GITHUB_USERNAME;
await page.evaluate((username) => {
const target = document.querySelector('#login_field');
target.value = username;
}, username);
const password = process.env.GITHUB_PASSWORD;
await page.evaluate((password) => {
const target = document.querySelector('#password');
target.value = password;
}, password);
await page.$eval('#login > div.auth-form-body.mt-3 > form > div > input.btn.btn-primary.btn-block.js-sign-in-button', el => el.click());
try {
// verification code
await page.waitForSelector('#otp', {visible: true, timeout: 5000 })
console.log('otp appears');
await new Promise(resolve => setTimeout(resolve, 30000));
const code = process.env.CODE;
console.log("verification code: " + code);
await page.evaluate((code) => {
const target = document.querySelector('#otp');
target.value = code;
}, code);
console.log("input verification code: ", code);
await page.$eval('#login > div.auth-form-body.mt-3 > form > button', el => el.click());
console.log("click verificate button");
await page.waitForSelector('body > div.position-relative.js-header-wrapper > header > div.Header-item.mr-0.mr-md-3.flex-order-1.flex-md-order-none > notification-indicator > a > svg', {visible: true, timeout: 10000 });
console.log("Step 2 logined");
} catch (e) {
// verification may not needed, continue
console.log("No verificate page, cur url: ", page.url());
console.log("exception: " + e);
}
} catch (e) {
console.log("cur url: ", page.url());
console.log("exception: " + e);
if (firstLogin) {
console.log("first login and exception happenes in the process, return login exception");
throw e;
} else {
console.log("exception happen in not first login, maybe it has logined before, return login success");
}
} finally {
await page.close();
console.log("Step 3 page closed");
}
res.send("login success");
});
// for save verificate code temporarily
app.get('/setCode', async function(req, res) {
process.env.CODE = req.query.code;
res.send("set success");
});
app.get('/getCode', async function(req, res) {
res.send(process.env.CODE);
});
app.listen(port, function() {
console.log('App listening on port ' + port)
})