1
+ const axios = require ( 'axios' ) ;
2
+ const path = require ( 'path' ) ;
3
+ const fs = require ( 'fs' ) ;
4
+ const yaml = require ( 'js-yaml' ) ;
5
+
6
+ require ( 'dotenv' ) . config ( ) ;
7
+
8
+ PLATFORM_API_URL = "https://platform.segmentapis.com"
9
+
10
+ const slugify = ( displayName ) => {
11
+ let slug = displayName . toLowerCase ( ) . replace ( / \s + / g, '-' )
12
+ if ( slug === '.net' ) slug = 'net'
13
+ if ( slug === 'roku-(alpha)' ) slug = 'roku'
14
+ return slug
15
+ }
16
+
17
+ const getCatalog = async ( url , page_token = "" ) => {
18
+ try {
19
+ const res = await axios . get ( url , {
20
+ headers : {
21
+ 'Content-Type' : 'application/json' ,
22
+ 'Authorization' : `Bearer ${ process . env . PLATFORM_API_TOKEN } `
23
+ } ,
24
+ params : {
25
+ page_token,
26
+ page_size : 100
27
+ }
28
+ } ) ;
29
+ return res . data
30
+ } catch ( error ) {
31
+ console . log ( error )
32
+ }
33
+ }
34
+
35
+ const updateSources = async ( ) => {
36
+ let sources = [ ]
37
+ let sourcesUpdated = [ ]
38
+ let categories = new Set ( ) ;
39
+ let sourceCategories = [ ]
40
+ let nextPageToken = null
41
+
42
+ while ( nextPageToken !== "" ) {
43
+ const res = await getCatalog ( `${ PLATFORM_API_URL } /v1beta/catalog/sources` , nextPageToken )
44
+ sources = sources . concat ( res . sources )
45
+ nextPageToken = res . next_page_token
46
+ }
47
+
48
+ sources . sort ( ( a , b ) => {
49
+ if ( a . display_name < b . display_name ) { return - 1 ; }
50
+ if ( a . display_name > b . display_name ) { return 1 ; }
51
+ return 0 ;
52
+ } )
53
+
54
+ const libraryCategories = [
55
+ 'server' ,
56
+ 'mobile' ,
57
+ 'ott' ,
58
+ 'roku' ,
59
+ 'website'
60
+ ]
61
+
62
+ sources . forEach ( source => {
63
+ let slug = slugify ( source . display_name )
64
+ let url = ''
65
+ let mainCategory = source . categories [ 0 ] ? source . categories [ 0 ] . toLowerCase ( ) : ''
66
+
67
+ if ( libraryCategories . includes ( mainCategory ) ) {
68
+ url = `connections/sources/catalog/libraries/${ mainCategory } /${ slug } `
69
+ } else {
70
+ url = `connections/sources/catalog/cloud-apps/${ slug } `
71
+ }
72
+
73
+ let updatedSource = {
74
+ display_name : source . display_name ,
75
+ slug,
76
+ name : source . name ,
77
+ description : source . description ,
78
+ url,
79
+ logo : {
80
+ url : source . logos . logo
81
+ } ,
82
+ mark : {
83
+ url : source . logos . mark
84
+ } ,
85
+ categories : source . categories
86
+ }
87
+ sourcesUpdated . push ( updatedSource )
88
+
89
+ // add unique source categories to set
90
+ source . categories . reduce ( ( s , e ) => s . add ( e ) , categories ) ;
91
+ } )
92
+
93
+ const sourceArray = Array . from ( categories )
94
+ sourceArray . forEach ( category => {
95
+ sourceCategories . push ( {
96
+ display_name : category ,
97
+ slug : slugify ( category )
98
+ } )
99
+ } )
100
+
101
+ // Create source catalog yaml file
102
+ const options = { noArrayIndent : true } ;
103
+ let output = "# AUTOGENERATED FROM PLATFORM API. DO NOT EDIT\n"
104
+ output += yaml . safeDump ( { items : sourcesUpdated } , options ) ;
105
+ fs . writeFileSync ( path . resolve ( __dirname , `../src/_data/catalogV2/sources.yml` ) , output ) ;
106
+
107
+ // Create source-category mapping yaml file
108
+ output = "# AUTOGENERATED FROM PLATFORM API. DO NOT EDIT\n"
109
+ output += yaml . safeDump ( { items : sourceCategories } , options ) ;
110
+ fs . writeFileSync ( path . resolve ( __dirname , `../src/_data/catalogV2/source_categories.yml` ) , output ) ;
111
+ }
112
+
113
+ const updateDestinations = async ( ) => {
114
+ let destinations = [ ]
115
+ let destinationsUpdated = [ ]
116
+ let destinationCategories = new Set ( ) ;
117
+ let nextPageToken = null
118
+
119
+ while ( nextPageToken !== "" ) {
120
+ const res = await getCatalog ( `${ PLATFORM_API_URL } /v1beta/catalog/destinations` , nextPageToken )
121
+ destinations = destinations . concat ( res . destinations )
122
+ nextPageToken = res . next_page_token
123
+ }
124
+ destinations . sort ( ( a , b ) => {
125
+ if ( a . display_name < b . display_name ) { return - 1 ; }
126
+ if ( a . display_name > b . display_name ) { return 1 ; }
127
+ return 0 ;
128
+ } )
129
+ destinations . forEach ( destination => {
130
+ let slug = slugify ( destination . display_name )
131
+
132
+ let categories = [ destination . categories . primary , destination . categories . secondary , ...destination . categories . additional ]
133
+ categories = categories . filter ( category => category != '' )
134
+
135
+ let updatedDestination = {
136
+ display_name : destination . display_name ,
137
+ slug,
138
+ name : destination . name ,
139
+ description : destination . description ,
140
+ url : `connections/destinations/catalog/${ slug } ` ,
141
+ status : destination . status ,
142
+ logo : {
143
+ url : destination . logos . logo
144
+ } ,
145
+ mark : {
146
+ url : destination . logos . mark
147
+ } ,
148
+ categories
149
+ }
150
+ destinationsUpdated . push ( updatedDestination )
151
+
152
+ // add unique destination categories to set
153
+ categories . reduce ( ( s , e ) => s . add ( e ) , destinationCategories ) ;
154
+ } )
155
+
156
+ const destinationArray = Array . from ( categories )
157
+ destinationArray . forEach ( category => {
158
+ destinationCategories . push ( {
159
+ display_name : category ,
160
+ slug : slugify ( category )
161
+ } )
162
+ } )
163
+
164
+ // Create destination catalog yaml file
165
+ const options = { noArrayIndent : true } ;
166
+ let output = "# AUTOGENERATED FROM PLATFORM API. DO NOT EDIT\n"
167
+ output += yaml . safeDump ( { items : destinationsUpdated } , options ) ;
168
+ fs . writeFileSync ( path . resolve ( __dirname , `../src/_data/catalogV2/destinations.yml` ) , output ) ;
169
+
170
+ // Create source-category mapping yaml file
171
+ let output = "# AUTOGENERATED FROM PLATFORM API. DO NOT EDIT\n"
172
+ output += yaml . safeDump ( { items : destinationCategories } , options ) ;
173
+ fs . writeFileSync ( path . resolve ( __dirname , `../src/_data/catalogV2/destination_categories.yml` ) , output ) ;
174
+ }
175
+
176
+ updateSources ( )
177
+ updateDestinations ( )
0 commit comments