-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (65 loc) · 1.87 KB
/
app.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var http = require('http'),
https = require('https');
var access_token = process.env.access_token;
var url = 'https://graph.facebook.com/me/links?method=GET&format=json&suppress_http_code=1&access_token=' + access_token;
var setMapping = function(){
var options = {
host: process.env.ES_PORT_9200_TCP_ADDR,
port: process.env.ES_PORT_9200_TCP_PORT,
path: '/links',
method: 'POST'
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Mapping set');
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(JSON.stringify({ "mappings": { "link": { "properties": { "link": { "type": "string", "analyzer": "simple" } } } } }));
req.end();
}
var getPosts = function(url){
console.log(url);
https.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var response = JSON.parse(body);
response.data.forEach(function(it){
save(it);
});
var paging = response.paging;
if(paging && paging.next){
getPosts(paging.next);
}
});
}).on('error', function(e){
console.log('Get error: ' + e.message);
});
}
var save = function(it){
var options = {
host: process.env.ES_PORT_9200_TCP_ADDR,
port: process.env.ES_PORT_9200_TCP_PORT,
path: '/links/link/' + it.id,
method: 'PUT'
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(JSON.stringify({'link': it.link, 'name': it.name, 'description': it.description, 'picture': it.picture, 'created_time': it.created_time, 'message': it.message}));
req.end();
}
setMapping();
getPosts(url);