Add misc/url.* with m_url_(un)escaped_form
[libquvi.git] / src / lua / exec_playlist_script_parse.c
blob6b850615ca13800a5a0f0cbbe507e17c820f4774
1 /* libquvi
2 * Copyright (C) 2012,2013 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.
30 #include "config.h"
32 #include <lauxlib.h>
33 #include <glib.h>
35 #include "quvi.h"
36 /* -- */
37 #include "_quvi_s.h"
38 #include "_quvi_playlist_s.h"
39 #include "_quvi_script_s.h"
40 /* -- */
41 #include "lua/setfield.h"
42 #include "lua/chk.h"
43 #include "lua/def.h"
44 #include "misc/playlist.h"
46 static const gchar script_func[] = "parse";
48 static gpointer _playlist_media_new()
50 _quvi_playlist_media_t qpm = g_new0(struct _quvi_playlist_media_s, 1);
51 qpm->title = g_string_new(NULL);
52 qpm->url = g_string_new(NULL);
53 return (qpm);
56 static gboolean _new_media(lua_State *l, _quvi_playlist_t qp,
57 const gchar *script_path, const gint i,
58 _quvi_playlist_media_t *qpm)
60 *qpm = _playlist_media_new();
62 lua_pushnil(l);
63 while (lua_next(l, LI_KEY)) /* For each qargs.media */
65 l_chk_assign_n(l, PSM_DURATION_MS, &(*qpm)->duration_ms);
66 l_chk_assign_s(l, PSM_TITLE, (*qpm)->title, TRUE);
67 l_chk_assign_s(l, PSM_URL, (*qpm)->url, TRUE);
68 lua_pop(l, 1);
71 if ((*qpm)->url->len ==0)
73 m_playlist_media_free(*qpm);
74 *qpm = NULL;
77 return ( ((*qpm)->url->len >0) ? TRUE:FALSE);
80 /* For each qargs.media */
81 static void _foreach_media(lua_State *l, _quvi_playlist_t qp,
82 const gchar *script_path)
84 _quvi_playlist_media_t qpm;
85 gint i;
87 i = 0;
89 lua_pushnil(l);
90 while (lua_next(l, LI_KEY))
92 if (lua_istable(l, LI_VALUE))
94 if (_new_media(l, qp, script_path, ++i, &qpm) == TRUE)
95 qp->media = g_slist_prepend(qp->media, qpm);
97 lua_pop(l, 1);
99 qp->media = g_slist_reverse(qp->media);
102 /* Check for qargs.media */
103 static void _chk_media(lua_State *l, _quvi_playlist_t qp,
104 const gchar *script_path)
106 lua_pushstring(l, PS_MEDIA);
107 lua_gettable(l, LI_KEY);
109 if (lua_istable(l, LI_VALUE))
110 _foreach_media(l, qp, script_path);
111 else
113 g_warning("%s: %s: should return a dictionary containing "
114 "the `qargs.%s' dictionary",
115 script_path, script_func, PS_MEDIA);
117 lua_pop(l, 1);
120 static void _chk_optional(lua_State *l, _quvi_playlist_t qp)
122 lua_pushnil(l);
123 while (lua_next(l, LI_KEY))
125 l_chk_assign_s(l, PS_THUMB_URL, qp->url.thumbnail, TRUE);
126 l_chk_assign_s(l, PS_ID, qp->id.playlist, TRUE);
127 l_chk_assign_s(l, PS_TITLE, qp->title, TRUE);
128 lua_pop(l, 1);
132 extern gint c_reset(_quvi_t);
134 QuviError l_exec_playlist_script_parse(gpointer p, GSList *sl)
136 _quvi_playlist_t qp;
137 _quvi_script_t qs;
138 lua_State *l;
140 qp = (_quvi_playlist_t) p;
141 l = qp->handle.quvi->handle.lua;
143 c_reset(qp->handle.quvi);
145 qs = (_quvi_script_t) sl->data;
146 lua_getglobal(l, script_func);
148 if (!lua_isfunction(l, -1))
150 luaL_error(l, "%s: the function `%s' was not found",
151 qs->fpath->str, script_func);
154 lua_newtable(l);
155 l_set_reg_userdata(l, USERDATA_QUVI_T, (gpointer) qp->handle.quvi);
156 l_setfield_s(l, PS_INPUT_URL, qp->url.input->str, -1);
158 if (lua_pcall(l, 1, 1, 0))
160 g_string_assign(qp->handle.quvi->status.errmsg, lua_tostring(l, -1));
161 return (QUVI_ERROR_SCRIPT);
164 if (!lua_istable(l, -1))
166 static const gchar *_E =
167 "%s: %s: must return a dictionary, this is typically the `qargs'";
169 luaL_error(l, _E, qs->fpath->str, script_func);
172 _chk_optional(l, qp);
173 _chk_media(l, qp, qs->fpath->str);
175 lua_pop(l, 1);
176 return (QUVI_OK);
179 /* vim: set ts=2 sw=2 tw=72 expandtab: */