Photos search example

Example 1. search-photos.c: Search for my interesting photos with a given tag and print the resulting photo IDs

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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <flickcurl.h>

static const char* program;

static const char*
my_basename(const char *name)
{
  char *p;
  if((p = strrchr(name, '/')))
    name = p+1;
  else if((p = strrchr(name, '\\')))
    name = p+1;

  return name;
}


static void
my_message_handler(void *user_data, const char *message)
{
  fprintf(stderr, "%s: ERROR: %s\n", program, message);
}


static const char* config_filename = ".flickcurl.conf";
static const char* config_section = "flickr";


int
main(int argc, char *argv[]) 
{
  flickcurl *fc = NULL;
  int rc = 0;
  const char* home;
  char config_path[1024];
  char* tag = NULL;
  flickcurl_photos_list_params list_params;
  flickcurl_search_params params;
  flickcurl_photos_list* photos_list = NULL;
  int i;
  
  program = my_basename(argv[0]);

  flickcurl_init();

  home = getenv("HOME");
  if(home)
    sprintf(config_path, "%s/%s", home, config_filename);
  else
    strcpy(config_path, config_filename);
  
  if(argc != 2) {
    fprintf(stderr, "%s: No tag given\n"
                    "Try `%s -h for more information.\n", program, program);
    rc = 1;
    goto tidy;
  }

  if(!strcmp(argv[1], "-h")) {
    printf("%s - search for my interesting photos about a tag\n"
           "Usage: %s TAG\n\n", program, program);
    
    fputs("Flickcurl home page: ", stdout);
    puts(flickcurl_home_url_string);
    puts(flickcurl_copyright_string);
    fputs("License: ", stdout);
    puts(flickcurl_license_string);
    rc = 1;
    goto tidy;
  }
  
  tag = argv[1];

  /* Initialise the Flickcurl library */
  fc = flickcurl_new();
  if(!fc) {
    rc = 1;
    goto tidy;
  }

  flickcurl_set_error_handler(fc, my_message_handler, NULL);

  if(!access((const char*)config_path, R_OK)) {
    if(flickcurl_config_read_ini(fc, config_path, config_section,
                                 fc, flickcurl_config_var_handler)) {
      fprintf(stderr, "%s: Failed to read config filename %s: %s\n",
              program, config_path, strerror(errno));
      rc = 1;
      goto tidy;
    }
  }
  

  /* Initialise the search parameters themselves
   *  user_id: "me" - Search only photos of the calling user.
   *  sort: "interestingness-desc" - return results with most interesting first
   *  tag: TAG - search for photos about the TAG given on the command line
   */
  flickcurl_search_params_init(&params);
  /* these strings are shared and not strdup()ed since they are stored 
   * in 'params" on the stack */
  params.user_id = (char*)"me";
  params.sort = (char*)"interestingness-desc";
  params.tags = tag;

  /* Initialise the search result (list) parameters:
   *   per_page: 10 - ten results per-page
   *   page: 1 - return 1 page
   *   extras: "original_format" - want the returned photos to have the
   *      original secret and format fields.
   */
  flickcurl_photos_list_params_init(&list_params);
  list_params.per_page = 10;
  list_params.page = 1;
  /* this string is shared and not strdup()ed since it is stored 
   * in 'list_params" on the stack */
  list_params.extras = "original_format";

  photos_list = flickcurl_photos_search_params(fc, &params, &list_params);
  if(!photos_list)
    goto tidy;

  fprintf(stderr, "%s: Search returned %d photos\n", 
          program, photos_list->photos_count);

  for(i = 0; i < photos_list->photos_count; ++i)
    printf("  Result #%d has ID %s\n", i, photos_list->photos[i]->id);
  
 tidy:
  if(photos_list)
    flickcurl_free_photos_list(photos_list);

  if(fc)
    flickcurl_free(fc);

  flickcurl_finish();

  return(rc);
}

Compile it like this:

$ gcc -o search-photos search-photos.c `pkg-config flickcurl --cflags --libs`

and assuming the flickcurl API configuration has already been created in ~/.flickcurl.conf, run the program like this:

$ ./search-photos kitten
search-photos: Search returned 2 photos
  Result #0 has ID 1234567890
  Result #1 has ID 2345678901

Your kitten results may vary.