-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
242 lines (223 loc) · 8.91 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
'use strict'
let scholar = (function () {
let request = require('request')
let cheerio = require('cheerio')
let striptags = require('striptags')
const throttledQueue = require('throttled-queue')
// 1 per 200 ms ~= 5/s per
// https://developers.google.com/webmaster-tools/search-console-api-original/v3/limits
const perSecThrottle = throttledQueue(5, 1000)
const perMinThrottle = throttledQueue(200, 60 * 1000)
const RESULTS_PER_PAGE = 10
const GOOGLE_SCHOLAR_URL = 'https://scholar.google.com/scholar?hl=en&q='
const GOOGLE_SCHOLAR_URL_PREFIX = 'https://scholar.google.com'
const ELLIPSIS_HTML_ENTITY = '…'
const ET_AL_NAME = 'et al.'
const CITATION_COUNT_PREFIX = 'Cited by '
const RELATED_ARTICLES_PREFIX = 'Related articles'
const STATUS_CODE_FOR_RATE_LIMIT = 503
const STATUS_MESSAGE_FOR_RATE_LIMIT = 'Service Unavailable'
const STATUS_MESSAGE_BODY = 'This page appears when Google automatically detects requests coming from your computer network which appear to be in violation of the <a href="//www.google.com/policies/terms/">Terms of Service</a>. The block will expire shortly after those requests stop.'
// regex with thanks to http://stackoverflow.com/a/5917250/1449799
const RESULT_COUNT_RE = /\W*((\d+|\d{1,3}(,\d{3})*)(\.\d+)?) results/
function scholarResultsCallback (resolve, reject) {
return function (error, response, html) {
if (error) {
reject(error)
} else if (response.statusCode !== 200) {
if (response.statusCode === STATUS_CODE_FOR_RATE_LIMIT && response.statusMessage === STATUS_MESSAGE_FOR_RATE_LIMIT && response.body.indexOf(STATUS_MESSAGE_BODY) > -1) {
reject(new Error('you are being rate-limited by google. you have made too many requests too quickly. see: https://support.google.com/websearch/answer/86640'))
} else {
reject(new Error('expected statusCode 200 on http response, but got: ' + response.statusCode))
}
} else {
let $ = cheerio.load(html)
let results = $('.gs_r')
let resultCount = 0
let nextUrl = ''
let prevUrl = ''
if ($('.gs_ico_nav_next').parent().attr('href')) {
nextUrl = GOOGLE_SCHOLAR_URL_PREFIX + $('.gs_ico_nav_next').parent().attr('href')
}
if ($('.gs_ico_nav_previous').parent().attr('href')) {
prevUrl = GOOGLE_SCHOLAR_URL_PREFIX + $('.gs_ico_nav_previous').parent().attr('href')
}
let processedResults = []
results.each((i, r) => {
$(r).find('.gs_ri h3 span').remove()
let title = $(r).find('.gs_ri h3').text().trim()
let url = $(r).find('.gs_ri h3 a').attr('href')
let authorNamesHTMLString = $(r).find('.gs_ri .gs_a').html()
let etAl = false
let etAlBegin = false
let authors = []
let description = $(r).find('.gs_ri .gs_rs').text()
let footerLinks = $(r).find('.gs_ri .gs_fl a')
let citedCount = 0
let citedUrl = ''
let relatedUrl = ''
let pdfUrl = $($(r).find('.gs_ggsd a')[0]).attr('href')
if ($(footerLinks[0]).text().indexOf(CITATION_COUNT_PREFIX) >= 0) {
citedCount = $(footerLinks[0]).text().substr(CITATION_COUNT_PREFIX.length)
}
if ($(footerLinks[0]).attr &&
$(footerLinks[0]).attr('href') &&
$(footerLinks[0]).attr('href').length > 0) {
citedUrl = GOOGLE_SCHOLAR_URL_PREFIX + $(footerLinks[0]).attr('href')
}
if (footerLinks &&
footerLinks.length &&
footerLinks.length > 0) {
if ($(footerLinks[0]).text &&
$(footerLinks[0]).text().indexOf(CITATION_COUNT_PREFIX) >= 0) {
citedCount = $(footerLinks[0]).text().substr(CITATION_COUNT_PREFIX.length)
}
if ($(footerLinks[1]).text &&
$(footerLinks[1]).text().indexOf(RELATED_ARTICLES_PREFIX) >= 0 &&
$(footerLinks[1]).attr &&
$(footerLinks[1]).attr('href') &&
$(footerLinks[1]).attr('href').length > 0) {
relatedUrl = GOOGLE_SCHOLAR_URL_PREFIX + $(footerLinks[1]).attr('href')
}
}
if (authorNamesHTMLString) {
let cleanString = authorNamesHTMLString.substr(0, authorNamesHTMLString.indexOf(' - '))
if (cleanString.substr(cleanString.length - ELLIPSIS_HTML_ENTITY.length) === ELLIPSIS_HTML_ENTITY) {
etAl = true
cleanString = cleanString.substr(0, cleanString.length - ELLIPSIS_HTML_ENTITY.length)
}
if (cleanString.substr(0, ELLIPSIS_HTML_ENTITY.length) === ELLIPSIS_HTML_ENTITY) {
etAlBegin = true
cleanString = cleanString.substr(ELLIPSIS_HTML_ENTITY.length + 2)
}
let htmlAuthorNames = cleanString.split(', ')
if (etAl) {
htmlAuthorNames.push(ET_AL_NAME)
}
if (etAlBegin) {
htmlAuthorNames.unshift(ET_AL_NAME)
}
authors = htmlAuthorNames.map(name => {
let tmp = cheerio.load(name)
let authorObj = {
name: '',
url: ''
}
if (tmp('a').length === 0) {
authorObj.name = striptags(name)
} else {
authorObj.name = tmp('a').text()
authorObj.url = GOOGLE_SCHOLAR_URL_PREFIX + tmp('a').attr('href')
}
return authorObj
})
}
processedResults.push({
title: title,
url: url,
authors: authors,
description: description,
citedCount: citedCount,
citedUrl: citedUrl,
relatedUrl: relatedUrl,
pdf: pdfUrl
})
})
let resultsCountString = $('#gs_ab_md').text()
if (resultsCountString && resultsCountString.trim().length > 0) {
let matches = RESULT_COUNT_RE.exec(resultsCountString)
if (matches && matches.length > 0) {
resultCount = parseInt(matches[1].replace(/,/g, ''))
} else {
resultCount = processedResults.length
}
} else {
resultCount = processedResults.length
}
resolve({
results: processedResults,
count: resultCount,
nextUrl: nextUrl,
prevUrl: prevUrl,
next: function () {
let p = new Promise(function (resolve, reject) {
perMinThrottle(() => {
perSecThrottle(() => {
var requestOptions = {
jar: true
}
requestOptions.url = nextUrl
request(requestOptions, scholarResultsCallback(resolve, reject))
})
})
})
return p
},
previous: function () {
let p = new Promise(function (resolve, reject) {
perMinThrottle(() => {
perSecThrottle(() => {
var requestOptions = {
jar: true
}
requestOptions.url = prevUrl
request(requestOptions, scholarResultsCallback(resolve, reject))
})
})
})
return p
}
})
}
}
}
function search (query) {
let p = new Promise(function (resolve, reject) {
perMinThrottle(() => {
perSecThrottle(() => {
var requestOptions = {
jar: true
}
requestOptions.url = encodeURI(GOOGLE_SCHOLAR_URL + query)
request(requestOptions, scholarResultsCallback(resolve, reject))
})
})
})
return p
}
function all (query) {
return search(query)
.then(resultsObj => {
// eg n=111 but i have 10 already so 101 remain,
let remainingResultsCount = resultsObj.count - resultsObj.results.length
if (remainingResultsCount > 0) {
// pr = 10
let pagesRemaining = remainingResultsCount / RESULTS_PER_PAGE
let pageNumbers = []
for (var i = 1; i <= pagesRemaining + 1; i++) {
pageNumbers.push(i)
}
return Promise.all(pageNumbers.map(i => {
return search(query + '&start=' + i * RESULTS_PER_PAGE)
.then(laterPagesResultsObj => {
return laterPagesResultsObj.results
})
}))
.then(remainingResultsArr => {
let allResults = resultsObj.results.concat(remainingResultsArr.reduce((a, b) => a.concat(b)))
resultsObj.results = allResults
resultsObj.nextUrl = null
resultsObj.next = null
resultsObj.prevUrl = null
resultsObj.prev = null
return resultsObj
})
}
})
}
return {
search: search,
all: all
}
})()
module.exports = scholar