-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcastsAnalyzer.js
41 lines (34 loc) · 1.12 KB
/
castsAnalyzer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const _ = require('lodash');
const request = require('request');
const Rx = require('@reactivex/rxjs');
const castsApi = 'http://casts-api-production.herokuapp.com/videos';
//const castsApi = 'http://localhost:3000/videos';
module.exports = class CastsAnalyzer {
constructor () {
this.cache = new Map();
}
getRelatedVideos (fileMetaData) {
let tags = fileMetaData.yamlProperties.tags && fileMetaData.yamlProperties.tags.join(',');
if (this.cache.has(tags)) {
console.log(`serving from cache for: ${tags}`);
return Rx.Observable.of(this.cache.get(tags));
}
return new Rx.Observable(obs => {
console.log(`requesting for: ${tags}`);
request(`${castsApi}?tags=${tags}&per_page=6`, (error, response, body) => {
if (!error && response.statusCode === 200) {
let data = JSON.parse(body)
if (data) {
let videos = data.data.map(video => video.id).slice(0, 6);
this.cache.set(tags, videos)
obs.next(videos);
obs.complete();
}
}
else {
obs.error(error);
}
});
});
}
}