File: photos-geo-api.c

package info (click to toggle)
flickcurl 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,508 kB
  • ctags: 3,340
  • sloc: ansic: 26,020; sh: 11,915; xml: 1,025; makefile: 343
file content (649 lines) | stat: -rw-r--r-- 16,424 bytes parent folder | download | duplicates (5)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/* -*- Mode: c; c-basic-offset: 2 -*-
 *
 * photos-geo-api.c - Flickr flickr.photos.geo.* API calls
 *
 * Copyright (C) 2007-2012, David Beckett http://www.dajobe.org/
 * 
 * This file is licensed under the following three licenses as alternatives:
 *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
 *   2. GNU General Public License (GPL) V2 or any newer version
 *   3. Apache License, V2.0 or any newer version
 * 
 * You may not use this file except in compliance with at least one of
 * the above three licenses.
 * 
 * See LICENSE.html or LICENSE.txt at the top of this package for the
 * complete terms and further detail along with the license texts for
 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
 * 
 */

#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef WIN32
#include <win32_flickcurl_config.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#undef HAVE_STDLIB_H
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <flickcurl.h>
#include <flickcurl_internal.h>


/**
 * flickcurl_photos_geo_batchCorrectLocation:
 * @fc: flickcurl context
 * @location: The location (lat, long, accuracy) of the photos to update.
 * @place_id: A Flickr Places ID (or NULL)
 * @woe_id: A Where On Earth (WOE) ID (or 0)
 *
 * Correct the places hierarchy for all the photos for a user at a
 * given location (latitude, longitude and accuracy).
 *
 * You must pass either a valid Places ID in @place_id or a WOE ID in
 * @woe_id.
 * 
 * Batch corrections are processed in a delayed queue so it may take
 * a few minutes before the changes are reflected in a user's photos.
 *
 * Implements flickr.photos.geo.batchCorrectLocation (1.8)
 * 
 * Return value: non-0 on failure
 **/
int
flickcurl_photos_geo_batchCorrectLocation(flickcurl* fc,
                                          flickcurl_location* location,
                                          const char* place_id, int woe_id)
{
  xmlDocPtr doc = NULL;
  char latitude_s[50];
  char longitude_s[50];
  char accuracy_s[50];
  char woe_id_str[10];
  int result = 0;

  flickcurl_init_params(fc, 1);

  if(!place_id || !woe_id)
    return 1;

  if(location->latitude < -90.0)
    location->latitude= -90.0;
  if(location->latitude > 90.0)
    location->latitude= 90.0;
  if(location->longitude < -180.0)
    location->longitude= -180.0;
  if(location->longitude > 180.0)
    location->longitude= 180.0;
  if(location->accuracy < 1 || location->accuracy > 16)
    location->accuracy = 0;
  

  sprintf(latitude_s, "%f", location->latitude);
  flickcurl_add_param(fc, "lat", latitude_s);
  sprintf(longitude_s, "%f", location->longitude);
  flickcurl_add_param(fc, "lon", longitude_s);
  sprintf(accuracy_s, "%d", location->accuracy);
  flickcurl_add_param(fc, "accuracy", accuracy_s);
  if(place_id) {
    flickcurl_add_param(fc, "place_id", place_id);
  }
  if(woe_id > 0) {
    sprintf(woe_id_str, "%d", woe_id);
    flickcurl_add_param(fc, "woe_id", woe_id_str);
  }
  flickcurl_end_params(fc);

  if(flickcurl_prepare(fc, "flickr.photos.geo.batchCorrectLocation"))
    goto tidy;

  doc = flickcurl_invoke(fc);
  if(!doc)
    goto tidy;

  result = 0;

  tidy:
  if(fc->failed)
    result = 1;

  return result;
}


/**
 * flickcurl_photos_geo_correctLocation:
 * @fc: flickcurl context
 * @photo_id: The ID of the photo whose WOE location is being corrected.
 * @place_id: A Flickr Places ID (or NULL)
 * @woe_id: A Where On Earth (WOE) ID (or NULL)
 * 
 * Correct a photo location.
 *
 * You must pass either a valid Places ID in @place_id or a WOE ID in @woe_id.
 * 
 * Implements flickr.photos.geo.correctLocation (1.8)
 * 
 * Return value: non-0 on failure
 **/
int
flickcurl_photos_geo_correctLocation(flickcurl* fc, const char* photo_id,
                                     const char* place_id, int woe_id)
{
  xmlDocPtr doc = NULL;
  xmlXPathContextPtr xpathCtx = NULL; 
  void* result = NULL;
  char woe_id_str[10];
  
  flickcurl_init_params(fc, 0);

  if(!photo_id)
    return 1;

  flickcurl_add_param(fc, "photo_id", photo_id);
  flickcurl_add_param(fc, "place_id", place_id);
  if(woe_id > 0) {
    sprintf(woe_id_str, "%d", woe_id);
    flickcurl_add_param(fc, "woe_id", woe_id_str);
  }
  flickcurl_end_params(fc);

  if(flickcurl_prepare(fc, "flickr.photos.geo.correctLocation"))
    goto tidy;

  doc = flickcurl_invoke(fc);
  if(!doc)
    goto tidy;


  xpathCtx = xmlXPathNewContext(doc);
  if(!xpathCtx) {
    flickcurl_error(fc, "Failed to create XPath context for document");
    fc->failed = 1;
    goto tidy;
  }

  result = NULL; /* your code here */

  tidy:
  if(xpathCtx)
    xmlXPathFreeContext(xpathCtx);

  if(fc->failed)
    result = NULL;

  return (result == NULL);
}


/**
 * flickcurl_photos_geo_getLocation:
 * @fc: flickcurl context
 * @photo_id: The id of the photo you want to retrieve location data for.
 * 
 * Get the geo data (latitude and longitude and the accuracy level) for a photo.
 *
 * Implements flickr.photos.geo.getLocation (0.12)
 * 
 * Return value: non-0 on failure
 **/
flickcurl_location*
flickcurl_photos_geo_getLocation(flickcurl* fc, const char* photo_id)
{
  xmlDocPtr doc = NULL;
  xmlXPathContextPtr xpathCtx = NULL; 
  flickcurl_location* location = NULL;
  
  flickcurl_init_params(fc, 0);

  if(!photo_id)
    return NULL;

  flickcurl_add_param(fc, "photo_id", photo_id);

  flickcurl_end_params(fc);

  if(flickcurl_prepare(fc, "flickr.photos.geo.getLocation"))
    goto tidy;

  doc = flickcurl_invoke(fc);
  if(!doc)
    goto tidy;


  xpathCtx = xmlXPathNewContext(doc);
  if(!xpathCtx) {
    flickcurl_error(fc, "Failed to create XPath context for document");
    fc->failed = 1;
    goto tidy;
  }

  location = flickcurl_build_location(fc, xpathCtx,
                                    (const xmlChar*)"/rsp/photo/location");

  tidy:
  if(xpathCtx)
    xmlXPathFreeContext(xpathCtx);

  if(fc->failed) {
    if(location)
      flickcurl_free_location(location);
    location = NULL;
  }

  return location;
}


/**
 * flickcurl_photos_geo_getPerms:
 * @fc: flickcurl context
 * @photo_id: The id of the photo to get permissions for.
 * 
 * Get permissions for who may view geo data for a photo.
 *
 * Implements flickr.photos.geo.getPerms (0.12)
 * 
 * Return value: non-0 on failure
 **/
flickcurl_perms*
flickcurl_photos_geo_getPerms(flickcurl* fc, const char* photo_id)
{
  xmlDocPtr doc = NULL;
  xmlXPathContextPtr xpathCtx = NULL; 
  flickcurl_perms* perms = NULL;
  
  flickcurl_init_params(fc, 0);

  if(!photo_id)
    return NULL;

  flickcurl_add_param(fc, "photo_id", photo_id);

  flickcurl_end_params(fc);

  if(flickcurl_prepare(fc, "flickr.photos.geo.getPerms"))
    goto tidy;

  doc = flickcurl_invoke(fc);
  if(!doc)
    goto tidy;


  xpathCtx = xmlXPathNewContext(doc);
  if(!xpathCtx) {
    flickcurl_error(fc, "Failed to create XPath context for document");
    fc->failed = 1;
    goto tidy;
  }

  perms = flickcurl_build_perms(fc, xpathCtx,
                              (const xmlChar*)"/rsp/perms");

  tidy:
  if(xpathCtx)
    xmlXPathFreeContext(xpathCtx);

  if(fc->failed) {
    if(perms)
      flickcurl_free_perms(perms);
    perms = NULL;
  }

  return perms;
}


/**
 * flickcurl_photos_geo_photosForLocation_params:
 * @fc: flickcurl context
 * @location: The location (lat, long, accuracy) of the photos
 * @list_params: #flickcurl_photos_list_params result parameters (or NULL)
 * 
 * Get a list of photos for a user at a specific location (latitude, longitude
 * and accuracy)
 *
 * Return value: list of photos or NULL on failure
 **/
flickcurl_photos_list*
flickcurl_photos_geo_photosForLocation_params(flickcurl* fc,
                                              flickcurl_location* location,
                                              flickcurl_photos_list_params* list_params)
{
  flickcurl_photos_list* photos_list = NULL;
  char latitude_s[50];
  char longitude_s[50];
  char accuracy_s[50];
  const char* format = NULL;

  flickcurl_init_params(fc, 0);

  if(!location)
    return NULL;
  
  if(location->latitude < -90.0)
    location->latitude= -90.0;
  if(location->latitude > 90.0)
    location->latitude= 90.0;
  if(location->longitude < -180.0)
    location->longitude= -180.0;
  if(location->longitude > 180.0)
    location->longitude= 180.0;
  if(location->accuracy < 1 || location->accuracy > 16)
    location->accuracy = 0;
  

  sprintf(latitude_s, "%f", location->latitude);
  flickcurl_add_param(fc, "lat", latitude_s);
  sprintf(longitude_s, "%f", location->longitude);
  flickcurl_add_param(fc, "lon", longitude_s);
  sprintf(accuracy_s, "%d", location->accuracy);
  flickcurl_add_param(fc, "accuracy", accuracy_s);

  /* Photos List parameters */
  flickcurl_append_photos_list_params(fc, list_params, &format);

  flickcurl_end_params(fc);

  if(flickcurl_prepare(fc, "flickr.photos.geo.photosForLocation"))
    goto tidy;

  photos_list = flickcurl_invoke_photos_list(fc,
                                             (const xmlChar*)"/rsp/photos",
                                             format);

  tidy:
  if(fc->failed) {
    if(photos_list)
      flickcurl_free_photos_list(photos_list);
    photos_list = NULL;
  }

  return photos_list;
}


/**
 * flickcurl_photos_geo_photosForLocation:
 * @fc: flickcurl context
 * @location: The location (lat, long, accuracy) of the photos
 * @extras: A comma-delimited list of extra information to fetch for each returned record. Currently supported fields are: license, date_upload, date_taken, owner_name, icon_server, original_format, last_update, geo, tags, machine_tags, o_dims, views, media (or NULL)
 * @per_page: Number of photos to return per page. If this argument is omitted, it defaults to 100. The maximum allowed value is 500 (or NULL)
 * @page: The page of results to return. If this argument is omitted, it defaults to 1 (or NULL)
 * 
 * Get a list of photos for a user at a specific location (latitude,
 * longitude and accuracy)
 *
 * Implements flickr.photos.geo.photosForLocation (1.8)
 * 
 * Return value: list of photos or NULL on failure
 **/
flickcurl_photo**
flickcurl_photos_geo_photosForLocation(flickcurl* fc,
                                       flickcurl_location* location,
                                       const char* extras,
                                       int per_page, int page)
{
  flickcurl_photos_list_params list_params;
  flickcurl_photos_list* photos_list;
  flickcurl_photo** photos;

  memset(&list_params, '\0', sizeof(list_params));
  list_params.format   = NULL;
  list_params.extras   = extras;
  list_params.per_page = per_page;
  list_params.page     = page;

  photos_list = flickcurl_photos_geo_photosForLocation_params(fc, location,
                                                              &list_params);
  if(!photos_list)
    return NULL;

  photos = photos_list->photos; photos_list->photos = NULL;  
  /* photos array is now owned by this function */

  flickcurl_free_photos_list(photos_list);

  return photos;
}


/**
 * flickcurl_photos_geo_removeLocation:
 * @fc: flickcurl context
 * @photo_id: The id of the photo you want to remove location data from.
 * 
 * Removes the geo data associated with a photo.
 *
 * Implements flickr.photos.geo.removeLocation (0.12)
 * 
 * Return value: non-0 on failure
 **/
int
flickcurl_photos_geo_removeLocation(flickcurl* fc, const char* photo_id)
{
  xmlDocPtr doc = NULL;
  
  flickcurl_init_params(fc, 1);

  if(!photo_id)
    return 1;

  flickcurl_add_param(fc, "photo_id", photo_id);

  flickcurl_end_params(fc);

  if(flickcurl_prepare(fc, "flickr.photos.geo.removeLocation"))
    goto tidy;

  doc = flickcurl_invoke(fc);
  if(!doc)
    goto tidy;

  tidy:

  return fc->failed;
}


/**
 * flickcurl_photos_geo_setContext:
 * @fc: flickcurl context
 * @photo_id: The id of the photo to set context data for.
 * @context: Context is a numeric value representing the photo's geotagginess beyond latitude and longitude. The current values are: 0: not defined, 1: indoors, 2: outdoors.
 * 
 * Indicate the state of a photo's geotagginess beyond latitude and longitude.
 * 
 * Note : photos passed to this method must already be geotagged
 * using the flickcurl_photos_geo_setLocation() method.
 *
 * Implements flickr.photos.geo.setContext (1.8)
 * 
 * Return value: non-0 on failure
 **/
int
flickcurl_photos_geo_setContext(flickcurl* fc, const char* photo_id,
                                int context)
{
  xmlDocPtr doc = NULL;
  xmlXPathContextPtr xpathCtx = NULL; 
  char context_str[3];
  void* result = NULL;
  
  flickcurl_init_params(fc, 1);

  if(!photo_id || context < 0 || context > 2)
    return 1;

  flickcurl_add_param(fc, "photo_id", photo_id);
  sprintf(context_str, "%d", context);
  flickcurl_add_param(fc, "context", context_str);

  flickcurl_end_params(fc);

  if(flickcurl_prepare(fc, "flickr.photos.geo.setContext"))
    goto tidy;

  doc = flickcurl_invoke(fc);
  if(!doc)
    goto tidy;


  xpathCtx = xmlXPathNewContext(doc);
  if(!xpathCtx) {
    flickcurl_error(fc, "Failed to create XPath context for document");
    fc->failed = 1;
    goto tidy;
  }

  result = NULL; /* your code here */

  tidy:
  if(xpathCtx)
    xmlXPathFreeContext(xpathCtx);

  if(fc->failed)
    result = NULL;

  return (result == NULL);
}


/**
 * flickcurl_photos_geo_setLocation:
 * @fc: flickcurl context
 * @photo_id: The id of the photo to set location data for.
 * @location: The location
 * 
 * Sets the geo data (latitude and longitude and, optionally, the
 * accuracy level) for a photo.
 *
 * Implements flickr.photos.geo.setLocation (0.12)
 * 
 * Return value: non-0 on failure
 **/
int
flickcurl_photos_geo_setLocation(flickcurl* fc, const char* photo_id,
                                 flickcurl_location* location)
{
  xmlDocPtr doc = NULL;
  char latitude_s[50];
  char longitude_s[50];
  char accuracy_s[50];
  int result = 1;
  
  flickcurl_init_params(fc, 1);

  if(!photo_id)
    return 1;

  if(location->latitude < -90.0)
    location->latitude= -90.0;
  if(location->latitude > 90.0)
    location->latitude= 90.0;
  if(location->longitude < -180.0)
    location->longitude= -180.0;
  if(location->longitude > 180.0)
    location->longitude= 180.0;
  if(location->accuracy < 1 || location->accuracy > 16)
    location->accuracy = 0;
  

  flickcurl_add_param(fc, "photo_id", photo_id);
  sprintf(latitude_s, "%f", location->latitude);
  flickcurl_add_param(fc, "lat", latitude_s);
  sprintf(longitude_s, "%f", location->longitude);
  flickcurl_add_param(fc, "lon", longitude_s);
  if(location->accuracy >= 1) {
    sprintf(accuracy_s, "%d", location->accuracy);
    flickcurl_add_param(fc, "accuracy", accuracy_s);
  }

  flickcurl_end_params(fc);

  if(flickcurl_prepare(fc, "flickr.photos.geo.setLocation"))
    goto tidy;

  doc = flickcurl_invoke(fc);
  if(!doc)
    goto tidy;

  result = 0;

  tidy:
  if(fc->failed)
    result = 1;

  return result;
}


/**
 * flickcurl_photos_geo_setPerms:
 * @fc: flickcurl context
 * @photo_id: The id of the photo to get permissions for.
 * @perms: Geo permissions
 * 
 * Set the permission for who may view the geo data associated with a photo.
 *
 * Implements flickr.photos.geo.setPerms (0.12)
 * 
 * Return value: non-0 on failure
 **/
int
flickcurl_photos_geo_setPerms(flickcurl* fc, const char* photo_id,
                              flickcurl_perms* perms)
{
  xmlDocPtr doc = NULL;
  char is_public_str[2];
  char is_contact_str[2];
  char is_friend_str[2];
  char is_family_str[2];
  int result = 1;
  
  flickcurl_init_params(fc, 1);

  if(!photo_id || !perms)
    return 1;

  sprintf(is_public_str, "%d", (perms->is_public ? 1 : 0));
  flickcurl_add_param(fc, "is_public", is_public_str);
  sprintf(is_contact_str, "%d", (perms->is_contact ? 1 : 0));
  flickcurl_add_param(fc, "is_contact", is_contact_str);
  sprintf(is_friend_str, "%d", (perms->is_friend ? 1 : 0));
  flickcurl_add_param(fc, "is_friend", is_friend_str);
  sprintf(is_family_str, "%d", (perms->is_family ? 1 : 0));
  flickcurl_add_param(fc, "is_family", is_family_str);
  flickcurl_add_param(fc, "photo_id", photo_id);

  flickcurl_end_params(fc);

  if(flickcurl_prepare(fc, "flickr.photos.geo.setPerms"))
    goto tidy;

  doc = flickcurl_invoke(fc);
  if(!doc)
    goto tidy;

  result = 0;

  tidy:
  if(fc->failed)
    result = 1;

  return result;
}