-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
101 lines (95 loc) · 3.21 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
const scrapeIt = require("scrape-it")
const request = require('request')
const fs = require('fs')
const slugify = require('slugify')
const exec = require('child_process').exec
require('dotenv').config()
function os_func() {
this.execCommand = function(cmd, callback) {
exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
callback(stdout);
});
}
}
var os = new os_func();
var token = process.env.PDF_COOL_TOKEN
var instapaperCookies = [
{
name: 'pfp',
value: process.env.INSTAPAPER_PFP,
domain: 'www.instapaper.com'
},
{
name: 'pfu',
value: process.env.INSTAPAPER_PFU,
domain: 'www.instapaper.com'
},
{
name: 'pfh',
value: process.env.INSTAPAPER_PFH,
domain: 'www.instapaper.com'
}
]
// Promise interface
scrapeIt({
url: "https://www.instapaper.com/u"
, headers: { Cookie: `pfp=${process.env.INSTAPAPER_PFP}; pfu=${process.env.INSTAPAPER_PFU}; pfh=${process.env.INSTAPAPER_PFH}` }
}, {
articles: {
listItem: ".article_inner_item",
data: {
title: "a.article_title",
url: {
selector: "a.article_title",
attr: "href"
}
}
}
}).then(page => {
console.log(page.articles)
page.articles.forEach(function (article) {
// console.log(`https://www.instapaper.com${article.url}`)
const slugRemove = /[$*_+~.,/()'"!\-:@]/g
filename = `./pdfs/${slugify(article.title, {replacement: '-', remove: slugRemove, lower: true})}.pdf`
if (!fs.existsSync(filename)) {
var r = request({
url: 'https://pdf.cool/generate',
method: 'POST',
headers: {
authorization: `Bearer ${token}`
},
crossOrigin: true,
json: true,
body: {
url: `https://www.instapaper.com${article.url}`,
cookies: instapaperCookies,
"format": "A4",
"margin": {
"top": "24px",
"right": "16px",
"bottom": "24px",
"left": "24px"
},
// css: 'body{font-family: !initial important;}'
wait: 'load' // instapaper assets are slow... we don't want blank pdfs
}
}).on('response', function (response) {
// console.log(response)
if (response['headers']['content-disposition']) {
filename = `./pdfs/${slugify(article.title, {replacement: '-', remove: slugRemove, lower: true})}.pdf`
r.pipe(fs.createWriteStream(filename));
console.log(`stored ${filename}`)
os.execCommand(`./rmapi put ${filename} /Instapaper`, function (returnvalue) {
console.log(`uploaded ${filename} to /Instapaper`)
});
}
})
} else {
console.log(`exists: ${filename}`)
}
})
})