|
| 1 | +const axios = require('axios'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | +const fm = require('front-matter'); |
| 5 | +const yaml = require('js-yaml'); |
| 6 | +const fastcsv = require('fast-csv') |
| 7 | +const { |
| 8 | + type |
| 9 | +} = require('os'); |
| 10 | + |
| 11 | +require('dotenv').config(); |
| 12 | + |
| 13 | +PAPI_URL = "https://api.segmentapis.com" |
| 14 | + |
| 15 | +const getCatalog = async (url, page_token = "MA==") => { |
| 16 | + try { |
| 17 | + const res = await axios.get(url, { |
| 18 | + headers: { |
| 19 | + 'Content-Type': 'application/json', |
| 20 | + 'Authorization': `Bearer ${process.env.PAPI_TOKEN}` |
| 21 | + }, |
| 22 | + data: { |
| 23 | + "pagination": { |
| 24 | + "count": 200, |
| 25 | + "cursor": page_token |
| 26 | + } |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + return res.data |
| 31 | + } catch (error) { |
| 32 | + console.log(error) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +const slugify = (displayName) => { |
| 37 | + let slug = displayName |
| 38 | + .toLowerCase() |
| 39 | + .replace(/\s+/g, '-') |
| 40 | + .replace('-&-', '-') |
| 41 | + .replace('/', '-') |
| 42 | + .replace(/[\(\)]/g, '') |
| 43 | + .replace('.', '-') |
| 44 | + |
| 45 | + if (slug === '-net') slug = 'net' |
| 46 | + if (slug === 'talon-one') slug = 'talonone' |
| 47 | + if (slug === 'roku-alpha') slug = 'roku' |
| 48 | + if (slug === 'shopify-by-littledata') slug = 'shopify-littledata' |
| 49 | + if (slug === 'talon-one') slug = 'talonone' |
| 50 | + if (slug == 'google-adwords-remarketing-lists-customer-match') slug = 'adwords-remarketing-lists' |
| 51 | + if (slug == 'canny-classic') slug = 'canny' |
| 52 | + return slug |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +const isCatalogItemBeta = (itemURL) => { |
| 57 | + try { |
| 58 | + const catalogPath = path.resolve('src', itemURL, 'index.md') |
| 59 | + if (fs.existsSync(catalogPath)) { |
| 60 | + const f = fm(fs.readFileSync(catalogPath, 'utf8')); |
| 61 | + if (f.attributes.beta) return true |
| 62 | + } |
| 63 | + return false |
| 64 | + } catch (e) { |
| 65 | + console.log(error) |
| 66 | + return false |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +const getUpdateTime = (itemURL) => { |
| 71 | + try { |
| 72 | + const catalogPath = path.resolve('src', itemURL, 'index.md') |
| 73 | + if (fs.existsSync(catalogPath)) { |
| 74 | + |
| 75 | + const stats = fs.statSync(catalogPath) |
| 76 | + const date = stats.mtime |
| 77 | + return date.toISOString().split('T')[0] |
| 78 | + } |
| 79 | + |
| 80 | + } catch (e){ |
| 81 | + console.log(error) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +const getSources = async () => { |
| 86 | + let sources = [] |
| 87 | + let sourcesUpdated = [] |
| 88 | + //let regionalSourcesUpdated = [] |
| 89 | + let nextPageToken = "MA==" |
| 90 | + //let categories = new Set() |
| 91 | + //let sourceCategories = [] |
| 92 | + |
| 93 | + while (nextPageToken !== null) { |
| 94 | + const res = await getCatalog(`${PAPI_URL}/catalog/sources/`, nextPageToken) |
| 95 | + sources = sources.concat(res.data.sourcesCatalog) |
| 96 | + nextPageToken = res.data.pagination.next |
| 97 | + } |
| 98 | + |
| 99 | + sources.sort((a, b) => { |
| 100 | + if (a.name.toLowerCase() < b.name.toLowerCase()) { |
| 101 | + return -1; |
| 102 | + } |
| 103 | + if (a.name.toLowerCase() > b.name.toLowerCase()) { |
| 104 | + return 1; |
| 105 | + } |
| 106 | + return 0; |
| 107 | + }) |
| 108 | + |
| 109 | + const libraryCategories = [ |
| 110 | + 'server', |
| 111 | + 'mobile', |
| 112 | + 'ott', |
| 113 | + 'roku', |
| 114 | + 'website' |
| 115 | + ] |
| 116 | + |
| 117 | + sources.forEach(source => { |
| 118 | + let slug = slugify(source.name) |
| 119 | + let mainCategory = source.categories[0] ? source.categories[0].toLowerCase() : '' |
| 120 | + |
| 121 | + // determine the doc url based on the source's main category |
| 122 | + if (libraryCategories.includes(mainCategory)) { |
| 123 | + url = `connections/sources/catalog/libraries/${mainCategory}/${slug}` |
| 124 | + } else { |
| 125 | + url = `connections/sources/catalog/cloud-apps/${slug}` |
| 126 | + mainCategory = 'cloud-app' |
| 127 | + } |
| 128 | + |
| 129 | + let updateTime = getUpdateTime(url) |
| 130 | + |
| 131 | + let updatedSource = { |
| 132 | + id: source.id, |
| 133 | + display_name: source.name, |
| 134 | + slug, |
| 135 | + url: "https://segment.com/docs/" + url, |
| 136 | + updateTime |
| 137 | + } |
| 138 | + |
| 139 | + if (isCatalogItemBeta(url)) { |
| 140 | + sourcesUpdated.push(updatedSource) |
| 141 | + } |
| 142 | + }) |
| 143 | + const ws = fs.createWriteStream("beta-sources.csv"); |
| 144 | + fastcsv |
| 145 | + .write(sourcesUpdated, { |
| 146 | + headers: true |
| 147 | + }) |
| 148 | + .on("finish", function () { |
| 149 | + console.log("Write to CSV successfully!"); |
| 150 | + }) |
| 151 | + .pipe(ws); |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | +const getDestinations = async () => { |
| 156 | + let destinations = [] |
| 157 | + let destinationsUpdated = [] |
| 158 | + let regionalDestinationsUpdated = [] |
| 159 | + let destinationCategories = [] |
| 160 | + let categories = new Set() |
| 161 | + let nextPageToken = "MA==" |
| 162 | + |
| 163 | + while (nextPageToken !== null) { |
| 164 | + const res = await getCatalog(`${PAPI_URL}/catalog/destinations/`, nextPageToken) |
| 165 | + destinations = destinations.concat(res.data.destinationsCatalog) |
| 166 | + nextPageToken = res.data.pagination.next |
| 167 | + } |
| 168 | + |
| 169 | + destinations.sort((a, b) => { |
| 170 | + if (a.name.toLowerCase() < b.name.toLowerCase()) { |
| 171 | + return -1; |
| 172 | + } |
| 173 | + if (a.name.toLowerCase() > b.name.toLowerCase()) { |
| 174 | + return 1; |
| 175 | + } |
| 176 | + return 0; |
| 177 | + }) |
| 178 | + |
| 179 | + destinations.forEach(destination => { |
| 180 | + // We need to be able to keep the system slug in some cases. |
| 181 | + const slugOverrides = ['actions-google-enhanced-conversions', 'actions-google-analytics-4', 'actions-facebook-conversions-api', 'actions-friendbuy-cloud', 'sprig-web'] |
| 182 | + let slug = slugify(destination.name) |
| 183 | + if (slugOverrides.includes(destination.slug)) { |
| 184 | + slug = destination.slug |
| 185 | + } |
| 186 | + // Flip the slug of Actions destinations |
| 187 | + const actionsDests = [ |
| 188 | + 'amplitude-actions', |
| 189 | + 'slack-actions', |
| 190 | + 'fullstory-actions', |
| 191 | + 'friendbuy-actions' |
| 192 | + ] |
| 193 | + |
| 194 | + if (actionsDests.includes(slug)) { |
| 195 | + const newSlug = slug.split('-') |
| 196 | + slug = newSlug[1] + '-' + newSlug[0] |
| 197 | + } |
| 198 | + let url = `connections/destinations/catalog/${slug}` |
| 199 | + let updateTime = getUpdateTime(url) |
| 200 | + |
| 201 | + let updatedDestination = { |
| 202 | + destination_id: destination.id, |
| 203 | + display_name: destination.name, |
| 204 | + slug, |
| 205 | + url: "https://segment.com/docs/" + url, |
| 206 | + updateTime |
| 207 | + } |
| 208 | + if (destination.status == 'PUBLIC_BETA') { |
| 209 | + destinationsUpdated.push(updatedDestination) |
| 210 | + //console.log(destination.name) |
| 211 | + } |
| 212 | + }) |
| 213 | + const ws = fs.createWriteStream("beta-destinations.csv"); |
| 214 | + fastcsv |
| 215 | + .write(destinationsUpdated, { |
| 216 | + headers: true |
| 217 | + }) |
| 218 | + .on("finish", function () { |
| 219 | + console.log("Write destinations to CSV successfully!"); |
| 220 | + }) |
| 221 | + .pipe(ws); |
| 222 | +} |
| 223 | + |
| 224 | + |
| 225 | +getSources() |
| 226 | +getDestinations() |
0 commit comments