-
Notifications
You must be signed in to change notification settings - Fork 0
/
down-paper.js
85 lines (80 loc) · 2.88 KB
/
down-paper.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
'use strict'
const pdfcrowd = require('pdfcrowd')
let scholar = (function () {
let request = require('request')
let cheerio = require('cheerio')
const throttledQueue = require('throttled-queue')
const perSecThrottle = throttledQueue(5, 1000)
const perMinThrottle = throttledQueue(200, 60 * 1000)
const GOOGLE_SCHOLAR_URL = 'https://scholar.google.com/scholar?hl=en&q='
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.'
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('Too many requests!'))
} else {
reject(new Error(response.statusCode))
}
} else {
let $ = cheerio.load(html)
let results = $('.gs_r')
let processedResults = []
results.each((i, r) => {
$(r).find('.gs_ri h3 span').remove()
let title = $(r).find('.gs_ri h3').text().trim()
let pdfUrl = $($(r).find('.gs_ggsd a')[0]).attr('href')
if((title!=='') && (pdfUrl!==undefined)){
processedResults.push({
title: title,
pdf:pdfUrl
})
}
})
resolve({
results: processedResults,
})
}
}
}
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
}
return {
search: search,
}
})()
scholar.search('VR')
.then(resultsObj => {
download(resultsObj)
})
var client = new pdfcrowd.HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");
var i = 0
function download(res){
const a = res
while(i < 5){
console.log(a["results"][i].title)
// run the conversion and write the result to a file
client.convertUrlToFile(a["results"][i].pdf, a["results"][i].title+'.pdf',
function(err, fileName) {
if (err) return console.error("Pdfcrowd Error: " + err);
console.log("Success: the file was created " + fileName);
});
i=i+1
}
}