This repository was archived by the owner on Mar 2, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 9 files changed +516
-236
lines changed Expand file tree Collapse file tree 9 files changed +516
-236
lines changed Original file line number Diff line number Diff line change 3
3
# config
4
4
.env
5
5
6
+ # secrets
7
+ .runtimeconfig.json
8
+
6
9
# compiled output
7
10
/dist
8
11
/tmp
Original file line number Diff line number Diff line change
1
+ 6.11.5
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -195,3 +195,16 @@ Firebase allowed this project to come to life without having the need to spend t
195
195
196
196
Do you want your project to appear here? Send a pull request or get in touch.
197
197
198
+ ## Developement
199
+
200
+ ` nvm use ` will tell nvm to use the right ` nodejs ` version for this
201
+ Firebase Function project. It reads it from the ` .nvmrc ` file.
202
+
203
+ ` firebase functions:config:get > .runtimeconfig.json ` will download
204
+ locally the secret keys saved in the project environment. You will
205
+ need them to run the API.
206
+
207
+ ` yarn start ` will run the API.
208
+
209
+ ` yarn firebase:shell ` will run the Firebase shell, with which can
210
+ locally be testes the non-https functions. See the [ documentation] ( https://firebase.google.com/docs/functions/local-emulator ) .
Original file line number Diff line number Diff line change 1
1
const functions = require ( 'firebase-functions' )
2
2
const app = require ( './src/app' )
3
+ const onTrackUrlChange = require ( './src/on-track-url-change' )
3
4
5
+ // Public API for Radio4000
4
6
exports . api = functions . https . onRequest ( app )
7
+
8
+ // Listen to every track change
9
+ exports . onTrackUrlChange =
10
+ functions . database . ref ( '/tracks/{trackId}/url' )
11
+ . onWrite ( onTrackUrlChange )
Original file line number Diff line number Diff line change 7
7
"scripts" : {
8
8
"start" : " firebase serve --only hosting,functions --port 4001" ,
9
9
"test" : " ava" ,
10
+ "firebase:shell" : " firebase functions:shell" ,
10
11
"deploy-api" : " firebase use staging; firebase deploy --only functions" ,
11
12
"deploy-api-production" : " firebase use production; firebase deploy --only functions" ,
12
13
"deploy-rules" : " firebase use staging; firebase deploy --only database" ,
16
17
"body-parser" : " ^1.17.2" ,
17
18
"cors" : " ^2.8.4" ,
18
19
"express" : " ^4.15.3" ,
19
- "firebase-admin" : " ^5.10.0 " ,
20
- "firebase-functions" : " ^0.8.2 " ,
20
+ "firebase-admin" : " ^5.12.1 " ,
21
+ "firebase-functions" : " ^1.1.0 " ,
21
22
"got" : " ^6.7.1" ,
22
23
"radio4000-sdk" : " ^0.0.5" ,
23
- "stripe" : " ^4.24.0"
24
+ "stripe" : " ^4.24.0" ,
25
+ "youtube-regex" : " ^1.0.5"
24
26
},
25
27
"devDependencies" : {
26
28
"ava" : " ^0.21.0" ,
27
- "firebase-tools" : " ^3.17.7"
29
+ "firebase-tools" : " ^3.17.7" ,
30
+ "@google-cloud/functions-emulator" : " ^1.0.0-beta.4"
28
31
}
29
32
}
Original file line number Diff line number Diff line change
1
+ const functions = require ( 'firebase-functions' )
2
+ const admin = require ( 'firebase-admin' ) ;
3
+ const getYoutubeId = require ( './utils/youtube-url-to-id.js' )
4
+
5
+ /*
6
+ To test the execution of this function
7
+ you can run `yarn firebase:shell`
8
+ and use the following mockup call
9
+
10
+ onTrackUrlChange({before: 'youtu.be/xIaco5AQrUQ', after: 'https://www.youtube.com/watch?v=OkR7UNnQU6c' })
11
+ */
12
+
13
+ const onTracksChange = ( snapshot , context ) => {
14
+ // console.log('snapshot: ', snapshot)
15
+ // console.log('context: ', context)
16
+
17
+ let db = admin . database ( ) ;
18
+
19
+ let newUrl = snapshot . after . val ( )
20
+ let providerId = getYoutubeId ( newUrl )
21
+ let providerName = 'youtube'
22
+
23
+ // abort if no ID in provider URL
24
+ if ( ! providerId ) {
25
+ return false
26
+ }
27
+
28
+ const ref = db . ref ( `/providerTracks/${ providerName } :${ providerId } /tracks/${ context . params . trackId } ` ) ;
29
+
30
+ return ref . set ( true ) ;
31
+ }
32
+
33
+ module . exports = onTracksChange
34
+
35
+ /*
36
+ Model `providerTrack`:
37
+ - tracks: list of tracks that reference this providerTrack
38
+ - discogsReleaseId: id of this track's release on discogs
39
+ */
Original file line number Diff line number Diff line change
1
+ const youtubeRegex = require ( 'youtube-regex' )
2
+
3
+ const youtubeUrlToId = function ( url ) {
4
+ const results = youtubeRegex ( ) . exec ( url ) ;
5
+ if ( ! results ) {
6
+ return false ;
7
+ }
8
+ return results [ 1 ] ;
9
+ }
10
+
11
+ module . exports = youtubeUrlToId
You can’t perform that action at this time.
0 commit comments