-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstarter.js
71 lines (61 loc) · 1.71 KB
/
starter.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var request = require('superagent');
var Rx = require('rx');
var clientId = process.argv[2];
if (!clientId) {
console.error('Run with Instagram client_id:\nnpm start -- <client_id>\n');
process.exit(0);
}
var participant = 'anonymous';
var interval = 5; // seconds
var postEndpoint = 'http://rxdisplay.neueda.lv/in';
var instagramTag = 'ничоси';
// Step 0: Helper to create Observable instances from HTTP requests
Rx.Observable.fromRequest = function(req) {
return Rx.Observable.create(function(observable) {
req.end(function(err, res) {
if (err) {
observable.onError(err);
} else {
observable.onNext(res);
}
observable.onCompleted();
})
});
};
// Step 1: Fetch from Instagram by tag
var apiRoot = 'https://api.instagram.com/v1/';
var fromInstagramByTag = function(tag) {
var encodedTag = encodeURIComponent(instagramTag);
var url = ???;
return Rx.Observable.???(request.get(url));
};
// Step 2: Fetch with an interval and flatten
var ticker = Rx.Observable.???;
var rawPics = ticker.flatMap(function() {
return fromInstagramByTag(instagramTag).???(function(res) {
return res.body.data;
});
});
// Step 3: Filter and restructure data, then send to UI
var uniquePics = rawPics.???(function (pic) {
return ???;
});
var pics = uniquePics.???(function(pic) {
return {
tag: instagramTag,
???
};
});
pics.subscribe(function(pic) {
var req = request.post(postEndpoint).send(pic);
Rx.Observable.fromRequest(req)
.subscribe(function(res) {
console.log(postEndpoint + ' <- ' + pic.url);
}, function(err) {
if (err.response) {
console.log(err.response.text.trim());
} else {
console.log(err.code);
}
});
});