forked from MrSwitch/hello.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram.js
162 lines (127 loc) · 3.21 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
(function(hello) {
hello.init({
instagram: {
name: 'Instagram',
oauth: {
// SEE http://instagram.com/developer/authentication/
version: 2,
auth: 'https://instagram.com/oauth/authorize/',
grant: 'https://api.instagram.com/oauth/access_token'
},
// Refresh the access_token once expired
refresh: true,
scope: {
basic: 'basic',
friends: 'relationships',
publish: 'likes comments'
},
scope_delim: ' ',
login: function(p) {
// Instagram throws errors like 'Javascript API is unsupported' if the display is 'popup'.
// Make the display anything but 'popup'
p.qs.display = '';
},
base: 'https://api.instagram.com/v1/',
get: {
me: 'users/self',
'me/feed': 'users/self/feed?count=@{limit|100}',
'me/photos': 'users/self/media/recent?min_id=0&count=@{limit|100}',
'me/friends': 'users/self/follows?count=@{limit|100}',
'me/following': 'users/self/follows?count=@{limit|100}',
'me/followers': 'users/self/followed-by?count=@{limit|100}',
'friend/photos': 'users/@{id}/media/recent?min_id=0&count=@{limit|100}'
},
post: {
'me/like': function(p, callback) {
var id = p.data.id;
p.data = {};
callback('media/' + id + '/likes');
}
},
del: {
'me/like': 'media/@{id}/likes'
},
wrap: {
me: function(o) {
formatError(o);
if ('data' in o) {
o.id = o.data.id;
o.thumbnail = o.data.profile_picture;
o.name = o.data.full_name || o.data.username;
}
return o;
},
'me/friends': formatFriends,
'me/following': formatFriends,
'me/followers': formatFriends,
'me/photos': function(o) {
formatError(o);
paging(o);
if ('data' in o) {
o.data = o.data.filter(function(d) {
return d.type === 'image';
});
o.data.forEach(function(d) {
d.thumbnail = d.images.thumbnail.url;
d.picture = d.images.standard_resolution.url;
d.name = d.caption ? d.caption.text : null;
});
}
return o;
},
'default': function(o) {
paging(o);
return o;
}
},
// Instagram does not return any CORS Headers
// So besides JSONP we're stuck with proxy
xhr: function(p, qs) {
var method = p.method;
var proxy = method !== 'get';
if (proxy) {
if ((method === 'post' || method === 'put') && p.query.access_token) {
p.data.access_token = p.query.access_token;
delete p.query.access_token;
}
// No access control headers
// Use the proxy instead
p.proxy = proxy;
}
return proxy;
},
// No form
form: false
}
});
function formatError(o) {
if (o && 'meta' in o && 'error_type' in o.meta) {
o.error = {
code: o.meta.error_type,
message: o.meta.error_message
};
}
}
function formatFriends(o) {
paging(o);
if (o && 'data' in o) {
o.data.forEach(formatFriend);
}
return o;
}
function formatFriend(o) {
if (o.id) {
o.thumbnail = o.profile_picture;
o.name = o.full_name || o.username;
}
}
// See: http://instagram.com/developer/endpoints/
function paging(res) {
if ('pagination' in res) {
res.paging = {
next: res.pagination.next_url
};
delete res.pagination;
}
}
})(hello);