-
Notifications
You must be signed in to change notification settings - Fork 0
/
flickr_connector.html
238 lines (215 loc) · 7.31 KB
/
flickr_connector.html
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
<html>
<meta http-equiv="Cache-Control" content="no-store" />
<head>
<title>Flickr Connector - Hackathon</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script src="https://public.tableau.com/javascripts/api/tableauwdc-1.1.0.js" type="text/javascript"></script>
<script src="config.js" type="text/javascript"></script>
<script type="text/javascript">
(function() {
//
// Helper functions
//
function getNewEntry() {
var entry = { 'PhotoId': ""
,'Title': ""
,'Views': 0
,'Favorites': 0
,'UserId': ""
};
return entry;
};
var flickrUrlBase = "https://api.flickr.com/services/rest/?method=";
var flickrUrlKey = "&api_key="+global["FLICKR_KEY"];
var flickrUrlLast = "&format=json&nojsoncallback=1";
function buildUrlImages(user, numimages, page) {
var url = flickrUrlBase + "flickr.people.getPublicPhotos"+flickrUrlKey+"&user_id="+user+"&extras=views"+"&per_page="+numimages+"&page="+page+flickrUrlLast;
return url;
}
function buildUrlGroupImages(group, numimages, page) {
var url = flickrUrlBase + "flickr.groups.pools.getPhotos"+flickrUrlKey+"&group_id="+group+"&extras=views"+"&per_page="+numimages+"&page="+page+flickrUrlLast;
return url;
}
function buildUrlFindUser(username) {
var url = flickrUrlBase + "flickr.people.findByUsername"+flickrUrlKey+"&username="+username+flickrUrlLast;
return url;
}
function buildUrlGetFavorites(image) {
var url = flickrUrlBase + "flickr.photos.getFavorites"+flickrUrlKey+"&photo_id="+image+flickrUrlLast;
return url;
}
function findUser(username) {
var connectionUrl = buildUrlFindUser(username);
var user;
var xhr = $.ajax({
url: connectionUrl,
dataType: 'json',
async: false,
success: function (data) {
if (data.user) {
user = data.user.id;
} else {
tableau.abortWithError("No user found for username: " + username);
}
},
error: function (xhr, ajaxOptions, thrownError) {
// add something to the log and return an empty set if there was problem with the connection
tableau.log("connection error: " + xhr.responseText + "\n" + thrownError);
tableau.abortWithError("error connecting to flickr");
}
});
return user;
};
function getImages(user, groupId, numimages) {
var imageList = [];
var pageNum = numimages / 100 + 1;
var imageCounter = 1;
var p;
for(p = 0; p < pageNum; ++p) {
if (user != "") {
var connectionUrl = buildUrlImages(user, 100, p+1);
} else {
var connectionUrl = buildUrlGroupImages(groupId, 100, p+1);
}
// grab images for the user
var xhr = $.ajax({
url: connectionUrl,
dataType: 'json',
async: false,
success: function (data) {
if (data.photos) {
var quotes = data.photos.photo;
var jj;
// mash the data into an array of objects
for (jj = 0; jj < quotes.length; ++jj) {
var photo = quotes[jj];
var entry = getNewEntry();
entry.PhotoId = photo.id;
entry.Title = photo.title;
entry.Views = photo.views;
entry.UserId = photo.owner
imageList.push(entry);
imageCounter++;
if (imageCounter >= numimages) {
// that is enough
return;
}
}
} else {
//tableau.abortWithError("No results found for user: " + username);
}
},
error: function (xhr, ajaxOptions, thrownError) {
// add something to the log and return an empty set if there was problem with the connection
tableau.log("connection error: " + xhr.responseText + "\n" + thrownError);
tableau.abortWithError("error connecting to flickr");
}
});
}
return imageList;
};
function getFavorites(photoId) {
var connectionUrl = buildUrlGetFavorites(photoId);
var numFavorites = 0;
var xhr = $.ajax({
url: connectionUrl,
dataType: 'json',
async: false,
success: function (data) {
if (data.photo) {
numFavorites = data.photo.total;
} else {
tableau.log("image data connection error for image " + imageList[i].Image + "\n");
}
},
error: function (xhr, ajaxOptions, thrownError) {
// add something to the log
tableau.log("image data connection error: " + xhr.responseText + "\n" + thrownError);
}
});
return numFavorites;
};
//
// Connector definition
//
var myConnector = tableau.makeConnector();
myConnector.getColumnHeaders = function() {
// Tell tableau about the fields and their types
var fieldNames = ['PhotoId', 'Title', 'Views', 'Favorites', 'UserId'];
var fieldTypes = ['string', 'string', 'int', 'int', 'string'];
tableau.headersCallback(fieldNames, fieldTypes); // tell tableau about the fields and their type
};
myConnector.getTableData = function(lastRecordToken) {
// Call back to tableau with the table data
var connectionData = JSON.parse(tableau.connectionData)
var username = connectionData.username;
var groupId = connectionData.groupId;
var numimages = connectionData.numpic;
if (username) {
var user = findUser(username);
var imageList = getImages(user, "", numimages);
} else {
var imageList = getImages("", groupId, numimages);
}
// grab details from the images
if (connectionData.favorites) {
var ii;
for (ii = 0; ii < imageList.length; ++ii) {
imageList[ii].Favorites = getFavorites(imageList[ii].PhotoId);
}
}
// Call back to tableau with the table data and the new record number (this is stored as a string)
tableau.dataCallback(imageList, imageList.length.toString(), false);
};
tableau.registerConnector(myConnector);
//
// Setup connector UI
//
$(document).ready(function() {
$("#submitButton").click(function() { // This event fires when a button is clicked
var username = $('input[name=username]').val().trim();
var groupId = $('input[name=groupId]').val().trim();
var numpic = $('input[name=numpic]').val().trim();
if (numpic < 0) {
numpic = 1;
}
var favorites = $('#favorites').val() == 1;
if (username || groupId) {
tableau.connectionData = JSON.stringify({"username": username, "groupId": groupId, "numpic": numpic, "favorites": favorites});
tableau.connectionName = 'Flickr info: ' + username;
tableau.submit();
}
});
});
})();
</script>
</head>
<body>
<table>
<tr>
<td>Enter a username:</td>
<td><input type="text" name="username" style="width: 150px;" value="melekzek"/></td>
</tr>
<tr>
<td>Enter a groupId:</td>
<td><input type="text" name="groupId" style="width: 150px;" value=""/></td>
</tr>
<tr>
<td>Number of pictures:</td>
<td><input type="text" name="numpic" style="width: 150px;" value="50"/><td>
</tr>
<tr>
<td>
<select id="favorites">
<option value="0">Skip Favorites</option>
<option value="1">Count Favorites</option>
</select>
</td>
<td>
</td>
</tr>
</table>
<br>
<button type="button" id="submitButton">Get Image Data</button>
</body
</html>