-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrawl-hn.js
77 lines (69 loc) · 2 KB
/
crawl-hn.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
const {TribeClient} = require('@tribeplatform/gql-client')
const https = require('https')
const {PostMappingTypeEnum} = require("@tribeplatform/gql-client/types");
const client = new TribeClient({
graphqlUrl: 'https://app.tribe.so/graphql',
accessToken: '',
})
const maxItem = 30120143
const fetch = async itemId => {
return new Promise(resolve => {
const req = https.request(`https://hacker-news.firebaseio.com/v0/item/${itemId}.json`, res => {
res.on('data', data => {
resolve(JSON.parse(data.toString()))
})
})
req.end()
})
}
const createComments = async (item, postId) => {
if(!item.kids) return
for(let id of item.kids) {
const comment = await fetch(id)
client.posts.reply(postId, {
input: {
postTypeId: '4DznH0SQ6k2IQUT',
publish: true,
mappingFields: [{
key: 'content',
type: PostMappingTypeEnum.HTML,
value: JSON.stringify(comment.text)
}],
}
})
}
}
const createPost = item => {
client.posts.create({
spaceId: 'dy3eAqs9cDCC',
input: {
postTypeId: 'nizYc5RZXWArXJW',
publish: true,
mappingFields: [
{
key: 'title',
type: PostMappingTypeEnum.TEXT,
value: JSON.stringify(item.title)
},
{
key: 'content',
type: PostMappingTypeEnum.HTML,
value: JSON.stringify(`<div>Visit: ${item.url}</div>`),
}
]
}
}).then(res => {
createComments(item, res.id)
})
}
const crawl = async itemId => {
const item = await fetch(itemId)
if (item.type === 'story') {
if (!item.title || !item.url)
return
createPost(item)
}
}
for (let i = 0; i < 10000; i++) {
crawl(maxItem - i)
}