From 22d3779d8220ee9740f00900c86b969f2315f146 Mon Sep 17 00:00:00 2001 From: Toni Gundogdu Date: Thu, 28 Jul 2011 00:33:38 +0300 Subject: [PATCH] Make "ok" flag a member of Query instead of Media Instead of returning $rc with * Query::parse * Query::formats * Query::next_website Use the Query::ok flag for checking errors. --- Media.cxx | 4 +-- Query.cxx | 71 ++++++++++++++++++++++++++++++----------------- examples/get_media.pl | 4 +-- examples/query_formats.pl | 10 ++----- examples/query_media.pl | 5 +--- examples/websites.pl | 21 ++++++++------ i/Quvi.h | 7 +++-- i/Quvi.i | 6 ++-- t/02_Media_defaults.t | 1 - t/04_Query_defaults.t | 5 ++-- t/query.t | 8 ++---- t/support.t | 10 +++---- 12 files changed, 80 insertions(+), 72 deletions(-) diff --git a/Media.cxx b/Media.cxx index c44ccaa..2ccce13 100644 --- a/Media.cxx +++ b/Media.cxx @@ -22,7 +22,7 @@ #include "Quvi.h" #define _init \ - : content_length(-1), duration(-1), ok(0) + : content_length(-1), duration(-1) Media::Media() _init @@ -47,7 +47,6 @@ _init _wrap(QUVIPROP_MEDIADURATION, duration, double); #undef _wrap quvi_parse_close(&m); - ok = 1; } Media::Media(const Media& v) @@ -81,7 +80,6 @@ void Media::_swap(const Media& m) url = m.url; thumbnail_url = m.thumbnail_url; duration = m.duration; - ok = m.ok; } // vim: set ts=2 sw=2 tw=72 expandtab: diff --git a/Query.cxx b/Query.cxx index cb77f61..05b2adf 100644 --- a/Query.cxx +++ b/Query.cxx @@ -17,32 +17,35 @@ * 02110-1301 USA */ -#include #include #include +#include #include #include "Quvi.h" +#define _init_m \ + : _quvi(NULL), \ + _curl(NULL), \ + quvi_code(QUVI_OK), \ + resp_code(-1), \ + ok(1) + Query::Query() - : _quvi(NULL), - _curl(NULL), - quvi_code(QUVI_OK), - resp_code(-1) +_init_m { _init(); } Query::Query(const Query& q) - : _quvi(NULL), - _curl(NULL), - quvi_code(QUVI_OK), - resp_code(-1) +_init_m { _init(); } +#undef _init_m + Query& Query::operator=(const Query& q) { if (this != &q) @@ -70,6 +73,8 @@ void Query::_init() quvi_getinfo(_quvi, QUVIINFO_CURL, &_curl); assert(_curl != NULL); + + set_opts(_opts); } void Query::_close() @@ -94,7 +99,7 @@ void Query::set_opts(const Options& opts) curl_easy_setopt(_curl, CURLOPT_USERAGENT, opts.user_agent.c_str()); if ( !opts.http_proxy.empty() ) - curl_easy_setopt(_curl, CURLOPT_PROXY, opts.http_proxy.c_str() ); + curl_easy_setopt(_curl, CURLOPT_PROXY, opts.http_proxy.c_str()); curl_easy_setopt(_curl, CURLOPT_VERBOSE, opts.verbose_libcurl ? 1L:0L); } @@ -106,33 +111,42 @@ Media Query::parse(const std::string& url) quvi_getinfo(_quvi, QUVIINFO_HTTPCODE, &resp_code); + Media res; + if (quvi_code != QUVI_OK) + _format_error(); + else { - _format_error(); - return Media(); + res = Media(m); + ok = 1; } - return Media(m); + return res; } -int Query::next_website(std::string& domain, std::string& formats) +void Query::next_website(std::string& domain, + std::string& formats) { char *d=NULL, *f=NULL; - const QUVIcode rc = quvi_next_supported_website(_quvi, &d, &f); - if (rc == QUVI_OK) + quvi_code = quvi_next_supported_website(_quvi, &d, &f); + + if (quvi_code == QUVI_OK) { - domain = d; - formats = f; + domain = d; quvi_free(d); + formats = f; quvi_free(f); + ok = 1; } - return static_cast(rc); + else + ok = 0; } void Query::_format_error() { const char *e = quvi_strerror(_quvi, static_cast(quvi_code)); errmsg = std::string(e); + ok = 0; } int Query::supported(const std::string& url) @@ -145,22 +159,27 @@ int Query::supported(const std::string& url) return static_cast(quvi_code); } -int Query::formats(const std::string& url, std::string& dst) +std::string Query::formats(const std::string& url) { - char *s; + char *s = NULL; quvi_code = quvi_query_formats(_quvi, const_cast(url.c_str()), &s); - if (quvi_code == QUVI_OK) + quvi_getinfo(_quvi, QUVIINFO_HTTPCODE, &resp_code); + + std::string res; + + if (quvi_code != QUVI_OK) + _format_error(); + else { - dst = s; + res = s; quvi_free(s); + ok = 1; } - else - _format_error(); - return static_cast(quvi_code); + return res; } // vim: set ts=2 sw=2 tw=72 expandtab: diff --git a/examples/get_media.pl b/examples/get_media.pl index 6b51a6e..06ee222 100644 --- a/examples/get_media.pl +++ b/examples/get_media.pl @@ -45,9 +45,7 @@ my $q = new WWW::Quvi::Query; $q->set_opts($o); my $m = $q->parse(URL); - -croak "error: $q->{last_error}\n" - unless $m->{ok}; +croak "error: $q->{errmsg}\n" unless $q->{ok}; # Save to file. diff --git a/examples/query_formats.pl b/examples/query_formats.pl index 4d66454..8af7f48 100644 --- a/examples/query_formats.pl +++ b/examples/query_formats.pl @@ -32,16 +32,10 @@ use constant URL => my $q = new WWW::Quvi::Query; -# Set options. - -$q->set_opts(new WWW::Quvi::Options); - # Query available formats to URL. -my ($rc, $formats) = $q->formats(URL); - -croak "error: $q->{last_error}\n" - unless $rc == WWW::Quvi::OK; +my $formats = $q->formats(URL); +croak "error: $q->{errmsg}\n" unless $q->{ok}; #print "$_\n" foreach split /\|/, $formats; printf "%20s : %s\n", $formats, URL; diff --git a/examples/query_media.pl b/examples/query_media.pl index d1af318..a6ad765 100644 --- a/examples/query_media.pl +++ b/examples/query_media.pl @@ -31,14 +31,11 @@ use constant URL => "http://www.dailymotion.com/video/xdpig1_city-of-scars_shortfilms"; my $q = new WWW::Quvi::Query; -$q->set_opts(new WWW::Quvi::Options); # Query media. my $m = $q->parse(URL); - -croak "error: $q->{last_error}\n" - unless $m->{ok}; +croak "error: $q->{errmsg}\n" unless $q->{ok}; printf "%10s : %s : %s diff --git a/examples/websites.pl b/examples/websites.pl index 0c2421a..37808e5 100644 --- a/examples/websites.pl +++ b/examples/websites.pl @@ -28,15 +28,20 @@ use Carp qw(croak); use WWW::Quvi; my $q = new WWW::Quvi::Query; -$q->set_opts(new WWW::Quvi::Options); +=for comment +my $o = new WWW::Quvi::Options; +$o->{category} = WWW::Quvi::ProtoRtmp; +$q->set_opts($o); +=cut -while (1) { - # fmts here has no real use as of libquvi 0.2.17. If you need to know - # the available formats to an URL, use the Query::formats instead, see - # the query_formats.pl for an example. - my ($done, $domain, $fmts) = $q->next_website; - last if $done; - printf "%32s : %s\n", $domain, $fmts; +# The returned formats (f) has no real use since libquvi 0.2.17. +# Query::next_website function is suitable for only listing the +# supported websites. If you want to know which formats are +# available to an URL use the Query::formats instead. + +while ($q->{ok}) { + my ($d,$f) = $q->next_website; + printf "%32s : %s\n", $d, $f if $q->{ok}; } # vim: set ts=2 sw=2 tw=72 expandtab: diff --git a/i/Quvi.h b/i/Quvi.h index e14da13..0c1fed3 100644 --- a/i/Quvi.h +++ b/i/Quvi.h @@ -67,7 +67,6 @@ public: std::string url; double duration; std::string id; - int ok; }; class Query @@ -78,8 +77,8 @@ public: Query& operator=(const Query&); virtual ~Query(); public: - int formats(const std::string&, std::string&); - int next_website(std::string&, std::string&); + void next_website(std::string&, std::string&); + std::string formats(const std::string&); int supported(const std::string&); Media parse(const std::string&); void set_opts(const Options&); @@ -88,12 +87,14 @@ private: void _close(); void _init(); private: + Options _opts; quvi_t _quvi; void *_curl; public: std::string errmsg; long quvi_code; long resp_code; + int ok; }; #endif diff --git a/i/Quvi.i b/i/Quvi.i index 5069c19..19502e7 100644 --- a/i/Quvi.i +++ b/i/Quvi.i @@ -109,7 +109,6 @@ public: std::string url; double duration; std::string id; - int ok; }; /* Query::_init throws std::runtime_error if quvi_init fails. */ @@ -127,8 +126,8 @@ public: Query(); virtual ~Query(); public: - int next_website(std::string &domain, std::string &formats); - int formats(const std::string& url, std::string &formats); + void next_website(std::string &domain, std::string &formats); + std::string formats(const std::string&); int supported(const std::string&); Media parse(const std::string&); void set_opts(const Options&); @@ -137,6 +136,7 @@ public: std::string errmsg; long quvi_code; long resp_code; + int ok; }; %exception; diff --git a/t/02_Media_defaults.t b/t/02_Media_defaults.t index 8b4e4e3..8bc77d4 100644 --- a/t/02_Media_defaults.t +++ b/t/02_Media_defaults.t @@ -30,7 +30,6 @@ my %h = ( page_title => '', host => '', id => '', - ok => 0, content_length=> -1, content_type => '', file_suffix => '', diff --git a/t/04_Query_defaults.t b/t/04_Query_defaults.t index 2bb5201..0c84418 100644 --- a/t/04_Query_defaults.t +++ b/t/04_Query_defaults.t @@ -25,9 +25,10 @@ use Test::More; use WWW::Quvi; my %h = ( - errmsg => '', + errmsg => '', quvi_code => 0, - resp_code => -1 + resp_code => -1, + ok => 1 ); plan tests => keys(%h) + 1; diff --git a/t/query.t b/t/query.t index d8c0ae6..78b140f 100644 --- a/t/query.t +++ b/t/query.t @@ -31,19 +31,17 @@ $ENV{HAVE_INTERNET} use constant URL => "http://www.dailymotion.com/video/xdpig1_city-of-scars_shortfilms"; -my $o = new WWW::Quvi::Options; my $q = new WWW::Quvi::Query; -$q->set_opts($o); # Formats. -my ($rc, $fmts) = $q->formats(URL); -is($rc, WWW::Quvi::OK) or diag $q->{last_error}; +my $fmts = $q->formats(URL); +is($q->{ok}, 1) or diag $q->{errmsg}; #diag $fmts; # Media. my $m = $q->parse(URL); -is($m->{ok}, 1) or diag $q->{last_error}; +is($q->{ok}, 1) or diag $q->{errmsg}; # vim: set ts=2 sw=2 tw=72 expandtab: diff --git a/t/support.t b/t/support.t index bd8705f..564d225 100644 --- a/t/support.t +++ b/t/support.t @@ -25,15 +25,13 @@ use Test::More tests => 3; use WWW::Quvi; my $q = new WWW::Quvi::Query; -$q->set_opts(new WWW::Quvi::Options); # Next (supported) website. -my $n=0; -while (1) { - my ($done) = $q->next_website; - last if $done; - ++$n; +my $n = 0; +while ($q->{ok}) { + my ($d) = $q->next_website; + ++$n if $q->{ok}; } isnt($n, 0); #diag $n; -- 2.11.4.GIT