-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
198 lines (168 loc) · 5.11 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
'use strict';
const cheerio = require('cheerio');
const got = require('got');
const pRetry = require('p-retry');
const PROXY_URL = 'http://api.proxiesapi.com';
const SCHOLAR_BASE_URL = 'https://scholar.google.com';
class ScholarlyError extends Error {
constructor(msg) {
super(msg);
this.name = 'ScholarlyError';
}
}
class CaptchaError extends ScholarlyError {
constructor(msg) {
super(msg);
this.name = 'CaptchaError';
}
}
class ProxyError extends ScholarlyError {
constructor(msg) {
super(msg);
this.name = 'ProxyError';
}
}
const isCaptchaError = err => err instanceof CaptchaError;
const isHTTPError = (err, { statusCode }) => {
return err instanceof got.HTTPError &&
err.response.statusCode === statusCode;
};
const selectors = {
captcha: {
message: '[id^="gs_captcha"] h1'
},
pub: {
container: '.gs_ri',
title: '.gs_rt',
authors: '.gs_a a'
},
author: {
container: '#gsc_prf',
name: '#gsc_prf_in',
affiliation: '#gsc_prf_in + .gsc_prf_il',
domain: '#gsc_prf_ivh',
homepage: '#gsc_prf_ivh a',
interests: '#gsc_prf_int a',
metrics: '#gsc_rsb_st tr:nth-of-type(2) .gsc_rsb_std'
}
};
class Scholar {
init(apiKey, { retries } = {}) {
const useProxy = !!apiKey;
this.useProxy = useProxy;
let client = this._setupClient();
if (useProxy) client = this._setupProxy(client, apiKey);
this.client = client;
if (retries === undefined) retries = useProxy ? 2 : 0;
this.retries = Math.max(0, retries);
return this;
}
_setupClient() {
return got.extend({
handlers: [
(options, next) => {
if (options.isStream) return next(options);
return (async () => {
const resp = await next(options);
const $html = cheerio.load(resp.body);
const body = this._verifyPage($html);
return Object.assign(resp, { body });
})();
}
]
});
}
_setupProxy(client, apiKey) {
return client.extend({
prefixUrl: PROXY_URL,
searchParams: { auth_key: apiKey },
handlers: [
(options, next) => {
if (options.isStream) return next(options);
return next(options).catch(error => {
if (isHTTPError(error, { statusCode: 401 })) {
throw new ProxyError('Api key invalid or expired');
}
throw error;
});
}
]
});
}
_verifyPage($html) {
const $ = $html;
const { captcha } = selectors;
const captchaMessage = $(captcha.message).text();
if (captchaMessage) {
throw new CaptchaError(captchaMessage);
}
return $html;
}
request(url) {
url = url.href || url;
return pRetry(() => {
if (!this.useProxy) return this.client.get(url);
const searchParams = { url };
return this.client.get({ searchParams });
}, {
retries: this.retries,
onFailedAttempt(error) {
if (!isCaptchaError(error)) {
throw error;
}
}
});
}
async searchPub(query) {
const url = new URL('scholar', SCHOLAR_BASE_URL);
url.searchParams.set('q', query);
const resp = await this.request(url);
return this.parsePub(resp.body);
}
async getAuthorProfile(link) {
const url = new URL(link, SCHOLAR_BASE_URL);
const resp = await this.request(url);
return this.parseAuthorProfile(resp.body);
}
async getPubAuthors(query) {
const { authors } = await this.searchPub(query);
return Promise.all(authors.map(async ({ id, url }) => {
const profile = await this.getAuthorProfile(url);
return { id, ...profile };
}));
}
parsePub($html) {
const $ = $html;
const { pub } = selectors;
const $publicationContainer = $(pub.container).first();
const $authors = $publicationContainer.find(pub.authors);
const authors = $authors.map((_, el) => {
const $el = $(el);
const name = $el.text();
const url = new URL($el.attr('href'), SCHOLAR_BASE_URL);
const id = url.searchParams.get('user');
return { id, name, url: url.href };
}).get();
const title = $publicationContainer.find(pub.title).text();
return { title, authors };
}
parseAuthorProfile($html) {
const $ = $html;
const { author } = selectors;
const $profileContainer = $(author.container);
const name = $profileContainer.find(author.name).text();
const affiliation = $profileContainer.find(author.affiliation).text();
const homepage = $profileContainer.find(author.homepage).attr('href');
const $domain = $profileContainer.find(author.domain);
const emailInfo = $domain[0].childNodes[0].data.trim();
const domain = emailInfo.split(/\s+/g).filter(token => token !== '-').pop();
const $interests = $profileContainer.find(author.interests);
const interests = $interests.map((_, el) => $(el).text()).get();
const hindex = $(author.metrics).first().text();
return { name, affiliation, homepage, domain, hindex, interests };
}
}
module.exports = new Scholar();
module.exports.ScholarlyError = ScholarlyError;
module.exports.CaptchaError = CaptchaError;
module.exports.ProxyError = ProxyError;