-
Notifications
You must be signed in to change notification settings - Fork 0
/
_nova.js
142 lines (136 loc) · 3.41 KB
/
_nova.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
const request = require('request-promise')
const async = require('async')
const sanitizeHtml = require('sanitize-html')
const cheerio = require('cheerio')
const levelup = require('levelup')
const leveldown = require('leveldown')
const { html2json } = require('html2json')
const fetch = require('node-fetch')
const { extend } = require('lodash')
const md5 = require('md5')
var db = levelup(leveldown('/tmp/sdsds111'))
async function doRequest (url) {
return new Promise((resolve, reject) => {
request(url, (error, res, body) => {
if (!error && res.statusCode == 200) {
resolve(body)
} else {
reject(error)
}
})
})
}
const insert = (json, callback) => {
fetch('https://db.rudixlab.com/v2/query', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-hasura-admin-secret': 'dasdasasd'
},
body: JSON.stringify({
type: 'insert',
args: {
table: { name: 'News', schema: 'public' },
source: 'DB',
objects: [
{
...json
//user_id: res.locals.user.username,
}
],
returning: ['id']
}
})
})
.then(result => {
return result.json()
})
.then(data => {
console.log(data)
callback(data)
})
}
async function nova (params, callback) {
const htmlString = await doRequest('https://nova.bg/filter/all')
const $x = cheerio.load(htmlString)
let extended = []
const arrx = $x('.thumb-box .thumb-desc .thumb-title h3 a')
.toArray()
.map(x => ({
title: $x(x).text(),
href: $x(x).attr('href'),
id: $x(x)
.attr('href')
.split('/')
}))
async.eachSeries(
arrx,
(i, cb) => {
fetch(i.href)
.then(res => res.text())
.then(body => {
const $ = cheerio.load(body)
const title = $('.title-wrap-roboto h1').text()
const media = $('.row-custom-media .gutter-0 div img').attr('src')
const tags = $('.article-body')
.text()
.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ' ')
.replace(/[\r\n\t]+/g, ' ')
.split(' ')
.filter(x => x.length > 8)
.map(x => x.toLowerCase())
const clean = sanitizeHtml($('.article-body').html(), {
allowedTags: ['p', 'img'],
allowedAttributes: {
img: ['data-original', 'class', 'src']
}
}).replace(/data-original/g, 'src')
const react = html2json(clean).child.map((item, i) => {
return { id: i, ...item }
})
if (title.length > 20) {
extended.push({
title,
md5: md5(title),
media,
react,
tags,
source: 'nova.bg',
uid: md5(title)
})
cb()
} else {
cb()
}
})
},
err => {
async.each(
extended,
(i, cb) => {
db.get(i.uid, (e, data) => {
if (!data) {
db.put(i.uid, 'xxx', function () {
insert(i, () => {
cb()
})
})
} else {
cb()
}
})
},
err => {
callback(extended)
}
)
}
)
}
if (!process.env.PORT) {
nova('', () => {})
}
module.exports = {
nova
//imgur,
}