-
Notifications
You must be signed in to change notification settings - Fork 1
/
instagram_session.dart
245 lines (207 loc) · 9.92 KB
/
instagram_session.dart
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
part of dart.instagram_api;
class InstagramSession {
String _accessToken;
User _currentUser;
String _clientId;
InstagramSession(this._accessToken);
String get accessToken => _accessToken;
set accessToken (String token) => _accessToken = token;
User get currentUser => _currentUser;
set currentUser (User user) => _currentUser = user;
String get clientId => _clientId;
set clientId (String id) => _clientId = id;
/* User endpoints from http://instagram.com/developer/endpoints/users/ */
/* Get basic information about a user. */
User getUserById(int userId) {
Map<String, String> data = new Map<String, String>();
data['user_id'] = userId.toString();
String uri = UriConstructor.constructUri(UriFactory.USER_GET_DATA, data);
data.clear();
data['access_token'] = accessToken;
String jsonUser = doGet(uri, data);
Map request = JSON.decode(jsonUser);
User user = new User(accessToken, request['data']);
return user;
}
/* See the authenticated user's feed. May return a mix of both image and video types. */
PaginatedCollection<Media> getUserSelfFeed() {
String uri = UriFactory.USER_GET_SELF_FEED;
List<Media> medias = new List<Media>();
PaginationIterator<Media> iterator =
new PaginationIterator<Media>(medias, uri+'?access_token='+accessToken, ModelType.MEDIA);
return new PaginatedCollection<Media>(medias, iterator);
}
/* Get the most recent media published by a user. May return a mix of both image and video types. */
PaginatedCollection<Media> getRecentPublishedMedia(int userId) {
Map<String, String> data = new Map<String, String>();
data['user_id'] = userId.toString();
String uri = UriConstructor.constructUri(UriFactory.USER_GET_RECENT_MEDIA, data);
data.clear();
List<Media> medias = new List<Media>();
PaginationIterator<Media> iterator =
new PaginationIterator<Media>(medias, uri+'?access_token='+accessToken, ModelType.MEDIA);
return new PaginatedCollection<Media>(medias, iterator);
}
/*
* See the authenticated user's list of media they've liked. May return a mix of both image and video types.
* Note: This list is ordered by the order in which the user liked the media.
* Private media is returned as long as the authenticated user has permission to view that media.
* Liked media lists are only available for the currently authenticated user.
*/
PaginatedCollection<Media> getUserLikedMedia() {
String uri = UriFactory.USER_GET_LIKED_MEDIA;
List<Media> medias = new List<Media>();
PaginationIterator<Media> iterator =
new PaginationIterator<Media>(medias, uri+'?access_token='+accessToken, ModelType.MEDIA);
return new PaginatedCollection<Media>(medias, iterator);
}
/* Search for a user by name */
List<User> searchUserByName(String username) {
Map<String, String> data = new Map<String, String>();
data['q'] = username;
data['access_token'] = accessToken;
String uri = UriConstructor.constructUri(UriFactory.USER_SEARCH_USER_BY_NAME, data);
String jsonUsers = doGet(uri, data);
Map request = JSON.decode(jsonUsers);
List<User> users = new List<User>();
request['data'].forEach((jsonUser) {
users.add(new User(accessToken, jsonUser));
});
return(users);
}
/* Relationship Endpoints from http://instagram.com/developer/endpoints/relationships */
/* Get the list of users this user follows. */
PaginatedCollection<User> getUserFollows(int userId) {
Map<String, String> data = new Map<String, String>();
data['user_id'] = userId.toString();
String uri = UriConstructor.constructUri(UriFactory.RELATIONSHIP_GET_FOLLOWS, data);
data.clear();
List<User> users = new List<User>();
PaginationIterator<User> iterator =
new PaginationIterator<User>(users, uri+'?access_token='+accessToken, ModelType.USER);
return new PaginatedCollection<User>(users, iterator);
}
/* Get the list of users this user is followed by. */
PaginatedCollection<User> getUserFollowedBy(int userId) {
Map<String, String> data = new Map<String, String>();
data['user_id'] = userId.toString();
String uri = UriConstructor.constructUri(UriFactory.RELATIONSHIP_GET_FOLLOWERS, data);
data.clear();
List<User> users = new List<User>();
PaginationIterator<User> iterator =
new PaginationIterator<User>(users, uri+'?access_token='+accessToken, ModelType.USER);
return new PaginatedCollection<User>(users, iterator);
}
/* List the users who have requested this user's permission to follow. */
PaginatedCollection<User> getUserRequestedBy(int userId) {
Map<String, String> data = new Map<String, String>();
data['user_id'] = userId.toString();
String uri = UriConstructor.constructUri(UriFactory.RELATIONSHIP_GET_FOLLOW_REQUESTS, data);
data.clear();
List<User> users = new List<User>();
PaginationIterator<User> iterator =
new PaginationIterator<User>(users, uri+'?access_token='+accessToken, ModelType.USER);
return new PaginatedCollection<User>(users, iterator);
}
/* Get information about a relationship to another user. */
Relationship getUserRelationship(int userId) {
Map<String, String> data = new Map<String, String>();
data['user_id'] = userId.toString();
String uri = UriConstructor.constructUri(UriFactory.RELATIONSHIP_GET_RELATIONSHIP_STATUS, data);
data.clear();
data['access_token'] = accessToken;
String jsonRelationship = doGet(uri, data);
Map request = JSON.decode(jsonRelationship);
Relationship relationship = new Relationship(accessToken, request['data']);
return relationship;
}
/* Modify the relationship between the current user and the target user. */
Relationship modifyRelation(int userId, String relationshipStatus) {
Map<String, String> data = new Map<String, String>();
data['user_id'] = userId.toString();
String uri = UriConstructor.constructUri(UriFactory.RELATIONSHIP_GET_RELATIONSHIP_STATUS, data);
data.clear();
data['access_token'] = accessToken;
uri = UriConstructor.uriFromMap(uri, data);
data.clear();
data['action'] = relationshipStatus;
String jsonRelationship = doPost(uri, data);
Map request = JSON.decode(jsonRelationship);
Relationship relationship = new Relationship(accessToken, request['data']);
return relationship;
}
/* Media Endpoints from http://instagram.com/developer/endpoints/media */
/*
* Get information about a media object. The returned type key will allow you to differentiate between image and video media.
* Note: if you authenticate with an OAuth Token, you will receive the user_has_liked key
* which quickly tells you whether the current user has liked this media item.
*/
Media getMediaById(int mediaId) {
Map<String, String> data = new Map<String, String>();
data['media_id'] = mediaId.toString();
String uri = UriConstructor.constructUri(UriFactory.MEDIA_GET_BY_ID, data);
data.clear();
data['access_token'] = accessToken;
String jsonUser = doGet(uri, data);
Map request = JSON.decode(jsonUser);
Media media = new Media(accessToken, request['data']);
return media;
}
/*
* This endpoint returns the same response as GET /media/media-id.
* A media object's shortcode can be found in its shortlink URL.
* An example shortlink is http://instagram.com/p/D/
* Its corresponding shortcode is D.
*/
Media getMediaByShortcode(String shortcode) {
Map<String, String> data = new Map<String, String>();
data['shortcode'] = shortcode.toString();
String uri = UriConstructor.constructUri(UriFactory.MEDIA_GET_BY_SHORCODE, data);
data.clear();
data['access_token'] = accessToken;
String jsonUser = doGet(uri, data);
Map request = JSON.decode(jsonUser);
Media media = new Media(accessToken, request['data']);
return media;
}
/* Search for media in a given area. The default time span is set to 5 days.
* The time span must not exceed 7 days.
* Defaults time stamps cover the last 5 days. Can return mix of image and video types.
*
*/
PaginatedCollection<Media> searchMedia(double lat, double lng, int minTimestamp, int maxTimestamp, int distance) {
String uri = UriFactory.MEDIA_SEARCH;
Map<String, String> params = new Map<String, String>();
params['lat'] = lat.toString();
params['lng'] = lng.toString();
params['min_timestamp'] = minTimestamp.toString();
params['max_timestamp'] = maxTimestamp.toString();
params['distance'] = distance.toString();
params['access_token'] = accessToken;
List<Media> medias = new List<Media>();
PaginationIterator<Media> iterator =
new PaginationIterator<Media>(medias, UriConstructor.uriFromMap(uri, params), ModelType.MEDIA);
return new PaginatedCollection<Media>(medias, iterator);
}
/* Get a list of what media is most popular at the moment. Can return mix of image and video types. */
PaginatedCollection<Media> getPopular() {
String uri = UriFactory.MEDIA_GET_POPULAR;
List<Media> medias = new List<Media>();
PaginationIterator<Media> iterator =
new PaginationIterator<Media>(medias, uri+'?access_token='+accessToken, ModelType.MEDIA);
return new PaginatedCollection<Media>(medias, iterator);
}
/* Comment Endpoints from http://instagram.com/developer/endpoints/comments */
/* Get a full list of comments on a media object. */
PaginatedCollection<Comment> getCommentsByMediaId(int mediaId) {
Map<String, String> data = new Map<String, String>();
data['media_id'] = mediaId.toString();
String uri = UriConstructor.constructUri(UriFactory.GET_MEDIA_COMMENTS, data);
data.clear();
List<Comment> comments = new List<Comment>();
PaginationIterator<Comment> iterator =
new PaginationIterator<Comment>(comments, uri+'?access_token='+accessToken, ModelType.COMMENT);
return new PaginatedCollection<Comment>(comments, iterator);
}
}
// 193886659.1fb234f.f25f2aa534834fd48f6b4965ab424b7c