Skip to content
This repository was archived by the owner on Mar 2, 2021. It is now read-only.

Commit 2c220dc

Browse files
author
Hugo Vieilledent
committed
feat: each track.url update matching providerTrack
1 parent 7c5d13b commit 2c220dc

File tree

9 files changed

+516
-236
lines changed

9 files changed

+516
-236
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# config
44
.env
55

6+
# secrets
7+
.runtimeconfig.json
8+
69
# compiled output
710
/dist
811
/tmp

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.11.5

.runtimeconfig.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,16 @@ Firebase allowed this project to come to life without having the need to spend t
195195

196196
Do you want your project to appear here? Send a pull request or get in touch.
197197

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).

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
const functions = require('firebase-functions')
22
const app = require('./src/app')
3+
const onTrackUrlChange = require('./src/on-track-url-change')
34

5+
// Public API for Radio4000
46
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)

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"scripts": {
88
"start": "firebase serve --only hosting,functions --port 4001",
99
"test": "ava",
10+
"firebase:shell": "firebase functions:shell",
1011
"deploy-api": "firebase use staging; firebase deploy --only functions",
1112
"deploy-api-production": "firebase use production; firebase deploy --only functions",
1213
"deploy-rules": "firebase use staging; firebase deploy --only database",
@@ -16,14 +17,16 @@
1617
"body-parser": "^1.17.2",
1718
"cors": "^2.8.4",
1819
"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",
2122
"got": "^6.7.1",
2223
"radio4000-sdk": "^0.0.5",
23-
"stripe": "^4.24.0"
24+
"stripe": "^4.24.0",
25+
"youtube-regex": "^1.0.5"
2426
},
2527
"devDependencies": {
2628
"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"
2831
}
2932
}

src/on-track-url-change.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
*/

src/utils/youtube-url-to-id.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

0 commit comments

Comments
 (0)