-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (54 loc) · 1.85 KB
/
app.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
require('dotenv').config()
const { Kutt } = require('kutt')
const kuttApi = process.env.KUTT_URL
const kuttKey = process.env.KUTT_KEY
const targetDomain = process.env.TARGET_DOMAIN || '*'
const targetDomainParamsPrefix = process.env.TARGET_DOMAIN_PARAMS_PREFIX || '*'
Kutt.set('api', `${kuttApi}/api/v2`).set('key', kuttKey)
const getParamsFromLink = (link, paramsPrefix) => {
const urlAndParams = link.split('?')
if (urlAndParams.length > 0) {
let params = {}
urlAndParams[1].split('&').forEach(param => {
if (paramsPrefix != '*') {
if (param.includes(paramsPrefix)) {
const paramKeyAndValue = param.split('=')
params[paramKeyAndValue[0]] = paramKeyAndValue[1]
}
} else {
const paramKeyAndValue = param.split('=')
params[paramKeyAndValue[0]] = paramKeyAndValue[1]
}
})
return Object.keys(params).length === 0 ? null : params
}
return null
}
const getLinks = async () => {
const kutt = new Kutt()
const isHealthy = await kutt.health().check()
if (!isHealthy) throw new Error('API is not healthy, aborted')
const response = await kutt.links().list({ all: true, limit: 100, skip: 0 })
if (!response || response.data.length === 0) return null
return response.data
.filter(link => (targetDomain === '*' ? true : link.target.includes(targetDomain)))
.map(link => {
return {
targetUrl: link.target,
targetShortUrl: link.link,
targetCanonicalUrl: link.target.split('?')[0],
params: getParamsFromLink(link.target, targetDomainParamsPrefix),
visitCount: link.visit_count,
}
})
.sort((a, b) => b.visitCount - a.visitCount)
}
const handleLinksStats = links => {
if (!links) return
console.log({
links,
linksCount: links.length,
})
}
const start = async () => handleLinksStats(await getLinks())
start()