@@ -4,7 +4,7 @@ const yaml = require('yaml-front-matter');
4
4
5
5
function findVideoFilesRecursive ( dir , arrayOfFiles ) {
6
6
const files = fs . readdirSync ( dir ) ;
7
-
7
+
8
8
arrayOfFiles = arrayOfFiles || [ ] ;
9
9
10
10
for ( const file of files ) {
@@ -31,28 +31,27 @@ function getPlaylist(file) {
31
31
}
32
32
33
33
function getVideoData ( ) {
34
-
35
34
const directories = [
36
- '_learning' ,
37
- '_beginners' ,
38
- '_more' ,
39
- '_challenges' ,
35
+ // '_learning',
36
+ // '_beginners',
37
+ // '_more',
38
+ // '_challenges',
40
39
'_CodingChallenges' ,
41
- '_Courses' ,
42
- '_GuestTutorials' ,
43
- '_Streams' ,
44
- '_TeachableMachine' ,
45
- '_Tutorials' ,
40
+ // '_Courses',
41
+ // '_GuestTutorials',
42
+ // '_Streams',
43
+ // '_TeachableMachine',
44
+ // '_Tutorials',
46
45
] ;
47
46
48
47
let files = [ ] ;
49
48
for ( const dir of directories ) {
50
49
findVideoFilesRecursive ( dir , files ) ;
51
50
}
52
-
53
- const videos = [ ] ;
54
51
52
+ const videos = [ ] ;
55
53
for ( const file of files ) {
54
+ console . log ( file ) ;
56
55
const content = fs . readFileSync ( `./${ file } ` , 'UTF8' ) ;
57
56
const parsed = yaml . loadFront ( content ) ;
58
57
let url = file . substring ( 1 ) ;
@@ -68,33 +67,31 @@ function getVideoData() {
68
67
}
69
68
70
69
function primeDirectory ( dir ) {
71
-
72
70
fs . rmdirSync ( dir , { recursive : true } , ( err ) => {
73
71
if ( err ) {
74
- throw err ;
72
+ throw err ;
75
73
}
76
74
} ) ;
77
-
78
- fs . mkdirSync ( dir , err => {
79
- if ( err ) {
75
+
76
+ fs . mkdirSync ( dir , ( err ) => {
77
+ if ( err ) {
80
78
throw err ;
81
79
}
82
80
} ) ;
83
-
84
81
}
85
82
86
83
function getVideoID ( url ) {
87
84
const location = url . substring ( 1 , url . length ) ;
88
85
let page ;
89
86
try {
90
87
// link to page on the site
91
- page = fs . readFileSync ( `./_${ location } .md` , " UTF8" ) ;
88
+ page = fs . readFileSync ( `./_${ location } .md` , ' UTF8' ) ;
92
89
} catch ( err ) {
93
90
try {
94
91
// link to series on site
95
92
const files = fs . readdirSync ( `./_${ location } ` ) ;
96
93
// get first page in series
97
- page = fs . readFileSync ( `./_${ location } /${ files [ 0 ] } .md` , " UTF8" ) ;
94
+ page = fs . readFileSync ( `./_${ location } /${ files [ 0 ] } .md` , ' UTF8' ) ;
98
95
} catch ( e ) {
99
96
// link to youtube playlist
100
97
return url ;
@@ -105,16 +102,14 @@ function getVideoID(url) {
105
102
}
106
103
107
104
function writeDescriptions ( videos ) {
108
-
109
105
primeDirectory ( './_descriptions' ) ;
110
106
111
107
for ( let i = 0 ; i < videos . length ; i ++ ) {
112
-
113
108
const data = videos [ i ] . data ;
114
109
const pageURL = videos [ i ] . pageURL ;
115
110
const playlist = videos [ i ] . playlist ;
116
111
117
- let description = "" ;
112
+ let description = '' ;
118
113
119
114
// Description
120
115
let content = data . __content ;
@@ -132,7 +127,6 @@ function writeDescriptions(videos) {
132
127
if ( data . variations ) {
133
128
for ( let j = 0 ; j < data . variations . length ; ++ j ) {
134
129
if ( data . variations [ j ] . web_editor ) {
135
-
136
130
if ( ! hasWebEditorVariations ) {
137
131
description += '\np5.js Web Editor Sketches:\n' ;
138
132
@@ -144,7 +138,6 @@ function writeDescriptions(videos) {
144
138
description += `🕹️ ${ data . variations [ j ] . name } : https://editor.p5js.org/codingtrain/sketches/${ data . variations [ j ] . web_editor } \n` ;
145
139
146
140
hasWebEditorVariations = true ;
147
-
148
141
}
149
142
}
150
143
}
@@ -156,7 +149,10 @@ function writeDescriptions(videos) {
156
149
// Next Video / Previous Video / Playlist
157
150
let nextID ;
158
151
if ( i !== videos . length - 1 ) {
159
- if ( pageURL . substring ( 0 , pageURL . lastIndexOf ( '/' ) ) === videos [ i + 1 ] . pageURL . substring ( 0 , videos [ i + 1 ] . pageURL . lastIndexOf ( '/' ) ) ) {
152
+ if (
153
+ pageURL . substring ( 0 , pageURL . lastIndexOf ( '/' ) ) ===
154
+ videos [ i + 1 ] . pageURL . substring ( 0 , videos [ i + 1 ] . pageURL . lastIndexOf ( '/' ) )
155
+ ) {
160
156
nextID = videos [ i + 1 ] . data . video_id ;
161
157
} else {
162
158
nextID = false ;
@@ -167,7 +163,10 @@ function writeDescriptions(videos) {
167
163
168
164
let previousID ;
169
165
if ( i !== 0 ) {
170
- if ( pageURL . substring ( 0 , pageURL . lastIndexOf ( '/' ) ) === videos [ i - 1 ] . pageURL . substring ( 0 , videos [ i - 1 ] . pageURL . lastIndexOf ( '/' ) ) ) {
166
+ if (
167
+ pageURL . substring ( 0 , pageURL . lastIndexOf ( '/' ) ) ===
168
+ videos [ i - 1 ] . pageURL . substring ( 0 , videos [ i - 1 ] . pageURL . lastIndexOf ( '/' ) )
169
+ ) {
171
170
previousID = videos [ i - 1 ] . data . video_id ;
172
171
} else {
173
172
previousID = false ;
@@ -198,34 +197,36 @@ function writeDescriptions(videos) {
198
197
199
198
// Links
200
199
if ( data . links ) {
201
- description += " \nLinks discussed in this video:\n" ;
200
+ description += ' \nLinks discussed in this video:\n' ;
202
201
for ( let i = 0 ; i < data . links . length ; ++ i ) {
203
202
const url = data . links [ i ] . url ;
204
- if ( / h t t p s ? : \/ \/ .* / . test ( url ) ) { // starts with http:// or https://
205
- description += `🔗 ${ data . links [ i ] . title } : ${ url } \n`
206
- } else { // assume relative link in thecodingtrain.com
207
- description += `🔗 ${ data . links [ i ] . title } : https://thecodingtrain.com${ url } \n`
203
+ if ( / h t t p s ? : \/ \/ .* / . test ( url ) ) {
204
+ // starts with http:// or https://
205
+ description += `🔗 ${ data . links [ i ] . title } : ${ url } \n` ;
206
+ } else {
207
+ // assume relative link in thecodingtrain.com
208
+ description += `🔗 ${ data . links [ i ] . title } : https://thecodingtrain.com${ url } \n` ;
208
209
}
209
210
}
210
211
}
211
212
212
213
// Videos
213
214
if ( data . videos ) {
214
- description += " \nOther videos mentioned in this video:\n" ;
215
+ description += ' \nOther videos mentioned in this video:\n' ;
215
216
for ( let i = 0 ; i < data . videos . length ; ++ i ) {
216
217
if ( data . videos [ i ] . video_id ) {
217
- description += `🎥 ${ data . videos [ i ] . title } : https://youtu.be/${ data . videos [ i ] . video_id } \n`
218
+ description += `🎥 ${ data . videos [ i ] . title } : https://youtu.be/${ data . videos [ i ] . video_id } \n` ;
218
219
} else if ( data . videos [ i ] . url ) {
219
- description += `🎥 ${ data . videos [ i ] . title } : ${ getVideoID ( data . videos [ i ] . url ) } \n`
220
+ description += `🎥 ${ data . videos [ i ] . title } : ${ getVideoID ( data . videos [ i ] . url ) } \n` ;
220
221
}
221
222
}
222
223
}
223
224
224
225
// Timestamps
225
226
if ( data . topics ) {
226
- description += " \nTimestamps:\n" ;
227
+ description += ' \nTimestamps:\n' ;
227
228
for ( let i = 0 ; i < data . topics . length ; ++ i ) {
228
- description += `${ data . topics [ i ] . time } ${ data . topics [ i ] . title } \n`
229
+ description += `${ data . topics [ i ] . time } ${ data . topics [ i ] . title } \n` ;
229
230
}
230
231
}
231
232
@@ -253,15 +254,15 @@ function writeDescriptions(videos) {
253
254
254
255
This description was auto-generated. If you see a problem, please open an issue: https://github.com/CodingTrain/website/issues/new` ;
255
256
256
- fs . writeFileSync ( `_descriptions/${ data . video_id } .txt` , description ) ;
257
- }
257
+ //fs.writeFileSync(`_descriptions/${data.video_id}.txt`, description);
258
258
259
+ let filename = / \/ ( (?: .(? ! \/ ) ) + ) $ / . exec ( pageURL ) [ 1 ] ;
260
+ fs . writeFileSync ( `_descriptions/${ filename } .txt` , description ) ;
261
+ }
259
262
}
260
263
261
264
( ( ) => {
262
-
263
- console . log ( "💫 Generating YouTube Descriptions 💫" )
265
+ console . log ( '💫 Generating YouTube Descriptions 💫' ) ;
264
266
265
267
writeDescriptions ( getVideoData ( ) ) ;
266
-
267
268
} ) ( ) ;
0 commit comments