-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (110 loc) · 3.59 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
#!/usr/bin/env node
const { ArgumentParser } = require('argparse')
const sanitize = require('sanitize-filename')
const mkdirp = require('mkdirp2').promise
const puppeteer = require('puppeteer')
const fs = require('fs').promises
const path = require('path')
const parser = new ArgumentParser({
version: require('./package.json').version,
description: 'The Tech Game DL',
addHelp: true
})
parser.addArgument('id', {
help: 'The ID of the category to download',
})
parser.addArgument([ '-u', '--username' ], {
help: 'Your username',
required: true
})
parser.addArgument([ '-p', '--password' ], {
help: 'Your password',
required: true
})
const args = parser.parseArgs()
async function main() {
const browser = await puppeteer.launch({
headless: true,
args: [
'--no-sandbox'
]
})
const page = await browser.newPage()
await signIn(page, {
username: args.username,
password: args.password
})
await downloadCategory(browser, page, args.id)
}
async function downloadCategory(browser, page, id) {
await page.goto(`https://www.thetechgame.com/Downloads/cid=${id}.html`, {
waitUntil: 'domcontentloaded'
})
while (true) {
const links = await page.$$('a.forumlink[title=Download]')
for (const link of links) {
try {
await downloadItem(browser, await link.evaluate((node) => node.href))
} catch (error) {
console.log(error)
console.log('FAILED! Was downloading', await link.evaluate((node) => node.href))
process.exit(1)
}
}
const nextPage = await page.$('a[title="Next page"]')
if (!nextPage) break
await nextPage.click()
await page.waitForNavigation({ waitUntil: 'domcontentloaded' })
}
await browser.close()
console.log(`Everything is downloaded! Saved to directory '${args.id}'.`)
}
/**
*
* @param {import('puppeteer').Page} page
* @param {Object} credentials
*/
async function signIn(page, { username, password }) {
await page.goto('https://www.thetechgame.com/Account.html', { waitUntil: 'domcontentloaded' })
await page.type('#username', username)
await page.type('#password', password)
try {
await Promise.all([
page.click('#buttons > button[type="submit"]'),
page.waitForNavigation({ timeout: 1000 * 120 })
])
} catch (error) {
if (error.name === 'TimeoutError') {
console.error('Could not log in! Are your credentials correct?')
process.exit(1)
}
throw error
}
}
async function downloadItem(browser, url) {
const id = /\/id=(\d*)\//.exec(url)[1]
if (!id) throw new Error('no id') // should never happen, but will make debugging easier if it does
const page = await browser.newPage()
await page.goto(url, { waitUntil: 'domcontentloaded' })
await page.$eval('a[title="Manage your profile"]', el => el.outerHTML = '')
const pageTitle = await page.title()
const itemName = pageTitle.substr(0, pageTitle.length - ' - The Tech Game'.length)
const itemPath = path.join(
path.resolve('./'),
args.id,
sanitize(`${itemName} [${id}]`)
)
await mkdirp(itemPath)
await fs.writeFile(path.join(itemPath, 'page.html'), await page.content())
try {
await page.pdf({ path: path.join(itemPath, 'page.pdf') })
} catch {} // when not headless, pdf fails
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: itemPath
})
await page.click('#buttons > button[title=Download]')
await page.close()
console.log(`Downloaded: ${itemName}`)
}
main()