From 833b4a052039a1745f92dfe3dcfd80d129e3c9a3 Mon Sep 17 00:00:00 2001 From: Toni Gundogdu Date: Tue, 14 Dec 2010 14:17:31 +0200 Subject: [PATCH] Extend quvi.fetch to allow setting user-agent [11] Modify quvi.fetch so that the options can be passed in a table instead of args. - http://sourceforge.net/apps/trac/quvi/ticket/11 --- lib/curl_wrap.c | 36 ++++++++++++++++++++++++----- lib/curl_wrap.h | 4 +--- lib/lua_wrap.c | 47 ++++++++++++++++---------------------- share/lua/website/README | 11 +++++---- share/lua/website/cbsnews.lua | 2 +- share/lua/website/clipfish.lua | 2 +- share/lua/website/collegehumor.lua | 2 +- share/lua/website/dailymotion.lua | 4 +++- share/lua/website/golem.lua | 7 +++--- share/lua/website/liveleak.lua | 6 +++-- share/lua/website/sevenload.lua | 2 +- share/lua/website/spiegel.lua | 5 ++-- share/lua/website/vimeo.lua | 2 +- share/lua/website/youtube.lua | 3 ++- 14 files changed, 76 insertions(+), 57 deletions(-) diff --git a/lib/curl_wrap.c b/lib/curl_wrap.c index bfd2cf7..fe422ee 100644 --- a/lib/curl_wrap.c +++ b/lib/curl_wrap.c @@ -58,11 +58,14 @@ quvi_write_callback_default(void *p, size_t size, size_t nmemb, void *data) return (rsize); } +/* lua_wrap.c */ +extern char *lua_get_field_s(lua_State *, const char *); + QUVIcode -fetch_to_mem(_quvi_video_t video, - const char *url, - const char *cookie_header, const QUVIstatusType type, char **dst) +fetch_to_mem(_quvi_video_t video, const char *url, lua_State * l, char **dst) { + char *fetch_type, *arbitrary_cookie, *user_agent; + QUVIstatusType fetch_type_n; long respcode, conncode; CURLcode curlcode; struct mem_s mem; @@ -81,9 +84,27 @@ fetch_to_mem(_quvi_video_t video, return (QUVI_INVARG); *dst = NULL; + fetch_type = NULL; + user_agent = NULL; + arbitrary_cookie = NULL; + fetch_type_n = QUVISTATUSTYPE_PAGE; /* default */ + + /* Additional settings from LUA table */ + + if (lua_istable(l, 2)) { + fetch_type = lua_get_field_s(l, "fetch_type"); + if (fetch_type) { + if (strcmp(fetch_type, "config") == 0) + fetch_type_n = QUVISTATUSTYPE_CONFIG; + else if (strcmp(fetch_type, "playlist") == 0) + fetch_type_n = QUVISTATUSTYPE_PLAYLIST; + } + arbitrary_cookie = lua_get_field_s(l, "arbitrary_cookie"); + user_agent = lua_get_field_s(l, "user_agent"); + } if (quvi->status_func) { - if (quvi->status_func(makelong(QUVISTATUS_FETCH, type), + if (quvi->status_func(makelong(QUVISTATUS_FETCH, fetch_type_n), (void *)url) != QUVI_OK) { return (QUVI_ABORTEDBYCALLBACK); } @@ -101,8 +122,11 @@ fetch_to_mem(_quvi_video_t video, else csetopt(CURLOPT_WRITEFUNCTION, quvi_write_callback_default); - if (cookie_header != NULL && *cookie_header != '\0') - csetopt(CURLOPT_COOKIE, cookie_header); + if (arbitrary_cookie != NULL && *arbitrary_cookie != '\0') + csetopt(CURLOPT_COOKIE, arbitrary_cookie); + + if (user_agent != NULL && *user_agent != '\0') + csetopt(CURLOPT_USERAGENT, user_agent); curlcode = curl_easy_perform(quvi->curl); respcode = 0; diff --git a/lib/curl_wrap.h b/lib/curl_wrap.h index 07869fc..b538ddc 100644 --- a/lib/curl_wrap.h +++ b/lib/curl_wrap.h @@ -21,9 +21,7 @@ #define curl_wrap_h QUVIcode -fetch_to_mem(_quvi_video_t video, - const char *url, - const char *cookie_header, const QUVIstatusType type, char **dst); +fetch_to_mem(_quvi_video_t video, const char *url, lua_State * l, char **dst); QUVIcode query_file_length(_quvi_t, llst_node_t); diff --git a/lib/lua_wrap.c b/lib/lua_wrap.c index 44ae98b..3f02f81 100644 --- a/lib/lua_wrap.c +++ b/lib/lua_wrap.c @@ -77,10 +77,10 @@ static _quvi_video_t qv = NULL; static int l_quvi_fetch(lua_State * l) { QUVIstatusType st; - char *data, *url; luaL_Buffer b; _quvi_t quvi; QUVIcode rc; + char *data; quvi = qv->quvi; st = QUVISTATUSTYPE_PAGE; @@ -88,24 +88,7 @@ static int l_quvi_fetch(lua_State * l) if (!lua_isstring(l, 1)) luaL_error(l, "`quvi.fetch' expects `url' argument"); - url = (char *)lua_tostring(l, 1); - - if (lua_isstring(l, 2)) { - - const char *type = lua_tostring(l, 2); - - if (strcmp(type, "config") == 0) - st = QUVISTATUSTYPE_CONFIG; - - else if (strcmp(type, "playlist") == 0) - st = QUVISTATUSTYPE_PLAYLIST; - - lua_remove(l, 2); - } - - lua_remove(l, 1); - - rc = fetch_to_mem(qv, url, lua_tostring(l, 1), st, &data); + rc = fetch_to_mem(qv, lua_tostring(l, 1), l, &data); if (rc == QUVI_OK) { @@ -353,7 +336,8 @@ static void set_key(lua_State * l, const char *key) } \ } while (0) -static char *get_field_s(lua_State * l, _quvi_lua_script_t qls, const char *key) +static char *get_field_req_s(lua_State * l, _quvi_lua_script_t qls, + const char *key) { char *s; set_key(l, key); @@ -363,6 +347,15 @@ static char *get_field_s(lua_State * l, _quvi_lua_script_t qls, const char *key) return (s); } +char *lua_get_field_s(lua_State * l, const char *key) +{ + char *s; + set_key(l, key); + s = (char *)lua_tostring(l, -1); + lua_pop(l, 1); + return (s); +} + static int get_field_b(lua_State * l, _quvi_lua_script_t qls, const char *key) { int b; @@ -614,8 +607,8 @@ QUVIcode run_ident_func(lua_ident_t ident, llst_node_t node) } if (lua_istable(l, -1)) { - ident->domain = strdup(get_field_s(l, qls, "domain")); - ident->formats = strdup(get_field_s(l, qls, "formats")); + ident->domain = strdup(get_field_req_s(l, qls, "domain")); + ident->formats = strdup(get_field_req_s(l, qls, "formats")); rc = get_field_b(l, qls, "handles") ? QUVI_OK : QUVI_NOSUPPORT; } else @@ -665,7 +658,7 @@ run_parse_func(lua_State * l, llst_node_t node, _quvi_video_t video) return (QUVI_LUA); } - setvid(video->redirect, "%s", get_field_s(l, qls, "redirect")); + setvid(video->redirect, "%s", get_field_req_s(l, qls, "redirect")); if (strlen(video->redirect) == 0) { @@ -677,10 +670,10 @@ run_parse_func(lua_State * l, llst_node_t node, _quvi_video_t video) if (rc == QUVI_OK) { - setvid(video->host_id, "%s", get_field_s(l, qls, "host_id")); - setvid(video->title, "%s", get_field_s(l, qls, "title")); - setvid(video->id, "%s", get_field_s(l, qls, "id")); - setvid(video->starttime, "%s", get_field_s(l, qls, "starttime")); + setvid(video->host_id, "%s", get_field_req_s(l, qls, "host_id")); + setvid(video->title, "%s", get_field_req_s(l, qls, "title")); + setvid(video->id, "%s", get_field_req_s(l, qls, "id")); + setvid(video->starttime, "%s", get_field_req_s(l, qls, "starttime")); rc = iter_video_url(l, qls, "url", video); } diff --git a/share/lua/website/README b/share/lua/website/README index b06f6fe..1e6fcda 100644 --- a/share/lua/website/README +++ b/share/lua/website/README @@ -84,18 +84,19 @@ quvi exposes the following C functions to the website scripts. Each of these are in the "quvi" object, e.g. quvi.fetch. -3.1. Function: string quvi.fetch (url, what, arbitrary_cookie) +3.1. Function: string quvi.fetch (url, options) -------------------------------------------------------------- Fetches data from the specified URL. Parameters: * url (string) - URL to fetch - * what (string) - "page", "config" or "playlist" (optional) - * arbitrary_cookie (string) - e.g. "foo=bar" (optional) + * options (table) - Additional options* [optional] -quvi converts the `what' string to a matching QUVIstatusstype (see -quvi/quvi.h). If `what' is nil (undefined), quvi defaults to "page". +options: + * fetch_type (string) ("page"|"config"|"playlist") =page + * arbitrary_cookie (string) e.g. "foo=1;bar=2" + * user_agent (string) e.g. "foo/0.1" arbitrary_cookie value is passed (as it is) to libcurl, see: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTCOOKIE diff --git a/share/lua/website/cbsnews.lua b/share/lua/website/cbsnews.lua index b3a6645..a839dd7 100644 --- a/share/lua/website/cbsnews.lua +++ b/share/lua/website/cbsnews.lua @@ -43,7 +43,7 @@ function parse (video) .. s .. "&iod=videoMedia" - local xml = quvi.fetch(s, 'config') + local xml = quvi.fetch (s, {fetch_type = 'config'}) -- Grab title from the XML. local _,_,s = xml:find ('.-CDATA%[(.-)%]') diff --git a/share/lua/website/clipfish.lua b/share/lua/website/clipfish.lua index 6481113..80447b3 100644 --- a/share/lua/website/clipfish.lua +++ b/share/lua/website/clipfish.lua @@ -47,7 +47,7 @@ function parse (video) string.format("http://www.clipfish.de/video_n.php?p=0|DE&vid=%s", video.id) - local config = quvi.fetch(config_url, "config") + local config = quvi.fetch (config_url, {fetch_type = 'config'}) local _,_,s = config:find("&url=(.-)&") video.url = {s or error ("no match: url")} diff --git a/share/lua/website/collegehumor.lua b/share/lua/website/collegehumor.lua index 30a7ce2..2de2543 100644 --- a/share/lua/website/collegehumor.lua +++ b/share/lua/website/collegehumor.lua @@ -50,7 +50,7 @@ function parse (video) local page = quvi.fetch("http://www.collegehumor.com/moogaloop/video:" .. vid, - 'config') + {fetch_type = 'config'}) local _,_,s = page:find('<file>([%w%p]+)</file>') local default = s or error("no match: default quality URL") diff --git a/share/lua/website/dailymotion.lua b/share/lua/website/dailymotion.lua index 3aa9d21..9908350 100644 --- a/share/lua/website/dailymotion.lua +++ b/share/lua/website/dailymotion.lua @@ -51,7 +51,9 @@ end function parse (video) video.host_id = "dailymotion" video.page_url = normalize (video.page_url) - local page = quvi.fetch(video.page_url, "page", "family_filter=off") + + local opts = { arbitrary_cookie = 'family_filter=off' } + local page = quvi.fetch(video.page_url, opts) if (page:find('SWFObject("http:")') ~= nil) then error ("Looks like a partner video. Refusing to continue.") diff --git a/share/lua/website/golem.lua b/share/lua/website/golem.lua index def48ce..2e7bf0c 100644 --- a/share/lua/website/golem.lua +++ b/share/lua/website/golem.lua @@ -40,10 +40,9 @@ function parse (video) local config_url = string.format("http://video.golem.de/xml/%s", video.id) - local config = quvi.fetch(config_url, "config") - - local _,_,s = config:find("<title>(.-)</") - video.title = s or error ("no match: video title") + local config = quvi.fetch (config_url, {fetch_type = 'config'}) + local _,_,s = config:find("<title>(.-)</") + video.title = s or error ("no match: video title") video_url = string.format("http://video.golem.de/download/%s", video.id) diff --git a/share/lua/website/liveleak.lua b/share/lua/website/liveleak.lua index ce00209..12bd01a 100644 --- a/share/lua/website/liveleak.lua +++ b/share/lua/website/liveleak.lua @@ -42,11 +42,13 @@ function parse (video) local _,_,s = page:find("'config','(.-)'") local config_url = s or error ("no match: config") - local config = quvi.fetch(quvi.unescape(config_url), "config") + local opts = { fetch_type = 'config' } + local config = quvi.fetch (quvi.unescape (config_url), opts) local _,_,s = config:find("<file>(.-)</") local playlist_url = s or error ("no match: playlist") - local playlist = quvi.fetch(playlist_url, "playlist") + opts.fetch_type = 'playlist' + local playlist = quvi.fetch (playlist_url, opts) local _,_,s = playlist:find("<location>(.-)</") video.url = {s or error ("no match: location")} diff --git a/share/lua/website/sevenload.lua b/share/lua/website/sevenload.lua index 5e6059e..cefa85a 100644 --- a/share/lua/website/sevenload.lua +++ b/share/lua/website/sevenload.lua @@ -41,7 +41,7 @@ function parse (video) local config_url = s or error ("no match: config") config_url = quvi.unescape(config_url) - local config = quvi.fetch(config_url, "config") + local config = quvi.fetch (config_url, {fetch_type = 'config'}) local _,_,s = config_url:find("itemId=(%w+)") video.id = s or error ("no match: video id") diff --git a/share/lua/website/spiegel.lua b/share/lua/website/spiegel.lua index e8e90dc..defaac5 100644 --- a/share/lua/website/spiegel.lua +++ b/share/lua/website/spiegel.lua @@ -53,7 +53,7 @@ function parse (video) "http://www1.spiegel.de/active/playlist/fcgi/playlist.fcgi/" .. "asset=flashvideo/mode=id/id=%s", video.id) - local playlist = quvi.fetch(playlist_url, "playlist") + local playlist = quvi.fetch (playlist_url, {fetch_type = 'playlist'}) local _,_,s = playlist:find("<headline>(.-)</") video.title = s or error ("no match: video title") @@ -61,8 +61,7 @@ function parse (video) local config_url = string.format( "http://video.spiegel.de/flash/%s.xml", video.id) - local config = quvi.fetch(config_url, "config") - + local config = quvi.fetch (config_url, {fetch_type = 'config'}) local format = lookup[default] for k,v in pairs (lookup) do diff --git a/share/lua/website/vimeo.lua b/share/lua/website/vimeo.lua index 20e9c11..a64db65 100644 --- a/share/lua/website/vimeo.lua +++ b/share/lua/website/vimeo.lua @@ -43,7 +43,7 @@ function parse (video) local _,_,s = page:find('clip_id=(.-)[&"]') video.id = s or error ("no match: video id") local config_url = "http://vimeo.com/moogaloop/load/clip:" .. video.id - local config = quvi.fetch(config_url, "config") + local config = quvi.fetch (config_url, {fetch_type = 'config'}) local _,_,s = config:find("<caption>(.-)</") video.title = s or error ("no match: video title") diff --git a/share/lua/website/youtube.lua b/share/lua/website/youtube.lua index e07157f..8f6a955 100644 --- a/share/lua/website/youtube.lua +++ b/share/lua/website/youtube.lua @@ -101,7 +101,8 @@ function get_video_info (video, result) .. video.id .. "&el=detailpage&ps=default&eurl=&gl=US&hl=en" - local config = decode (quvi.fetch (config_url, "config")) + local opts = { fetch_type = 'config' } + local config = decode (quvi.fetch (config_url, opts)) if (config['reason']) then local reason = unescape (config['reason']) -- 2.11.4.GIT