-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathg4g_crawl.js
142 lines (123 loc) · 4.03 KB
/
g4g_crawl.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
var request = require('sync-request')
var cheerio = require('cheerio')
var processImg = require('epub-crawler/src/img')
var fs = require('fs')
var req = require('epub-crawler/src/util').requestRetry
var selectors = {
'title': 'article>h1',
'content': 'article>.text',
'remove': 'script, ins, iframe, p:empty, pre:empty, #personalNoteDiv, #practiceLinkDiv, div[id^=AP], .code-output-container, .textBasedMannualAds, ._ap_apex_ad, div>br',
'tab': '.code-block',
'code': 'td.code',
'line': '.line',
'link': '.articles-list .content .head a',
'pageUrl': 'https://www.geeksforgeeks.org/{id}/',
'tocUrl': 'https://www.geeksforgeeks.org/category/{cate}/page/{i}/',
'exiName': 'g4g_exist.json',
}
var artTemp = '<html><body>\n<h1>{title}</h1>\n{content}\n</body></html>'
function processTab($, $tab) {
var $code = $tab.find(selectors.code).eq(0)
var $newCode = $('<pre></pre>')
var $lines = $code.find(selectors.line)
var lines = []
for (var i = 0; i < $lines.length; i++) {
lines.push($lines.eq(i).text())
}
$newCode.text(lines.join('\n'))
$tab.replaceWith($newCode)
}
function getArticle(html, url) {
var $ = cheerio.load(html)
if (selectors.remove)
$(selectors.remove).remove()
var $tabs = $(selectors.tab)
for(var i = 0; i < $tabs.length; i++) {
var $tab = $tabs.eq(i)
processTab($, $tab)
}
var title = '<h1>' +
$(selectors.title).eq(0).text().trim() + '</h1>'
var co = $(selectors.content).html()
co = `<blockquote>原文:<a href='${url}'>${url}</a></blockquote>${co}`
return {title: title, content: co}
}
function load_exist() {
var fname = selectors.exiName
if (!fs.existsSync(fname))
return new Set()
var li = JSON.parse(fs.readFileSync(fname, 'utf-8'))
console.log(`load ${li.length} existed`)
return new Set(li)
}
var exi = load_exist()
function download(id, dir='out') {
console.log(id)
if (fs.existsSync(`${dir}/${id}.html`))
return
if (exi.has(id)) return
var url = selectors.pageUrl.replace('{id}', id)
var html = req('GET', url).body.toString()
var art = getArticle(html, url)
var imgs = new Map()
art.content = processImg(art.content, imgs, {
'pageUrl': url,
'imgPrefix': 'img/',
})
try {fs.mkdirSync(dir)} catch {}
try {fs.mkdirSync(`${dir}/img`)} catch {}
html = artTemp.replace('{title}', art.title)
.replace('{content}', art.content)
fs.writeFileSync(`${dir}/${id}.html`, html)
for (var [name, img] of imgs.entries()) {
fs.writeFileSync(`${dir}/img/${name}`, img)
}
}
function batch(fname) {
var dir = fname.replace(/\.\w+$/, '')
var ids = fs.readFileSync(fname, 'utf-8')
.split('\n')
.map(x => x.trim())
.filter(x => x)
for (var id of ids)
download(id, dir)
}
function getIds(html) {
var $ = cheerio.load(html)
var $links = $(selectors.link)
var res = []
for (var i = 0; i < $links.length; i++) {
var url = $links.eq(i).attr('href')
if (!url) continue
res.push(url.split('/').slice(-2)[0])
}
return res
}
function fetch(fname, cate, st, ed) {
st = st || 1
ed = ed || 1000000
var ofile = fs.openSync(fname, 'a')
for (var i = st; i <= ed; i++) {
console.log(`page: ${i}`)
var url = selectors.tocUrl
.replace('{cate}', cate)
.replace('{i}', i.toString())
var html = req('GET', url).body.toString()
var ids = getIds(html)
if (ids.length == 0) break
fs.writeSync(ofile, `page: ${i}\n`)
ids = ids.filter(x => !exi.has(x))
for (var id of ids)
fs.writeSync(ofile, id + '\n')
}
}
function main() {
var cmd = process.argv[2]
if (cmd == 'download')
download(process.argv[3])
else if (cmd == 'batch')
batch(process.argv[3])
else if (cmd == 'fetch')
fetch(...process.argv.slice(3, 7))
}
if (require.main === module) main()