forked from shunito/dlab2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (150 loc) · 5.01 KB
/
index.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
const fs = require('fs');
const assert = require('assert');
const puppeteer = require('puppeteer');
const util = require('util');
const pathUtil = require('path');
// Settings
const downloadPath = './build';
const inputPath = './ginga';
const creator = "銀河鉄道の夜";
const title = "宮沢賢治"
let files = [];
/*
const files = [
'ginga/ddconv.yml',
'ginga/cover.png',
'ginga/00_gingatetsudono_yoru.md',
'ginga/01_gingatetsudono_yoru.md',
'ginga/02_gingatetsudono_yoru.md',
'ginga/03_gingatetsudono_yoru.md',
'ginga/04_gingatetsudono_yoru.md',
'ginga/05_gingatetsudono_yoru.md',
'ginga/06_gingatetsudono_yoru.md',
'ginga/07_gingatetsudono_yoru.md',
'ginga/08_gingatetsudono_yoru.md',
'ginga/09_gingatetsudono_yoru.md',
'ginga/10_gingatetsudono_yoru.md'
];
*/
const readFiles = async()=>{
return new Promise((resolve, reject) => {
fs.readdir( inputPath, function(err, dir){
if (err) {
reject(err);
}
let list = dir.map(function(elm) {
return pathUtil.join( inputPath, elm);
});
files = list.filter(function(file){
console.log( 'File: ' , file );
return fs.statSync(file).isFile();
});
//console.log( files );
resolve(files);
});
});
}
const waitDownloadComplete = async(path, waitTimeSpanMs = 1000, timeoutMs = 60 * 1000) => {
return new Promise((resolve, reject) => {
const wait = (waitTimeSpanMs, totalWaitTimeMs) => setTimeout(
() => isDownloadComplete(path).then(
(completed) => {
if (completed) {
resolve();
} else {
const nextTotalTime = totalWaitTimeMs + waitTimeSpanMs;
if (nextTotalTime >= timeoutMs) {
reject('timeout');
}
const nextSpan = Math.min(
waitTimeSpanMs,
timeoutMs - nextTotalTime
);
wait(nextSpan, nextTotalTime);
}
}
).catch(
(err) => {
reject(err);
}
),
waitTimeSpanMs
);
wait(waitTimeSpanMs, 0);
});
}
const isDownloadComplete = async(path) => {
return new Promise((resolve, reject) => {
fs.readdir(path, (err, files) => {
if (err) {
reject(err);
} else {
for (let file of files) {
// .crdownloadがあればダウンロード中のものがある
if (/.*\.crdownload$/.test(file)) {
resolve(false);
return;
}
}
resolve(true);
}
});
});
}
(async() => {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
let fname = '';
await page._client.send(
'Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: downloadPath
}
);
await page.goto('https://conv.denshochan.com/', {
waitUntil: "networkidle2"
});
await page.screenshot({
path: 'screenshot/conv-denshochan.png'
});
await page.type("#form_creator", creator);
await page.type("#form_title", title);
page.on('response', res => {
(async() => {
//console.log( res );
const headers = res.headers;
const reg = new RegExp('filename="(.+)"');
if( res.ok ){
let contentDisposition = headers['content-disposition'];
if( contentDisposition ){
console.log("content Disposition:" , contentDisposition);
let content = contentDisposition.match( reg );
if( content ) {
fname = pathUtil.join( downloadPath, content[1]);
//console.log( "Download:", fname );
}
}
}
})();
});
const fileUploaders = await page.$("input[type=file]");
let input_creator = await page.$eval('#form_creator', el => el.value);
let input_title = await page.$eval('#form_title', el => el.value);
console.log("Headless でんでんコンバーター");
console.log("タイトル:", title);
console.log("製作者:" ,creator);
console.log("変換実行");
await readFiles();
fileUploaders.uploadFile(...files);
await page.screenshot({
path: 'screenshot/conv-denshochan2.png'
});
await page.click('button[name="submit"]');
await page.waitFor(10000);
await waitDownloadComplete(downloadPath)
.catch((err) => console.error(err));
await browser.close();
console.log("変換完了:" , fname );
})();