2 * Copyright (C) 2012 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of libquvi <http://quvi.sourceforge.net/>.
6 * This library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
22 * NOTE: The error messages produced in these functions are intended for
23 * developers. They would typically be seen when a new script is
24 * being developed or an old one is being maintained.
26 * These messages should be clear, indicating the actual error,
27 * minimizing the time spent on locating the problem in the script.
38 #include "_quvi_scan_s.h"
39 #include "_quvi_script_s.h"
41 #include "lua/setfield.h"
44 static const gchar script_func
[] = "parse";
46 static void _foreach_media_url(lua_State
*l
, _quvi_scan_t qs
,
47 const gchar
*script_path
)
50 while (lua_next(l
, LI_KEY
))
52 if (lua_isstring(l
, LI_KEY
) && lua_isstring(l
, LI_VALUE
))
54 const gchar
*url
= lua_tostring(l
, LI_VALUE
);
55 qs
->url
.media
= g_slist_prepend(qs
->url
.media
, g_strdup(url
));
59 qs
->url
.media
= g_slist_reverse(qs
->url
.media
);
62 /* Check for 'qargs.media_url'. */
63 static void _chk_media_url(lua_State
*l
, _quvi_scan_t qs
,
64 const gchar
*script_path
)
66 lua_pushstring(l
, SS_MEDIA_URL
);
67 lua_gettable(l
, LI_KEY
);
69 if (lua_istable(l
, LI_VALUE
))
70 _foreach_media_url(l
, qs
, script_path
);
73 g_warning("%s: %s: should return a dictionary containing "
75 script_path
, script_func
, SS_MEDIA_URL
);
80 QuviError
l_exec_scan_script_parse(gpointer p
, gpointer _qss
,
87 qss
= (_quvi_script_t
) _qss
;
88 qs
= (_quvi_scan_t
) p
;
90 l
= qs
->handle
.quvi
->handle
.lua
;
93 if (luaL_dofile(l
, qss
->fpath
->str
))
94 luaL_error(l
, "%s", lua_tostring(l
, -1));
96 lua_getglobal(l
, script_func
);
98 if (!lua_isfunction(l
, -1))
100 luaL_error(l
, "%s: the function `%s' was not found",
101 qss
->fpath
->str
, script_func
);
105 l_set_reg_userdata(l
, USERDATA_QUVI_T
, (gpointer
) qs
->handle
.quvi
);
106 l_setfield_s(l
, SS_INPUT_URL
, qs
->url
.input
->str
, -1);
107 l_setfield_s(l
, SS_CONTENT
, data
, -1);
109 if (lua_pcall(l
, 1, 1, 0))
111 g_string_assign(qs
->handle
.quvi
->status
.errmsg
, lua_tostring(l
, -1));
112 return (QUVI_ERROR_SCRIPT
);
115 if (!lua_istable(l
, -1))
117 static const gchar
*_E
=
118 "%s: %s: must return a dictionary, this is typically the `qargs'";
120 luaL_error(l
, _E
, qss
->fpath
->str
, script_func
);
123 _chk_media_url(l
, qs
, qss
->fpath
->str
);
129 /* vim: set ts=2 sw=2 tw=72 expandtab: */