File: libvalhalla-test.c

package info (click to toggle)
libvalhalla 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,148 kB
  • ctags: 1,442
  • sloc: ansic: 9,826; sh: 1,153; php: 492; makefile: 278
file content (298 lines) | stat: -rw-r--r-- 8,259 bytes parent folder | download
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
/*
 * GeeXboX Valhalla: tiny media scanner API.
 * Copyright (C) 2009 Mathieu Schroeter <mathieu.schroeter@gamesover.ch>
 *
 * This file is part of libvalhalla.
 *
 * libvalhalla is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libvalhalla is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with libvalhalla; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <time.h>

#include "valhalla.h"
#include "utils.h"    /* for macro VH_TIMERSUB */

#define APPNAME "libvalhalla-test"

#define TESTVALHALLA_HELP \
  APPNAME " for libvalhalla-" LIBVALHALLA_VERSION_STR "\n" \
  "\n" \
  "Usage: " APPNAME " [options ...] paths ...\n" \
  "\n" \
  "Options:\n" \
  " -h --help               this help\n" \
  " -v --verbose            increase verbosity\n" \
  " -l --loop               number of loops\n" \
  " -t --timewait           time to wait between loops [sec]\n" \
  " -m --timelimit          time limit [ms] for the scanning\n" \
  " -a --priority           priority for the threads\n" \
  " -d --database           path for the database\n" \
  " -f --download           path for the downloader destination\n" \
  " -c --commit-int         commits interval\n" \
  " -p --parser             number of parsers\n" \
  " -n --decrap             enable decrapifier (for title metadata)\n" \
  " -k --keyword            keyword for the decrapifier\n" \
  " -s --suffix             file suffix (extension)\n" \
  " -g --grabber            grabber to be used\n" \
  "\n" \
  "Example:\n" \
  " $ " APPNAME " -l 2 -t 5 -d ./mydb.db -p 1 -a 15 -s ogg -s mp3 /home/foobar/music\n" \
  "\n" \
  "Default values are loop=1, timewait=0, database=./valhalla.db,\n" \
  "                   commit-int=128, parser=2, priority=19,\n" \
  "                   suffix=flac,m4a,mp3,ogg,wav,wma\n" \
  "                          avi,mkv,mov,mpg,wmv\n" \
  "                          bmp,gif,jpeg,jpg,png,tga,tif,tiff\n" \
  "\n"

#define SUFFIX_MAX 16
#define KEYWORD_MAX 16
#define GRABBER_MAX 16

int
main (int argc, char **argv)
{
  int rc, i;
  int loop_nb = 1, loop_wait = 0, time_limit = 0;
  int priority = 19, commit = 128, decrap = 0;
  valhalla_t *handle;
  valhalla_verb_t verbosity = VALHALLA_MSG_WARNING;
  const char *database = "./valhalla.db";
  const char *download = NULL;
#ifdef USE_GRABBER
  const char *grabber = NULL;
#endif /* USE_GRABBER */
  int parser_nb = 2, sid = 0, kid = 0, gid = 0;
  const char *suffix[SUFFIX_MAX];
  const char *keyword[KEYWORD_MAX];
  const char *grabbers[GRABBER_MAX];
  struct timespec tss, tse, tsd;

  int c, index;
  const char *const short_options = "hvl:t:m:a:d:f:c:p:nk:s:g:";
  const struct option long_options[] = {
    { "help",       no_argument,       0, 'h'  },
    { "verbose",    no_argument,       0, 'v'  },
    { "loop",       required_argument, 0, 'l'  },
    { "timewait",   required_argument, 0, 't'  },
    { "timelimit",  required_argument, 0, 'm'  },
    { "priority",   required_argument, 0, 'a'  },
    { "database",   required_argument, 0, 'd'  },
    { "download",   required_argument, 0, 'f'  },
    { "commit-int", required_argument, 0, 'c'  },
    { "parser",     required_argument, 0, 'p'  },
    { "decrap",     no_argument,       0, 'n'  },
    { "keyword",    required_argument, 0, 'k'  },
    { "suffix",     required_argument, 0, 's'  },
    { "grabber",    required_argument, 0, 'g'  },
    { NULL,         0,                 0, '\0' },
  };

  for (;;)
  {
    c = getopt_long (argc, argv, short_options, long_options, &index);

    if (c == EOF)
      break;

    switch (c)
    {
    case 0:
      break;

    case '?':
    case 'h':
      printf (TESTVALHALLA_HELP);
      return 0;

    case 'v':
      if (verbosity == VALHALLA_MSG_WARNING)
        verbosity = VALHALLA_MSG_INFO;
      else
        verbosity = VALHALLA_MSG_VERBOSE;
      break;

    case 'l':
      loop_nb = atoi (optarg);
      break;

    case 't':
      loop_wait = atoi (optarg);
      break;

    case 'm':
      time_limit = atoi (optarg);
      break;

    case 'a':
      priority = atoi (optarg);
      break;

    case 'd':
      database = optarg;
      break;

    case 'f':
      download = optarg;
      break;

    case 'c':
      commit = atoi (optarg);
      break;

    case 'p':
      parser_nb = atoi (optarg);
      break;

    case 's':
      if (sid < SUFFIX_MAX)
        suffix[sid++] = optarg;
      break;

    case 'k':
      if (kid < KEYWORD_MAX)
        keyword[kid++] = optarg;
    case 'n':
      decrap = 1;
      break;

    case 'g':
      if (gid < GRABBER_MAX)
        grabbers[gid++] = optarg;
      break;

    default:
      printf (TESTVALHALLA_HELP);
      return -1;
    }
  }

  valhalla_verbosity (verbosity);

  handle = valhalla_init (database, parser_nb, decrap, commit, NULL, NULL);
  if (!handle)
    return -1;

  for (i = 0; i < sid; i++)
  {
    valhalla_suffix_add (handle, suffix[i]);
    printf ("Add suffix: %s\n", suffix[i]);
  }

  if (!sid)
  {
    /* audio */
    valhalla_suffix_add (handle, "flac");
    valhalla_suffix_add (handle, "m4a");
    valhalla_suffix_add (handle, "mp3");
    valhalla_suffix_add (handle, "ogg");
    valhalla_suffix_add (handle, "wav");
    valhalla_suffix_add (handle, "wma");

    /* video */
    valhalla_suffix_add (handle, "avi");
    valhalla_suffix_add (handle, "mkv");
    valhalla_suffix_add (handle, "mov");
    valhalla_suffix_add (handle, "mpg");
    valhalla_suffix_add (handle, "wmv");

    /* image */
    valhalla_suffix_add (handle, "bmp");
    valhalla_suffix_add (handle, "gif");
    valhalla_suffix_add (handle, "jpeg");
    valhalla_suffix_add (handle, "jpg");
    valhalla_suffix_add (handle, "png");
    valhalla_suffix_add (handle, "tga");
    valhalla_suffix_add (handle, "tif");
    valhalla_suffix_add (handle, "tiff");

    printf ("Default suffixes: flac,m4a,mp3,ogg,wav,wma\n"
            "                  avi,mkv,mov,mpg,wmv\n"
            "                  bmp,gif,jpeg,jpg,png,tga,tif,tiff\n");
  }

  for (i = 0; i < kid; i++)
  {
    valhalla_bl_keyword_add (handle, keyword[i]);
    printf ("Add keyword in the blacklist: %s\n", keyword[i]);
  }

  if (download)
  {
    printf ("Destination directory for downloaded files: %s\n", download);
    valhalla_downloader_dest_set (handle, VALHALLA_DL_DEFAULT, download);
  }

  while (optind < argc)
  {
    valhalla_path_add (handle, argv[optind], 1);
    printf ("Add path: %s\n", argv[optind++]);
  }

  printf ("Run: parser=%i loop=%i wait=%i priority=%i commit-int=%i\n",
          parser_nb, loop_nb, loop_wait, priority, commit);

#ifdef USE_GRABBER
  printf ("Grabbers available:\n");
  while ((grabber = valhalla_grabber_list_get (handle, grabber)))
  {
    if (gid == 0) /* no grabber has been specified */
      printf ("  %s\n", grabber);
    else
    {
      valhalla_grabber_state_set (handle, grabber, 0);
      for (i = 0; i < gid; i++)
      {
        if (!strcmp (grabber, grabbers[i]))
        {
          valhalla_grabber_state_set (handle, grabber, 1);
          printf ("  %s\n", grabber);
          break;
        }
      }
    }
  }
#endif /* USE_GRABBER */

  clock_gettime (CLOCK_REALTIME, &tss);

  rc = valhalla_run (handle, loop_nb, loop_wait, priority);
  if (rc)
  {
    fprintf (stderr, "Error code: %i\n", rc);
    valhalla_uninit (handle);
    return -1;
  }

  if (!time_limit)
    valhalla_wait (handle);
  else
    usleep (time_limit * 1000);

  clock_gettime (CLOCK_REALTIME, &tse);

  valhalla_uninit (handle);

  VH_TIMERSUB (&tse, &tss, &tsd);
  printf ("Time : %f sec\n", tsd.tv_sec + tsd.tv_nsec / 1000000000.0);

  return 0;
}