-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathinstafetch.js
106 lines (91 loc) · 3.21 KB
/
instafetch.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
var MAX_RETURN = 33;
function Instafetch(accessToken) {
this.accessToken = accessToken;
this.baseUrl = 'https://api.instagram.com/v1/';
}
Instafetch.prototype.fetch = function(params) {
// If a neither tags nor users were specified
if(!params.hasOwnProperty('tag') && !params.hasOwnProperty('user')) {
throw Error('Please specify either an user ID or a tag');
}
// Initiate a temporary store of the results
var filteredArr = [];
// Continuation from previews results, if exists
if(params.hasOwnProperty('tmpArr') && params.tmpArr.length > 0) {
filteredArr = params.tmpArr;
}
if(!params.hasOwnProperty('limit')) {
params.limit = MAX_RETURN;
}
var parent = this;
// Build the API URL
var apiUrl = this.baseUrl;
// If a user is specified, always get all media by user first
if(params.hasOwnProperty('user')) {
apiUrl += 'users/' + params.user + '/media/recent/?access_token=' + this.accessToken + '&callback=callbackFunction&count=';
// If no tags were specified, get based on the limit
if(!params.hasOwnProperty('tag')) {
apiUrl += params.limit;
}
// Otherwise get as much as possible
else {
apiUrl += MAX_RETURN;
}
}
// Otherwise, get the tags based on the limit
else {
apiUrl += 'tags/' + params.tag + '/media/recent/?access_token=' + this.accessToken + '&callback=callbackFunction&count=' + params.limit;
}
if (params.hasOwnProperty('maxId')) {
apiUrl += '&max_id=' + params.maxId;
}
// Call the API
$.ajax({
url: apiUrl,
jsonp: "callback",
dataType: "jsonp",
data: {
q: "",
format: "json"
},
success: function(response) {
/* FILTER */
// If only one parameter is specified, no need to filter
if(params.hasOwnProperty('user') != params.hasOwnProperty('tag')) {
filteredArr = filteredArr.concat(response.data);
}
// Otherwise, since user data is always fetched first, filter by tag
else {
for(var i = 0; i < response.data.length; i++) {
if(response.data[i].tags.indexOf(params.tag) > -1) {
filteredArr.push(response.data[i]);
}
}
}
/* CHECK */
// Check if the limit has been reached, or if no more images are available
if((~~filteredArr.length) >= params.limit || typeof response.pagination.next_max_id === 'undefined') {
// Structure the returnObj in a similar fashion to how Instagram returns it
var returnObj = {};
returnObj.data = [];
// Ensures only return up to the limit, or the number available, whichever is smaller
for(var j = 0; j < Math.min(filteredArr.length, params.limit); j++) {
returnObj.data.push(filteredArr[j]);
}
if(params.hasOwnProperty('callback')) {
params.callback(returnObj, params.params);
}
} else {
parent.fetch({
user: params.user,
tag: params.tag,
limit: params.limit,
callback: params.callback,
params: params.params,
maxId: response.pagination.next_max_id,
tmpArr: filteredArr
});
}
}
});
}