-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlickrAPI.groovy
340 lines (315 loc) · 12.6 KB
/
GlickrAPI.groovy
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
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
class GlickrAPI {
def nsid = ""
private String secret = "<your secret>"
private String akey = "<your api key>"
def authToken = ""
private String frob = ""
private String restString = "http://flickr.com/services/rest/?method="
private XmlParser xParser = new XmlParser()
def authNewUser() {
getFrob()
createAuthURL()
getToken()
}
def constructUrl(String sig,String flickrMethod, m =[:] ){
String s = this.restString+flickrMethod+"&api_key="+this.akey+m.collect{k,v-> k+v}.join()
if(sig){s+="&auth_token=${this.authToken}&api_sig=${sig}"}
return s
}
def mapFlickrUser(){
}
//if bool is true then this is a Authenticated call
private getSig(String method,boolean authenticated = false){
String sigString="${this.secret}api_key${this.akey}"
try {
if(authenticated){
sigString +="auth_token${this.authToken}${method}"
}
else{
sigString+=method
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace()
}
return calcMD5(sigString)
}
/**
* parameters map will take the following values
* title (optional)
* The title of the photo.
* description (optional)
* A description of the photo. May contain some limited HTML.
* tags (optional)
* A space-seperated list of tags to apply to the photo.
* is_public, is_friend, is_family (optional)
* Set to 0 for no, 1 for yes. Specifies who can view the photo.
* safety_level (optional)
* Set to 1 for Safe, 2 for Moderate, or 3 for Restricted.
* content_type (optional)
* Set to 1 for Photo, 2 for Screenshot, or 3 for Other.
* hidden (optional)
*/
def uploadPicToFlickr(String filePath, Map parameters=[:]){
println("parameters ${parameters.inspect()}")
def paramMessages=[]
def pLength=0
def sig=""
if(parameters==[:]){
sig = calcMD5("${this.secret}api_key${this.akey}auth_token${this.authToken}")
}else{
//sort them and add them to the signature
def pString=""
parameters.keySet().toList().sort().each{key->
//if the key ='tags' make it into space separated string
pString+= "${key}${parameters[key]}"
paramMessages.add("---------------------------6ec047855a60a\r\nContent-Disposition: form-data; name=\"${key}\"\r\n\r\n${parameters[key]}\r\n"
)
}
sig = calcMD5("${this.secret}api_key${this.akey}auth_token${this.authToken}${pString}")
paramMessages.each{msg->pLength += msg.length()}
println pLength
//pLength = paramMessages.sum{it.length()}
}
//String filePath="JuanGolf08.jpg"
File file = new File(filePath)
//def connection = url.openConnection()
HttpURLConnection conn = new URL("http://api.flickr.com/services/upload/").openConnection()
def fis = new FileInputStream(file)
byte[] imageData= new byte[(int)file.length()]
def offset = 0;
def numRead = 0;
while (offset < imageData.length && (numRead=fis.read(imageData, offset, imageData.length-offset)) >= 0) {
offset += numRead
}
// Ensure all the bytes have been read in
if (offset < imageData.length) {
throw new IOException("The file was not completely read: "+file.getName());
}
println "filesize -> $imageData.length"
// Close the input stream, all file contents are in the bytes variable
fis.close();
def pApiKey= "---------------------------6ec047855a60a\r\nContent-Disposition: form-data; name=\"api_key\"\r\n\r\n${this.akey}\r\n"
def pAuthToken ="---------------------------6ec047855a60a\r\nContent-Disposition: form-data; name=\"auth_token\"\r\n\r\n${this.authToken}\r\n"
def pSig = "---------------------------6ec047855a60a\r\nContent-Disposition: form-data; name=\"api_sig\"\r\n\r\n${sig}\r\n"
def header1 = "---------------------------6ec047855a60a\r\nContent-Disposition: form-data; name=\"photo\"; filename=\"${filePath}\"\r\nContent-Type: image/jpeg\r\n\r\n"
// the image is sent between the header1 and footer in the multipart message.
def footer = "\r\n---------------------------6ec047855a60a--\r\n"
println(pApiKey.length() + " "+pAuthToken.length() + " "+pSig.length() +" "+ header1.length() +" "+ footer.length() +" "+ imageData.length + " "+pLength)
def totalLength = pApiKey.length() + pAuthToken.length() + pSig.length() + header1.length() + footer.length() + imageData.length + pLength
println "length is ${totalLength}"
conn.setRequestMethod("POST")
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=-------------------------6ec047855a60a")
conn.setRequestProperty("Host", "api.flickr.com")
conn.setRequestProperty("Content-Length",totalLength.toString() )
conn.setDoOutput(true)
def os = conn.getOutputStream()
println pApiKey
os.write(pApiKey.getBytes())
println pAuthToken
os.write(pAuthToken.getBytes())
paramMessages.each{msg->os.write(msg.getBytes())}
println pSig
os.write(pSig.getBytes())
os.write(header1.getBytes())
os.write(imageData,0 ,imageData.length)
os.write(footer.getBytes())
os.flush()
os.close()
conn.connect()
conn.disconnect()
println conn.content.text
}
def getFrob() {
def sig = getSig("methodflickr.auth.getFrob")
def url = "http://flickr.com/services/rest/?method=flickr.auth.getFrob&api_key=${this.akey}&api_sig=${sig}"
def node = new XmlParser().parse(url)
this.frob= node['frob'].text().toString()
return this.frob
}
/**
* This function will calculate an MD5 sum of String Signature and return
* the String value of the Hash via strBuildup
*
* @param signature
* @throws NoSuchAlgorithmException
* @returns strBuildup
*/
private String calcMD5(String signature) throws NoSuchAlgorithmException {
String strBuildup = ""
MessageDigest md5 = MessageDigest.getInstance("MD5")
byte[] md5summe = md5.digest(signature.getBytes())
for (int k = 0; k < md5summe.length; k++) {
byte b = md5summe[k]
String temp = Integer.toHexString(b & 0xFF)
/*
* toHexString has the side effect of making stuff like 0x0F only
* one char F(when it should be '0F') so I check the length of
* string
*/
if (temp.length() < 2) {temp = "0" + temp}
strBuildup += temp.toUpperCase()
}
return strBuildup
}
/**
* get the authentication token
*
* @return groovy.util.Node
*/
def checkToken(){
this.xParser.parse(constructUrl(getSig("methodflickr.auth.checkToken",true),
'flickr.auth.checkToken'))
}
/**
* getToken can only be used after a frob has been approved in order to get an approved authToken
* @return groovy.util.Node
*/
def getToken() {
def sig = getSig("frob${this.frob}methodflickr.auth.getToken")
def node = this.xParser.parse(
"${this.restString}flickr.auth.getToken&api_key=${this.akey}&frob=${this.frob}&api_sig=${sig}"
)
//need to get the nsid and the authtoken
this.authToken = node['auth']['token'].text()
this.nsid = node['auth']['user'].'@nsid'.text()
return node
}
/**
* returns: the URL string
*/
def createAuthURL() {
return "http://flickr.com/services/auth/?api_key=${this.akey}&perms=delete&frob=${this.frob}&api_sig=" +
getSig("frob${this.frob}permsdelete")
}
/***************************************************************************
* AUTH
**************************************************************************/
/***************************************************************************
* ACTIVITIES
**************************************************************************/
//POLL ONLY ONCE AN HOUR!!!!!
/**
* @return groovy.util.Node
*/
public activityGetUserComments(){
this.xParser.parse(constructUrl(getSig("methodflickr.activity.userComments",true),
'flickr.activity.userComments'))
}
/* **************************************************************************
* BLOGS
************************************************************************* */
/* **************************************************************************
* CONTACTS
************************************************************************* */
/**
* @return groovy.util.Node
*/
public void contactsGetList(){
this.xParser.parse(constructUrl(getSig("methodflickr.contacts.getList",true),
'flickr.contacts.getList'))
}
/***************************************************************************
* FAVORITES
**************************************************************************/
/***************************************************************************
* GROUPS
**************************************************************************/
/***************************************************************************
* GROUP POOLS
**************************************************************************/
/***************************************************************************
* INTERESTINGNESS
**************************************************************************/
/***************************************************************************
* PHOTOS
*what if a return back an array of pictures?
**************************************************************************/
/**
* @return groovy.util.Node
*/
def photosGetNotInSet(){
this.xParser.parse(constructUrl(getSig("methodflickr.photos.getNotInSet",true),
'flickr.photos.getNotInSet'))
}
/**
* @return groovy.util.Node
*/
def photosGetContactsPhotos(){
this.xParser.parse(constructUrl(getSig("methodflickr.photos.getContactsPhotos",true),
'flickr.photos.getContactsPhotos'))
}
/**
* @return groovy.util.Node
*/
def photosGetRecentlyUploaded() {
this.xParser.parse(constructUrl(getSig("methodflickr.photos.getRecent",true),
'flickr.photos.getRecent'))
}
/**
* @return groovy.util.Node
*/
def photosGetInfo(){
}
/* **************************************************************************
* PEOPLE
************************************************************************* */
/**
* @return groovy.util.Node
*/
def contactsGetList(){
this.xParser.parse(constructUrl(getSig("methodflickr.contacts.getList",true),
'flickr.contacts.getList',))
}
/**
* @return groovy.util.Node
*/
def peopleGetUploadStatus(){
this.xParser.parse(constructUrl(getSig("methodflickr.people.getUploadStatus",true),
'flickr.people.getUploadStatus'))
}
/**
* @return groovy.util.Node
*/
def peopleGetPublicPhotos() {
this.xParser.parse(constructUrl(getSig("methodflickr.people.getPublicPhotosuser_id${this.nsid}",true),
'flickr.people.getPublicPhotos',['&user_id=':"${this.nsid}"]))
}
/**
* @return groovy.util.Node
*/
def peopleGetPeopleInfo() {
this.xParser.parse(constructUrl(getSig("methodflickr.people.getInfouser_id${this.nsid}",true),
'flickr.people.getInfo',['&user_id=':"${this.nsid}"]))
}
/********************************************
* photosets
************************/
/**
* @return groovy.util.Node
*/
def photosetsGetList(String uid){
this.xParser.parse(constructUrl(getSig("methodflickr.photosets.getListuser_id${uid}",true),
'flickr.photosets.getList',['&user_id=':"${uid}"]))
}
/**
* @return groovy.util.Node
*/
def photosetsGetPhotos(String setID){
this.xParser.parse(constructUrl(getSig("methodflickr.photosets.getPhotosphotoset_id${setID}",true),
'flickr.photosets.getPhotos',['&photoset_id=':"${setID}"]))
}
/**
Upload photos
http://api.flickr.com/services/upload/
*/
//get the picture when supplied the proper args
//http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}_[mstb].jpg
def getPicture(String farmId, String serverId, String photoid, String secret,String size ="s"){
return "http://farm${farmId}.static.flickr.com/${serverId}/${photoid}_${secret}_${size}.jpg"
}
def getPicture(Map map,String size){
return "http://farm${map['farm']}.static.flickr.com/${map['server']}/${map['id']}_${map['secret']}_${size}.jpg"
}
}