-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (60 loc) · 1.84 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
const fs = require('fs')
const lastDomainPath = 'var/domain.last'
async function getFeed(url) {
const fetch = (await import('node-fetch')).default
const response = await fetch(url)
return await response.json()
}
function mapData(data) {
return data.map(item => {
return [
item.domain + item.extension,
item.price,
item.venue,
item.closed
]
})
}
async function getCsv(data) {
const createCsvStringifier = (await import('csv-writer')).default.createArrayCsvStringifier
const csv = createCsvStringifier({header: ['Domain', 'Price']})
return csv.stringifyRecords(data)
}
function updateLastDomain(domain) {
fs.writeFileSync(lastDomainPath, domain, 'utf8')
}
function getLastDomain() {
return fs.existsSync(lastDomainPath) ? fs.readFileSync(lastDomainPath, 'utf8') : null
}
(async () => {
const url = 'https://namebio.com/live-feed'
const path = 'var/domains.csv'
const varPath = 'var'
!fs.existsSync(varPath) && fs.mkdirSync(varPath)
console.log(`Requesting ${url}...`)
const feed = await getFeed(url)
console.log('Success:', feed && feed.status, `- ${feed.message.length} items`)
let data = mapData(feed.message)
if (data.length) {
const lastDomain = getLastDomain()
if (lastDomain) {
const lastDomainIndex = data.findIndex(item => item[0] === lastDomain)
if (lastDomainIndex >= 0) {
data = data.slice(lastDomainIndex + 1)
}
}
}
if (data.length) {
console.log(`${data.length} new domains found`)
console.log('Convert to CSV...')
const csv = await getCsv(data, path)
console.log(`Writing data to ${path}...`)
fs.appendFileSync(path, csv)
console.log(`Updating last domain...`)
const lastDomain = data[data.length - 1][0]
updateLastDomain(lastDomain)
console.log('Done')
} else {
console.log('No new domains')
}
})()