-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (80 loc) · 2.62 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
const puppeteer = require('puppeteer')
const fs = require('fs')
const https = require('https')
main(process.argv[2], process.argv[3])
function download(url, cookies, filename) {
return new Promise(resolve => {
https.get(
url,
{
headers: {
Cookie: cookies.map(cookie => `${cookie.name}=${cookie.value}`),
},
},
res => {
res.pipe(fs.createWriteStream(filename)).on('close', () => {
resolve()
})
},
)
})
}
async function main(mailAddress, password) {
const browser = await puppeteer.launch()
const page = await browser.newPage()
page.setDefaultNavigationTimeout(100 * 1000)
await page.goto('https://8122.jp')
// 1. Sign in in the top page
const mailAddressInput = await page.$x(
"//*[contains(text(), 'メールアドレス')]/following-sibling::input[@type='text'][1]",
)
await mailAddressInput[0].type(mailAddress)
const passwordInput = await page.$x(
"//*[contains(text(), 'パスワード')]/following-sibling::input[@type='password'][1]",
)
await passwordInput[0].type(password)
const loginButton = await page.$x(
"//button[.//*[contains(text(), 'ログイン')]]",
)
loginButton[0].click()
await page.waitForNavigation()
// 2. Go to order history
const historyLink = await page.$x("//a[contains(text(), '注文履歴')]")
historyLink[0].click()
await page.waitForNavigation()
// 3. Go to download page
const downloadPageLink = await page.$x(
"//a[contains(text(), 'ダウンロード可能な写真の一覧はこちら')]",
)
downloadPageLink[0].click()
await page.waitForXPath("//a[contains(text(), 'ダウンロードする')]")
const cookies = await page.cookies()
let count = 1
for (;;) {
// 4. Download
const downloadLinks = await page.$x(
"//a[contains(text(), 'ダウンロードする')]",
)
const downlaodUrls = await Promise.all(
downloadLinks.map(downloadLink => downloadLink.evaluate(a => a.href)),
)
for (const downloadUrl of downlaodUrls) {
await download(downloadUrl, cookies, `${count}.jpg`)
console.log(`${downloadUrl} downloaded(${count})`)
count++
}
// 5. Go to next page
const nextPageButton = await page.$x("//button[contains(text(), '次へ')]")
const pointerEvents = await nextPageButton[0].evaluate(
a => getComputedStyle(a).pointerEvents,
)
if (pointerEvents !== 'none') {
nextPageButton[0].click()
await page.waitForTimeout(1000)
await page.waitForXPath("//a[contains(text(), 'ダウンロードする')]")
} else {
break
}
}
await browser.close()
}