Blame examples/subtitle.c

Packit 3ff1e7
/* libquvi
Packit 3ff1e7
 * Copyright (C) 2013  Toni Gundogdu <legatvs@gmail.com>
Packit 3ff1e7
 *
Packit 3ff1e7
 * This file is part of libquvi <http://quvi.sourceforge.net/>.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This program is free software: you can redistribute it and/or
Packit 3ff1e7
 * modify it under the terms of the GNU Affero General Public
Packit 3ff1e7
 * License as published by the Free Software Foundation, either
Packit 3ff1e7
 * version 3 of the License, or (at your option) any later version.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This program is distributed in the hope that it will be useful,
Packit 3ff1e7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff1e7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 3ff1e7
 * GNU Affero General Public License for more details.
Packit 3ff1e7
 *
Packit 3ff1e7
 * You should have received a copy of the GNU Affero General
Packit 3ff1e7
 * Public License along with this program.  If not, see
Packit 3ff1e7
 * <http://www.gnu.org/licenses/>.
Packit 3ff1e7
 */
Packit 3ff1e7
Packit 3ff1e7
#include "config.h"
Packit 3ff1e7
Packit 3ff1e7
#include <locale.h>
Packit 3ff1e7
#include <stdlib.h>
Packit 3ff1e7
#include <string.h>
Packit 3ff1e7
#include <glib.h>
Packit 3ff1e7
#include <quvi.h>
Packit 3ff1e7
Packit 3ff1e7
#include "examples.h"
Packit 3ff1e7
Packit 3ff1e7
struct _opts_s
Packit 3ff1e7
{
Packit 3ff1e7
  gboolean format_subrip;
Packit 3ff1e7
  gboolean autoproxy;
Packit 3ff1e7
  gboolean verbose;
Packit 3ff1e7
  gchar *language;
Packit 3ff1e7
  gchar **url;
Packit 3ff1e7
};
Packit 3ff1e7
Packit 3ff1e7
static struct _opts_s opts;
Packit 3ff1e7
Packit 3ff1e7
static const GOptionEntry entries[] =
Packit 3ff1e7
{
Packit 3ff1e7
  {
Packit 3ff1e7
    "format-subrip", 's', 0, G_OPTION_ARG_NONE, &opts.format_subrip,
Packit 3ff1e7
    "Retrieve selected language, convert it to SubRip and dump to stdout",
Packit 3ff1e7
    NULL
Packit 3ff1e7
  },
Packit 3ff1e7
  {
Packit 3ff1e7
    "language", 'l', 0, G_OPTION_ARG_STRING, &opts.language,
Packit 3ff1e7
    "Select subtitle language by ID, or a comma-separated list of IDs", "ID"
Packit 3ff1e7
  },
Packit 3ff1e7
  {
Packit 3ff1e7
    "autoproxy", 'a', 0, G_OPTION_ARG_NONE, &opts.autoproxy,
Packit 3ff1e7
    "Enable the autoproxy feature", NULL
Packit 3ff1e7
  },
Packit 3ff1e7
  {
Packit 3ff1e7
    "verbose", 'v', 0, G_OPTION_ARG_NONE, &opts.verbose,
Packit 3ff1e7
    "Verbose libcurl output", NULL
Packit 3ff1e7
  },
Packit 3ff1e7
  {
Packit 3ff1e7
    G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &opts.url, "URL"
Packit 3ff1e7
  },
Packit 3ff1e7
  {NULL, 0, 0, 0, NULL, NULL, NULL}
Packit 3ff1e7
};
Packit 3ff1e7
Packit 3ff1e7
static gint opts_new(gint argc, gchar **argv)
Packit 3ff1e7
{
Packit 3ff1e7
  GOptionContext *c;
Packit 3ff1e7
  GError *e;
Packit 3ff1e7
  gint r;
Packit 3ff1e7
Packit 3ff1e7
  c = g_option_context_new("URL");
Packit 3ff1e7
  r = EXIT_SUCCESS;
Packit 3ff1e7
  e = NULL;
Packit 3ff1e7
Packit 3ff1e7
  g_option_context_add_main_entries(c, entries, NULL);
Packit 3ff1e7
  g_option_context_set_help_enabled(c, TRUE);
Packit 3ff1e7
Packit 3ff1e7
  if (g_option_context_parse(c, &argc, &argv, &e) == FALSE)
Packit 3ff1e7
    {
Packit 3ff1e7
      g_printerr("error: %s\n", e->message);
Packit 3ff1e7
      r = EXIT_FAILURE;
Packit 3ff1e7
      g_error_free(e);
Packit 3ff1e7
    }
Packit 3ff1e7
  g_option_context_free(c);
Packit 3ff1e7
Packit 3ff1e7
  if (r == EXIT_SUCCESS && opts.url == NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      g_printerr("error: no input URL\n");
Packit 3ff1e7
      return (EXIT_FAILURE);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (r);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static void dump_language(const quvi_subtitle_lang_t l)
Packit 3ff1e7
{
Packit 3ff1e7
  gchar *translated, *original, *code, *id, *url;
Packit 3ff1e7
Packit 3ff1e7
  quvi_subtitle_lang_get(l, QUVI_SUBTITLE_LANG_PROPERTY_TRANSLATED,
Packit 3ff1e7
                         &translated);
Packit 3ff1e7
Packit 3ff1e7
  quvi_subtitle_lang_get(l, QUVI_SUBTITLE_LANG_PROPERTY_ORIGINAL,
Packit 3ff1e7
                         &original);
Packit 3ff1e7
Packit 3ff1e7
  quvi_subtitle_lang_get(l, QUVI_SUBTITLE_LANG_PROPERTY_CODE, &code);
Packit 3ff1e7
  quvi_subtitle_lang_get(l, QUVI_SUBTITLE_LANG_PROPERTY_URL, &url;;
Packit 3ff1e7
  quvi_subtitle_lang_get(l, QUVI_SUBTITLE_LANG_PROPERTY_ID, &id;;
Packit 3ff1e7
Packit 3ff1e7
  g_printerr("  lang: translated=%s, original=%s, code=%s, id=%s\n"
Packit 3ff1e7
          "    url=%s\n", translated, original, code, id, url);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static void dump_languages(const quvi_subtitle_type_t t)
Packit 3ff1e7
{
Packit 3ff1e7
  quvi_subtitle_lang_t l;
Packit 3ff1e7
  while ( (l = quvi_subtitle_type_next(t)) != NULL)
Packit 3ff1e7
    dump_language(l);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static void dump_subtitle(quvi_subtitle_type_t t)
Packit 3ff1e7
{
Packit 3ff1e7
  gdouble type, format;
Packit 3ff1e7
  quvi_subtitle_type_get(t, QUVI_SUBTITLE_TYPE_PROPERTY_FORMAT, &format);
Packit 3ff1e7
  quvi_subtitle_type_get(t, QUVI_SUBTITLE_TYPE_PROPERTY_TYPE, &type);
Packit 3ff1e7
  g_printerr("[%s] subtitle: format=%g, type=%g\n", __func__, format, type);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static void dump_subtitles()
Packit 3ff1e7
{
Packit 3ff1e7
  quvi_subtitle_type_t t;
Packit 3ff1e7
  gint i;
Packit 3ff1e7
Packit 3ff1e7
  i = 0;
Packit 3ff1e7
  while ( (t = quvi_subtitle_type_next(qsub)) != NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      dump_subtitle(t);
Packit 3ff1e7
      dump_languages(t);
Packit 3ff1e7
      ++i;
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  if (i == 0)
Packit 3ff1e7
    g_printerr("subtitles: none found\n");
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static void opts_free()
Packit 3ff1e7
{
Packit 3ff1e7
  g_free(opts.language);
Packit 3ff1e7
  g_strfreev(opts.url);
Packit 3ff1e7
Packit 3ff1e7
  memset(&opts, 0, sizeof(struct _opts_s));
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
typedef quvi_callback_status qcs;
Packit 3ff1e7
Packit 3ff1e7
gint main(gint argc, gchar **argv)
Packit 3ff1e7
{
Packit 3ff1e7
  quvi_subtitle_export_t e;
Packit 3ff1e7
  quvi_subtitle_lang_t l;
Packit 3ff1e7
  gint r, i;
Packit 3ff1e7
Packit 3ff1e7
  memset(&opts, 0, sizeof(struct _opts_s));
Packit 3ff1e7
  setlocale(LC_ALL, "");
Packit 3ff1e7
Packit 3ff1e7
  r = opts_new(argc, argv);
Packit 3ff1e7
  if (r != EXIT_SUCCESS)
Packit 3ff1e7
    return (r);
Packit 3ff1e7
Packit 3ff1e7
  q = quvi_new();
Packit 3ff1e7
  examples_exit_if_error();
Packit 3ff1e7
Packit 3ff1e7
  if (opts.autoproxy == TRUE)
Packit 3ff1e7
    examples_enable_autoproxy();
Packit 3ff1e7
Packit 3ff1e7
  if (opts.verbose == TRUE)
Packit 3ff1e7
    examples_enable_verbose();
Packit 3ff1e7
Packit 3ff1e7
  if (opts.format_subrip == TRUE && opts.language == NULL)
Packit 3ff1e7
    {
Packit 3ff1e7
      g_printerr("error: --format-subrip requires --language\n");
Packit 3ff1e7
      examples_cleanup();
Packit 3ff1e7
      opts_free();
Packit 3ff1e7
      return (EXIT_FAILURE);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  quvi_set(q, QUVI_OPTION_CALLBACK_STATUS, (qcs) examples_status);
Packit 3ff1e7
Packit 3ff1e7
  for (r=EXIT_SUCCESS, i=0; (opts.url[i] != NULL && r == EXIT_SUCCESS); ++i)
Packit 3ff1e7
    {
Packit 3ff1e7
      qsub = quvi_subtitle_new(q, opts.url[i]);
Packit 3ff1e7
      examples_exit_if_error();
Packit 3ff1e7
Packit 3ff1e7
      if (opts.language != NULL)
Packit 3ff1e7
        {
Packit 3ff1e7
          l = quvi_subtitle_select(qsub, opts.language);
Packit 3ff1e7
          examples_exit_if_error();
Packit 3ff1e7
          dump_language(l);
Packit 3ff1e7
          if (opts.format_subrip == TRUE)
Packit 3ff1e7
            {
Packit 3ff1e7
              e = quvi_subtitle_export_new(l, "srt");
Packit 3ff1e7
              examples_exit_if_error();
Packit 3ff1e7
              g_print("%s", quvi_subtitle_export_data(e));
Packit 3ff1e7
              quvi_subtitle_export_free(e);
Packit 3ff1e7
            }
Packit 3ff1e7
        }
Packit 3ff1e7
      else
Packit 3ff1e7
        dump_subtitles();
Packit 3ff1e7
    }
Packit 3ff1e7
  examples_cleanup();
Packit 3ff1e7
  opts_free();
Packit 3ff1e7
Packit 3ff1e7
  return (r);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* vim: set ts=2 sw=2 tw=72 expandtab: */