-
Notifications
You must be signed in to change notification settings - Fork 24
/
instagram.js
388 lines (304 loc) · 12.7 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
(function() {
'use strict';
// This config stores the important strings needed to
// connect to the foursquare API and OAuth service
//
// Storing these here is insecure for a public app
// See part II. of this tutorial for an example of how
// to do a server-side OAuth flow and avoid this problem
var config = {
clientId: '021d6ed7e5604c33924542e62a3d0a2e',
redirectUri: 'https://illonage.github.io/',
authUrl: 'https://api.instagram.com',
max_iteration: 20,
};
// Called when web page first loads and when
// the OAuth flow returns to the page
//
// This function parses the access token in the URI if available
// It also adds a link to the foursquare connect button
$(document).ready(function() {
//script to get the access Token
if((window.location.href).indexOf('#') != -1) {
var queryString = (window.location.href).substr((window.location.href).indexOf('?') + 1);
var value = (queryString.split('='))[1];
var accessToken = decodeURIComponent(value);
}
var hasAuth = accessToken && accessToken.length > 0;
updateUIWithAuthState(hasAuth);
$("#connectbutton").click(function() {
doAuthRedirect();
});
$("#submitButton").click(function() {
if (document.getElementById('gg') !== null && document.getElementById('gg') !== '') {
var type = document.getElementById('gg').textContent;
var variable = $('#ticker').val().trim();
}
else{ var type = "empty";
var variable = "empty";}
//var type = document.getElementById('gg').textContent;
tableau.connectionData = JSON.stringify({'type':type,'variable': variable});
tableau.connectionName = "Results for " + type +" "+ variable;
tableau.submit();
});
});
// An on-click funcion for the connect to foursquare button,
// This will redirect the user to a foursquare login
function doAuthRedirect() {
var appId = config.clientId;
if (tableau.authPurpose === tableau.authPurposeEnum.ephemerel) {
appId = config.clientId;
} else if (tableau.authPurpose === tableau.authPurposeEnum.enduring) {
appId = config.clientId; // This should be the Tableau Server appID
}
var url = config.authUrl + '/oauth/authorize/?client_id=' + appId +
'&redirect_uri=' + config.redirectUri +'&response_type=token&scope=basic';
window.location.href = url;
}
//------------- OAuth Helpers -------------//
// This helper function returns the URI for the venueLikes endpoint
// It appends the passed in accessToek to the call to personalize the call for the user
function getHashtag(accessToken, tickerSymbol) {
return "https://api.instagram.com/v1/tags/"+ tickerSymbol +"/media/recent?count=5&access_token=" +
accessToken;
}
function getUser(accessToken, tickerSymbol) {
return "https://api.instagram.com/v1/users/search?q="+ tickerSymbol +"&access_token=" +
accessToken;
}
function updateUIWithAuthState(hasAuth) {
if (hasAuth) {
$(".notsignedin").css('display', 'none');
$(".signedin").css('display', 'block');
} else {
$(".notsignedin").css('display', 'block');
$(".signedin").css('display', 'none');
}
}
//------------- Tableau WDC code -------------//
// Create tableau connector, should be called first
var myConnector = tableau.makeConnector();
// Init function for connector, called during every phase but
// only called when running inside the simulator or tableau
myConnector.init = function(initCallback) {
tableau.authType = tableau.authTypeEnum.custom;
// If we are in the auth phase we only want to show the UI needed for auth
if (tableau.phase == tableau.phaseEnum.authPhase) {
$("#getvenuesbutton").css('display', 'none');
}
if (tableau.phase == tableau.phaseEnum.gatherDataPhase) {
// If API that WDC is using has an enpoint that checks
// the validity of an access token, that could be used here.
// Then the WDC can call tableau.abortForAuth if that access token
// is invalid.
}
if((window.location.href).indexOf('#') != -1) {
var queryString = (window.location.href).substr((window.location.href).indexOf('?') + 1);
var value = (queryString.split('='))[1];
var accessToken = decodeURIComponent(value);
}
var hasAuth = (accessToken && accessToken.length > 0) || tableau.password.length > 0;
updateUIWithAuthState(hasAuth);
initCallback();
// If we are not in the data gathering phase, we want to store the token
// This allows us to access the token in the data gathering phase
if (tableau.phase == tableau.phaseEnum.interactivePhase || tableau.phase == tableau.phaseEnum.authPhase) {
if (hasAuth) {
tableau.password = accessToken;
if (tableau.phase == tableau.phaseEnum.authPhase) {
// Auto-submit here if we are in the auth phase
tableau.submit()
}
return;
}
}
};
myConnector.getSchema = function(schemaCallback) {
var cols = [
{ id : "username", alias : "username", dataType : tableau.dataTypeEnum.string},
{ id : "filter", alias : "filter", dataType : tableau.dataTypeEnum.string },
{ id : "likes", alias : "Number of likes", dataType : tableau.dataTypeEnum.float },
{ id : "tags", alias : "tags", dataType : tableau.dataTypeEnum.string },
{ id : "created_time", alias : "Created Time", dataType : tableau.dataTypeEnum.datetime },
{ id : "link", alias : "Link", dataType : tableau.dataTypeEnum.string },
{ id : "location", alias : "location", dataType : tableau.dataTypeEnum.string },
{ id : "lat", alias : "latitude", dataType : tableau.dataTypeEnum.float },
{ id : "lon", alias : "longitude", dataType : tableau.dataTypeEnum.float },
{ id : "nb_comments", alias : "number of Comments", dataType : tableau.dataTypeEnum.float },
{ id : "text", alias : "Text", dataType : tableau.dataTypeEnum.string },
{ id : "image_url", alias : "Image URL", dataType : tableau.dataTypeEnum.string },
];
var tableInfo = {
id : "instagramFeed",
alias : "Instagram Feed",
columns : cols
};
schemaCallback([tableInfo]);
};
function getHistory(table, doneCallback,connectionUri,count ) {
var dataToReturn = [];
var hasMoreData = false;
var new_url = connectionUri;
if (count == 0) {
var iteration = count;
}
var getPage= function(connectionUri){
var xhr = $.ajax({
url: connectionUri,
type: "GET",
crossDomain: true,
dataType: 'jsonp',
success: function (data) {
var feat = data.data;
var tableData = [];
for (var i = 0; i < feat.length; i++) {
var date = new Date(parseInt(feat[i].created_time) * 1000);
// Hours part from the timestamp
//var hours = date.getHours();
// Minutes part from the timestamp
//var minutes = date.getMinutes();
// Seconds part from the timestamp
//var seconds = date.getSeconds();
//var dateFinal = (date.getMonth()+1) +"/"+date.getDate()+"/"+ date.getFullYear()+" "+hours+":"+"0" +minutes+":"+"0" + date.getSeconds();
//var d = new Date (dateFinal);
if (feat[i].caption ) {
var text = feat[i].caption.text.toString();
}
else var text = " ";
if (feat[i].location && feat[i].location !== "null" && feat[i].location !== "undefined") {
var location = feat[i].location["name"];
var lon = feat[i].location.longitude;
var lat = feat[i].location.latitude;
}
else{
var location = "";
var lon = "";
var lat = "";
}
tableData.push({
"username": feat[i].user.username,
"filter": feat[i].filter,
"likes": feat[i].likes.count,
"tags": feat[i].tags.toString(),
"created_time": date,
"link": feat[i].link,
"nb_comments": feat[i].comments.count,
"location": location,
"lon": lon,
"lat": lat,
"text": text,
"image_url": feat[i].images.low_resolution.url,
});
}
connectionUri = data.pagination.next_url;
table.appendRows(tableData);
if (connectionUri && iteration < 20) {
iteration++;
getPage(connectionUri);
}
else{
doneCallback();
}
},
});
}
getPage(new_url)
}
// function getUserID(username,accessToken,cb){
// var xhr = $.ajax({
// url: getUser(accessToken, username) ,
// type: "GET",
// crossDomain: true,
// dataType: 'jsonp',
// success: function (data) {
// var user_id = data.data[0].id;
// cb(user_id);
// }
// })
// }
function getHistoryWithUserID(table, doneCallback,connectionUri,accessToken,count){
var dataToReturn = [];
var hasMoreData = false;
var new_url = "https://api.instagram.com/v1/users/self/media/recent/?access_token="+accessToken;
if (count == 0) {
var iteration = count;
}
var getPage = function(url){
var xhr = $.ajax({
url: url,
type: "GET",
crossDomain: true,
dataType: 'jsonp',
success: function (data2) {
var feat = data2.data;
var tableData = [];
for (var i = 0; i < feat.length; i++) {
var date = new Date(parseInt(feat[i].created_time) * 1000);
if (feat[i].caption ) {
var text = feat[i].caption.text.toString();
}
else var text = " ";
if (feat[i].location && feat[i].location !== "null" && feat[i].location !== "undefined") {
var location = feat[i].location["name"];
var lon = feat[i].location.longitude;
var lat = feat[i].location.latitude;
}
else{
var location = "";
var lon = "";
var lat = "";
}
tableData.push({
"username": feat[i].user.username,
"filter": feat[i].filter,
"likes": feat[i].likes.count,
"tags": feat[i].tags.toString(),
"created_time": date,
"link": feat[i].link,
"nb_comments": feat[i].comments.count,
"location": location,
"lon": lon,
"lat": lat,
"text": text,
"image_url": feat[i].images.low_resolution.url,
});
}
connectionUri = data2.pagination.next_url;
table.appendRows(tableData);
if (connectionUri && iteration < 20) {
iteration++;
getPage(connectionUri,count);
}
else{
doneCallback();
}
},
});
}
getPage(new_url)
}
function getHistory3(username, table, doneCallback,connectionUri,accessToken,count){
getHistoryWithUserID(table, doneCallback,connectionUri,accessToken,count)
}
// This function acutally make the foursquare API call and
// parses the results and passes them back to Tableau
myConnector.getData = function(table, doneCallback) {
var lastId = parseInt(table.incrementValue || -1);
var accessToken = tableau.password;
//var variable = JSON.parse(tableau.connectionData).variable;
var type = JSON.parse(tableau.connectionData).type;
if (type == "Hashtag"){
var variable = JSON.parse(tableau.connectionData).variable;
var connectionUri = getHashtag(accessToken,variable);
var count = 0;
getHistory(table, doneCallback,connectionUri, count);
}
else{
var connectionUri = getUser(accessToken,variable);
var count = 0;
getHistoryWithUserID(table, doneCallback,connectionUri,accessToken,count);
}
};
// Register the tableau connector, call this last
tableau.registerConnector(myConnector);
})();