Blame src/lua/exec_subtitle_script_parse.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 library 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 library 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 library.  If not, see
Packit 3ff1e7
 * <http://www.gnu.org/licenses/>.
Packit 3ff1e7
 */
Packit 3ff1e7
Packit 3ff1e7
/*
Packit 3ff1e7
 * NOTE: The error messages produced in these functions are intended for
Packit 3ff1e7
 *       developers. They would typically be seen when a new script is
Packit 3ff1e7
 *       being developed or an old one is being maintained.
Packit 3ff1e7
 *
Packit 3ff1e7
 *       These messages should be clear, indicating the actual error,
Packit 3ff1e7
 *       minimizing the time spent on locating the problem in the script.
Packit 3ff1e7
 */
Packit 3ff1e7
Packit 3ff1e7
#include "config.h"
Packit 3ff1e7
Packit 3ff1e7
#include <lauxlib.h>
Packit 3ff1e7
#include <glib.h>
Packit 3ff1e7
Packit 3ff1e7
#include "quvi.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "_quvi_s.h"
Packit 3ff1e7
#include "_quvi_subtitle_s.h"
Packit 3ff1e7
#include "_quvi_script_s.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "misc/subtitle.h"
Packit 3ff1e7
#include "lua/setfield.h"
Packit 3ff1e7
#include "lua/chk.h"
Packit 3ff1e7
#include "lua/def.h"
Packit 3ff1e7
Packit 3ff1e7
static const gchar script_func[] = "parse";
Packit 3ff1e7
Packit 3ff1e7
/* Verify that the language has an ID (if there are >1 languages). */
Packit 3ff1e7
static void _chk_lang_id(lua_State *l, _quvi_subtitle_type_t qst,
Packit 3ff1e7
                         _quvi_subtitle_lang_t qsl, const gchar *script_path,
Packit 3ff1e7
                         const gint i)
Packit 3ff1e7
{
Packit 3ff1e7
  if (g_slist_length(qst->languages) <2)
Packit 3ff1e7
    return;
Packit 3ff1e7
Packit 3ff1e7
  if (qsl->id->len ==0)
Packit 3ff1e7
    {
Packit 3ff1e7
      g_warning("%s: %s: `qargs.%s[%d].%s' should not be empty; "
Packit 3ff1e7
                "each language should have an ID when there are >1 languages",
Packit 3ff1e7
                script_path, script_func, SUS_SUBTITLES, i, SUSSL_ID);
Packit 3ff1e7
    }
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
typedef void (*foreach_cb)(lua_State*, _quvi_subtitle_type_t,
Packit 3ff1e7
                           const gchar*, const gint);
Packit 3ff1e7
Packit 3ff1e7
/* Process a "sublevel" of a dictionary, e.g. qargs.subtitles.lang */
Packit 3ff1e7
static void _chk_subtitle_sublevel(lua_State *l, const gchar *level,
Packit 3ff1e7
                                   _quvi_subtitle_type_t qst,
Packit 3ff1e7
                                   const gchar *script_path,
Packit 3ff1e7
                                   const foreach_cb cb, const gint i)
Packit 3ff1e7
{
Packit 3ff1e7
  if (lua_isstring(l, LI_KEY) && lua_istable(l, LI_VALUE))
Packit 3ff1e7
    {
Packit 3ff1e7
      if (g_strcmp0(lua_tostring(l, LI_KEY), level) ==0)
Packit 3ff1e7
        cb(l, qst, script_path, i);
Packit 3ff1e7
    }
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Return a new language handle. */
Packit 3ff1e7
static gpointer _subtitle_lang_new(_quvi_subtitle_type_t qst)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_subtitle_lang_t qsl = g_new0(struct _quvi_subtitle_lang_s, 1);
Packit 3ff1e7
  /* Handle. */
Packit 3ff1e7
  qsl->handle.quvi = qst->handle.quvi;
Packit 3ff1e7
  /* Strings. */
Packit 3ff1e7
  qsl->translated = g_string_new(NULL);
Packit 3ff1e7
  qsl->original = g_string_new(NULL);
Packit 3ff1e7
  qsl->code = g_string_new(NULL);
Packit 3ff1e7
  qsl->url = g_string_new(NULL);
Packit 3ff1e7
  qsl->id = g_string_new(NULL);
Packit 3ff1e7
  /* double */
Packit 3ff1e7
  qsl->format = qst->format;
Packit 3ff1e7
  return (qsl);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Return a new data handle. */
Packit 3ff1e7
static gpointer _subtitle_type_new(const _quvi_subtitle_t qsub)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_subtitle_type_t qst = g_new0(struct _quvi_subtitle_type_s, 1);
Packit 3ff1e7
  /* Handle. */
Packit 3ff1e7
  qst->handle.quvi = qsub->handle.quvi;
Packit 3ff1e7
  /* double. */
Packit 3ff1e7
  qst->format = -1;
Packit 3ff1e7
  qst->type = -1;
Packit 3ff1e7
  return (qst);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Must have at least one language URL. */
Packit 3ff1e7
static _quvi_subtitle_lang_t _chk_url(lua_State *l, const gchar *script_path,
Packit 3ff1e7
                                      _quvi_subtitle_lang_t qsl, gint i)
Packit 3ff1e7
{
Packit 3ff1e7
  if (qsl->url->len ==0)
Packit 3ff1e7
    {
Packit 3ff1e7
      m_subtitle_lang_free(qsl);
Packit 3ff1e7
Packit 3ff1e7
      luaL_error(l, "%s: %s: must return `qargs.%s[%d].%s[1].%s'",
Packit 3ff1e7
                 script_path, script_func, SUS_SUBTITLES, i,
Packit 3ff1e7
                 SUSS_LANG, SUSSL_URL);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (qsl);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Return a new lang handle. */
Packit 3ff1e7
static _quvi_subtitle_lang_t _new_lang(lua_State *l, const gchar *script_path,
Packit 3ff1e7
                                       const _quvi_subtitle_type_t qst,
Packit 3ff1e7
                                       const gint i)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_subtitle_lang_t qsl = _subtitle_lang_new(qst);
Packit 3ff1e7
  lua_pushnil(l);
Packit 3ff1e7
  while (lua_next(l, LI_KEY))
Packit 3ff1e7
    {
Packit 3ff1e7
      l_chk_assign_s(l, SUSSL_TRANSLATED, qsl->translated, TRUE, FALSE);
Packit 3ff1e7
      l_chk_assign_s(l, SUSSL_ORIGINAL, qsl->original, TRUE, FALSE);
Packit 3ff1e7
      l_chk_assign_s(l, SUSSL_CODE, qsl->code, TRUE, FALSE);
Packit 3ff1e7
      l_chk_assign_s(l, SUSSL_URL, qsl->url, TRUE, TRUE);
Packit 3ff1e7
      l_chk_assign_s(l, SUSSL_ID, qsl->id, TRUE, FALSE);
Packit 3ff1e7
      lua_pop(l, 1);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (_chk_url(l, script_path, qsl, i));
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* For each qargs.subtitles.lang property. */
Packit 3ff1e7
static void _foreach_lang_property(lua_State *l, _quvi_subtitle_type_t qst,
Packit 3ff1e7
                                   const gchar *script_path, const gint i)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_subtitle_lang_t qsl;
Packit 3ff1e7
  gint j;
Packit 3ff1e7
Packit 3ff1e7
  lua_pushnil(l);
Packit 3ff1e7
  j = 0;
Packit 3ff1e7
Packit 3ff1e7
  while (lua_next(l, LI_KEY))
Packit 3ff1e7
    {
Packit 3ff1e7
      if (lua_istable(l, LI_VALUE))
Packit 3ff1e7
        {
Packit 3ff1e7
          qsl = _new_lang(l, script_path, qst, ++j);
Packit 3ff1e7
          _chk_lang_id(l, qst, qsl, script_path, j);
Packit 3ff1e7
          qst->languages = g_slist_prepend(qst->languages, qsl);
Packit 3ff1e7
        }
Packit 3ff1e7
      lua_pop(l, 1);
Packit 3ff1e7
    }
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Verify required subtitle data properties. */
Packit 3ff1e7
static gpointer _chk_req_type(lua_State *l, _quvi_subtitle_type_t qst,
Packit 3ff1e7
                              const gint i, const gchar *script_path)
Packit 3ff1e7
{
Packit 3ff1e7
  static const gchar *_E = "%s: %s: must return `qargs.%s[%d].%s'";
Packit 3ff1e7
Packit 3ff1e7
  if (qst->format <0)
Packit 3ff1e7
    {
Packit 3ff1e7
      luaL_error(l, _E, script_path, script_func,
Packit 3ff1e7
                 SUS_SUBTITLES, i, SUSS_FORMAT);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  if (qst->type <0)
Packit 3ff1e7
    {
Packit 3ff1e7
      luaL_error(l, _E, script_path, script_func,
Packit 3ff1e7
                 SUS_SUBTITLES, i, SUSS_TYPE);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  /* Not all videos have subtitles of any kind. */
Packit 3ff1e7
  if (g_slist_length(qst->languages) ==0)
Packit 3ff1e7
    {
Packit 3ff1e7
      m_subtitle_type_free(qst);
Packit 3ff1e7
      return (NULL);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
#ifdef _1
Packit 3ff1e7
  if (g_slist_length(qst->languages) ==0)
Packit 3ff1e7
    {
Packit 3ff1e7
      luaL_error(l, "%s: %s: qargs.%s[%d].%s must not be empty",
Packit 3ff1e7
                 script_path, script_func, SUS_SUBTITLES, i, SUSS_LANG);
Packit 3ff1e7
    }
Packit 3ff1e7
#endif
Packit 3ff1e7
Packit 3ff1e7
  qst->languages = g_slist_reverse(qst->languages);
Packit 3ff1e7
  return (qst);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* Return a new data handle. */
Packit 3ff1e7
static _quvi_subtitle_type_t _new_type(lua_State *l, _quvi_subtitle_t qsub,
Packit 3ff1e7
                                       const gchar *script_path, const gint i)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_subtitle_type_t qst = _subtitle_type_new(qsub);
Packit 3ff1e7
  lua_pushnil(l);
Packit 3ff1e7
  while (lua_next(l, LI_KEY))
Packit 3ff1e7
    {
Packit 3ff1e7
      _chk_subtitle_sublevel(l, SUSS_LANG, qst, script_path,
Packit 3ff1e7
                             _foreach_lang_property, i);
Packit 3ff1e7
      l_chk_assign_n(l, SUSS_FORMAT, &qst->format);
Packit 3ff1e7
      l_chk_assign_n(l, SUSS_TYPE, &qst->type);
Packit 3ff1e7
      lua_pop(l, 1);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (_chk_req_type(l, qst, i, script_path));
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* For each qargs.subtitle */
Packit 3ff1e7
static void _foreach_subtitle(lua_State *l, _quvi_subtitle_t qsub,
Packit 3ff1e7
                              const gchar *script_path)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_subtitle_type_t qst;
Packit 3ff1e7
  gint i;
Packit 3ff1e7
Packit 3ff1e7
  lua_pushnil(l);
Packit 3ff1e7
  i = 0;
Packit 3ff1e7
Packit 3ff1e7
  while (lua_next(l, LI_KEY))
Packit 3ff1e7
    {
Packit 3ff1e7
      if (lua_istable(l, LI_VALUE))
Packit 3ff1e7
        {
Packit 3ff1e7
          qst = _new_type(l, qsub, script_path, ++i);
Packit 3ff1e7
          if (qst != NULL)
Packit 3ff1e7
            qsub->types = g_slist_prepend(qsub->types, qst);
Packit 3ff1e7
        }
Packit 3ff1e7
      lua_pop(l, 1);
Packit 3ff1e7
    }
Packit 3ff1e7
  qsub->types = g_slist_reverse(qsub->types);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static void _chk_subtitles(lua_State *l, _quvi_subtitle_t qsub,
Packit 3ff1e7
                           const gchar *script_path)
Packit 3ff1e7
{
Packit 3ff1e7
  lua_pushstring(l, SUS_SUBTITLES);
Packit 3ff1e7
  lua_gettable(l, LI_KEY);
Packit 3ff1e7
Packit 3ff1e7
  if (lua_istable(l, LI_VALUE))
Packit 3ff1e7
    _foreach_subtitle(l, qsub, script_path);
Packit 3ff1e7
  else
Packit 3ff1e7
    {
Packit 3ff1e7
      static const gchar *_E =
Packit 3ff1e7
        "%s: %s: must return a dictionary containing the `qargs.%s'";
Packit 3ff1e7
Packit 3ff1e7
      luaL_error(l, _E, script_path, script_func, SUS_SUBTITLES);
Packit 3ff1e7
    }
Packit 3ff1e7
  lua_pop(l, 1);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
extern gint c_reset(_quvi_t);
Packit 3ff1e7
Packit 3ff1e7
QuviError l_exec_subtitle_script_parse(gpointer p, GSList *sl)
Packit 3ff1e7
{
Packit 3ff1e7
  _quvi_subtitle_t qsub;
Packit 3ff1e7
  _quvi_script_t qs;
Packit 3ff1e7
  lua_State *l;
Packit 3ff1e7
Packit 3ff1e7
  qsub = (_quvi_subtitle_t) p;
Packit 3ff1e7
  l = qsub->handle.quvi->handle.lua;
Packit 3ff1e7
Packit 3ff1e7
  c_reset(qsub->handle.quvi);
Packit 3ff1e7
Packit 3ff1e7
  qs = (_quvi_script_t) sl->data;
Packit 3ff1e7
  lua_getglobal(l, script_func);
Packit 3ff1e7
Packit 3ff1e7
  if (!lua_isfunction(l, -1))
Packit 3ff1e7
    {
Packit 3ff1e7
      luaL_error(l, "%s: the function `%s' was not found",
Packit 3ff1e7
                 qs->fpath->str, script_func);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  lua_newtable(l);
Packit 3ff1e7
  l_set_reg_userdata(l, USERDATA_QUVI_T, (gpointer) qsub->handle.quvi);
Packit 3ff1e7
  l_setfield_s(l, SUS_INPUT_URL, qsub->url.input->str, -1);
Packit 3ff1e7
Packit 3ff1e7
  if (lua_pcall(l, 1, 1, 0))
Packit 3ff1e7
    {
Packit 3ff1e7
      g_string_assign(qsub->handle.quvi->status.errmsg, lua_tostring(l, -1));
Packit 3ff1e7
      return (QUVI_ERROR_SCRIPT);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  if (!lua_istable(l, -1))
Packit 3ff1e7
    {
Packit 3ff1e7
      static const gchar *_E =
Packit 3ff1e7
        "%s: %s: must return a dictionary, this is typically the `qargs'";
Packit 3ff1e7
Packit 3ff1e7
      luaL_error(l, _E, qs->fpath->str, script_func);
Packit 3ff1e7
    }
Packit 3ff1e7
Packit 3ff1e7
  _chk_subtitles(l, qsub, qs->fpath->str);
Packit 3ff1e7
  lua_pop(l, 1);
Packit 3ff1e7
Packit 3ff1e7
  return (QUVI_OK);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* vim: set ts=2 sw=2 tw=72 expandtab: */