forked from segment-boneyard/segment-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeta.js
More file actions
226 lines (197 loc) · 5.66 KB
/
Copy pathbeta.js
File metadata and controls
226 lines (197 loc) · 5.66 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const axios = require('axios');
const path = require('path');
const fs = require('fs');
const fm = require('front-matter');
const yaml = require('js-yaml');
const fastcsv = require('fast-csv')
const {
type
} = require('os');
require('dotenv').config();
PAPI_URL = "https://api.segmentapis.com"
const getCatalog = async (url, page_token = "MA==") => {
try {
const res = await axios.get(url, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.PAPI_TOKEN}`
},
data: {
"pagination": {
"count": 200,
"cursor": page_token
}
}
});
return res.data
} catch (error) {
console.log(error)
}
}
const slugify = (displayName) => {
let slug = displayName
.toLowerCase()
.replace(/\s+/g, '-')
.replace('-&-', '-')
.replace('/', '-')
.replace(/[\(\)]/g, '')
.replace('.', '-')
if (slug === '-net') slug = 'net'
if (slug === 'talon-one') slug = 'talonone'
if (slug === 'roku-alpha') slug = 'roku'
if (slug === 'shopify-by-littledata') slug = 'shopify-littledata'
if (slug === 'talon-one') slug = 'talonone'
if (slug == 'google-adwords-remarketing-lists-customer-match') slug = 'adwords-remarketing-lists'
if (slug == 'canny-classic') slug = 'canny'
return slug
}
const isCatalogItemBeta = (itemURL) => {
try {
const catalogPath = path.resolve('src', itemURL, 'index.md')
if (fs.existsSync(catalogPath)) {
const f = fm(fs.readFileSync(catalogPath, 'utf8'));
if (f.attributes.beta) return true
}
return false
} catch (e) {
console.log(error)
return false
}
}
const getUpdateTime = (itemURL) => {
try {
const catalogPath = path.resolve('src', itemURL, 'index.md')
if (fs.existsSync(catalogPath)) {
const stats = fs.statSync(catalogPath)
const date = stats.mtime
return date.toISOString().split('T')[0]
}
} catch (e){
console.log(error)
}
}
const getSources = async () => {
let sources = []
let sourcesUpdated = []
//let regionalSourcesUpdated = []
let nextPageToken = "MA=="
//let categories = new Set()
//let sourceCategories = []
while (nextPageToken !== null) {
const res = await getCatalog(`${PAPI_URL}/catalog/sources/`, nextPageToken)
sources = sources.concat(res.data.sourcesCatalog)
nextPageToken = res.data.pagination.next
}
sources.sort((a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) {
return -1;
}
if (a.name.toLowerCase() > b.name.toLowerCase()) {
return 1;
}
return 0;
})
const libraryCategories = [
'server',
'mobile',
'ott',
'roku',
'website'
]
sources.forEach(source => {
let slug = slugify(source.name)
let mainCategory = source.categories[0] ? source.categories[0].toLowerCase() : ''
// determine the doc url based on the source's main category
if (libraryCategories.includes(mainCategory)) {
url = `connections/sources/catalog/libraries/${mainCategory}/${slug}`
} else {
url = `connections/sources/catalog/cloud-apps/${slug}`
mainCategory = 'cloud-app'
}
let updateTime = getUpdateTime(url)
let updatedSource = {
id: source.id,
display_name: source.name,
slug,
url: "https://segment.com/docs/" + url,
updateTime
}
if (isCatalogItemBeta(url)) {
sourcesUpdated.push(updatedSource)
}
})
const ws = fs.createWriteStream("beta-sources.csv");
fastcsv
.write(sourcesUpdated, {
headers: true
})
.on("finish", function () {
console.log("Write to CSV successfully!");
})
.pipe(ws);
}
const getDestinations = async () => {
let destinations = []
let destinationsUpdated = []
let regionalDestinationsUpdated = []
let destinationCategories = []
let categories = new Set()
let nextPageToken = "MA=="
while (nextPageToken !== null) {
const res = await getCatalog(`${PAPI_URL}/catalog/destinations/`, nextPageToken)
destinations = destinations.concat(res.data.destinationsCatalog)
nextPageToken = res.data.pagination.next
}
destinations.sort((a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) {
return -1;
}
if (a.name.toLowerCase() > b.name.toLowerCase()) {
return 1;
}
return 0;
})
destinations.forEach(destination => {
// We need to be able to keep the system slug in some cases.
const slugOverrides = ['actions-google-enhanced-conversions', 'actions-google-analytics-4', 'actions-facebook-conversions-api', 'actions-friendbuy-cloud', 'sprig-web']
let slug = slugify(destination.name)
if (slugOverrides.includes(destination.slug)) {
slug = destination.slug
}
// Flip the slug of Actions destinations
const actionsDests = [
'amplitude-actions',
'slack-actions',
'fullstory-actions',
'friendbuy-actions'
]
if (actionsDests.includes(slug)) {
const newSlug = slug.split('-')
slug = newSlug[1] + '-' + newSlug[0]
}
let url = `connections/destinations/catalog/${slug}`
let updateTime = getUpdateTime(url)
let updatedDestination = {
destination_id: destination.id,
display_name: destination.name,
slug,
url: "https://segment.com/docs/" + url,
updateTime
}
if (destination.status == 'PUBLIC_BETA') {
destinationsUpdated.push(updatedDestination)
//console.log(destination.name)
}
})
const ws = fs.createWriteStream("beta-destinations.csv");
fastcsv
.write(destinationsUpdated, {
headers: true
})
.on("finish", function () {
console.log("Write destinations to CSV successfully!");
})
.pipe(ws);
}
getSources()
getDestinations()