-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
180 lines (145 loc) · 5.24 KB
/
bot.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
175
176
177
178
179
180
const puppeteer = require('puppeteer');
const fs = require('fs');
const db = require('./db.js');
const mail = require('./mail.js')
const noti = require('./noti.js');
const key = require('./key');
const options = {
'headless': true, // no gui browser
'userDataDir':"./data" // save browser data,session,cookie
};
const authInfo = JSON.parse(fs.readFileSync('./config.json','utf8'));
const logined = true;
const account = authInfo.account;
const password = key.decrypt(authInfo.password);
(async () => {
try{
const browser = await puppeteer.launch(options);
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36');
await page.setViewport({ width: 1280, height: 400 })
browser.addListener('targetchanged',function(event){
});
let firstPage = 'https://gengo.com/c/dashboard/jobs/approved/'
if(!logined) {
firstPage = 'https://gengo.com/auth/form/login/';
}
await page.goto(firstPage);
await page.waitFor(500);
if(!logined){
console.log('try login');
await page.waitForSelector('.form-group');
await page.type('.form-control', account)
await page.type('.real-password', password)
await page.click('.btn-primary')
}
await page.waitForSelector('.large-details-link');
// list all
let contextList = await page.evaluate(() =>{
let list = [];
document.querySelectorAll('.approved .large-details-link').forEach( item=>{
let url = item.getAttribute("href");
list.push(url);
});
return list;
})
console.log(contextList);
for(let i=0;i<contextList.length;i++){
let url = contextList[i];
const ids = url.split("/").slice(-2)[0];
try{
let result = await db.checkIDNotExist(ids)
await handlePage(browser, page, url);
}catch(error) {
console.error(`exists:${ids}`);
}
}
await page.goto('https://gengo.com/c/dashboard/');
await browser.close();
}catch(e) {
console.error(e);
}
})();
async function handlePage(browser, page, url) {
const ids = url.split("/").slice(-2)[0];
await page.goto(url);
await page.waitForSelector('.receipt');
await page.waitFor(2000);
/*
will crash chrome. BUG....
await page.waitForSelector('.ui_btn.primary_btn',{timeout:2000})
await page.click(".ui_btn.primary_btn");
await page.waitFor(8000);
await page.click(".blue-box.clearfix .orange-link");
await page.waitFor(8000);
await handleReceiptPage(browser, page, ids);
*/
try{
const originalText = await page.evaluate( ()=>{
return document.querySelector('.job-content p').innerText
});
const originalLang = await page.evaluate( ()=>{
return document.querySelector('#original-text-header .job-language').innerText.toLowerCase().replace(/[^a-zA-Z ]/g, "");
})
if (!fs.existsSync(`./public/${ids}`)){
fs.mkdirSync(`./public/${ids}`);
}
fs.writeFileSync(`./public/${ids}/${originalLang}-${ids}.txt`,originalText);
const targetLang = await page.evaluate( ()=>{
return document.querySelector('.translation-preview .job-language').innerText.toLowerCase().replace(/[^a-zA-Z ]/g, "");
})
const targetText = await page.evaluate( ()=>{
return document.querySelector('.job-content .translation-preview-holder').innerText
});
fs.writeFileSync(`./public/${ids}/${targetLang}-${ids}.txt`,targetText);
} catch(e){
console.log(e);
}
await page.waitFor(4000);
await handleReceiptPage(browser, page, ids);
}
async function handleReceiptPage(browser, page, ids) {
await page.click('.receipt',{clickCount: 4})
await page.waitFor(1000);
let pageList = await browser.pages();
for(let i=0;i<pageList.length;i++) {
let subPage = pageList[i];
let SubTitle = await subPage.title();
let SubUrl = await subPage.url();
if(SubUrl.includes('receipt_job')){
await subPage.waitFor('.area-customer');
await subPage.evaluate(function() {
document.querySelector('textarea').value = ''
});
let IIIAddress = `財團法人資訊工業策進會
10622台北市大安區和平東路二段106號11樓`;
let eyAddress = `行政院
100009臺北市中正區忠孝東路1段1號`;
const typeInfo = IIIAddress;
await subPage.type('textarea', typeInfo, {delay: 2})
await subPage._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: `./public/${ids}`});
await subPage.click('#download-button')
await subPage.waitFor(1000*20);
fs.renameSync(`./public/${ids}/gengo_receipt.pdf`,`./public/${ids}/gengo_receipt-${ids}.pdf`)
let files = await fs.readdirSync(`./public/${ids}`);
if(files.length>=1) {
// has original, target, receipt files.
try{
let result = await db.insert(ids)
mail.sendMail(ids);
const title = `PDIS Gengo 翻譯 - ${ids}`;
noti.sendMessage({
title: title,
id: ids
});
}catch(error) {
console.error(`exists:${ids}`);
}
}else {
console.log(`${ids} folder is not 3 files`);
}
await subPage.close();
//download-button
}
}
}