-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstagram.js
119 lines (102 loc) · 3.47 KB
/
instagram.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var Instagram = require('instagram-node-lib');
var config = require('config-heroku')
var request = require('request');
var qs = require('querystring');
var instagram = {
initialize: function() {
Instagram.set('client_id', config.instagram.client_id);
Instagram.set('client_secret', config.instagram.client_secret);
Instagram.set('callback_url', config.root_url + '/ig/callback');
Instagram.set('redirect_uri', config.root_url);
Instagram.set('maxSockets', 10);
Instagram.subscriptions.unsubscribe_all({});
},
handshake: function(req, res) {
Instagram.subscriptions.handshake(req, res);
},
subscribeByHashtag: function(hashTag) {
Instagram.subscriptions.subscribe({
object: 'tag',
object_id: hashTag,
aspect: 'media',
callback_url: config.root_url + '/ig/callback',
type: 'subscription',
id: '#'
});
console.log("Subscribed to hash tag: " + hashTag);
},
findRecentByHashtag: function(hashTag, callback) {
console.log("Recent pictures for " + hashTag + " queried");
Instagram.tags.recent({
name: hashTag,
complete: function(data) {
console.log("Recent pictures for " + hashTag + " received");
return callback(null, data);
},
error: function(errorMessage, errorObject, caller){
callback(errorObject, null);
}
});
},
parseUpdateObjects: function(updateObjects, callback) {
if ( Object.prototype.toString.call(updateObjects) === '[object Array]') {
updateObjects.forEach(function(tag) {
if (tag.object_id != null) {
var hashTag = tag.object_id;
console.log("Received update object for " + hashTag + " hash tag");
return callback(hashTag);
}
});
}
},
filterLocationPictures: function(pictures) {
var locationPictures = []
pictures.forEach(function(picture) {
if (picture.location != null && picture.location.longitude && picture.location.latitude){
locationPictures.push(picture);
}
});
return locationPictures;
},
unsubscribeByHashTag: function(hashTag) {
var url = 'https://api.instagram.com/v1/subscriptions?';
var params = {
client_id: config.instagram.client_id,
client_secret: config.instagram.client_secret,
}
url += qs.stringify(params)
request(url, function(error, response, body) {
jsonBody = JSON.parse(body);
if (jsonBody.meta.code === 200) {
var subscriptions = jsonBody.data;
subscriptions.forEach(function(subscription){
if (subscription.object_id === hashTag) {
Instagram.subscriptions.unsubscribe({id: subscription.id});
console.log("Unsubscribed to " + hashTag);
}
})
}
});
},
getStatus: function(callback) {
var url = 'https://api.instagram.com/v1/subscriptions?client_secret=' + config.instagram.client_secret + '&client_id=' + config.instagram.client_id;
request(url, function(error, response, body) {
if(error) {
callback(error, null);
}
else {
var parsedResponse = {
status_code: response.statusCode,
rate_limit: response.headers['x-ratelimit-limit'],
queries_remaing: response.headers['x-ratelimit-remaining'],
}
var jsonBody = JSON.parse(body)
if (jsonBody.data) {
parsedResponse.current_subscriptions = jsonBody.data
}
callback(null, parsedResponse);
}
});
}
}
module.exports = instagram;