Rename and reimplement to use "Media iface"
[www-quvi.git] / Query.cxx
blob4aff252a43ff4813bcbd7149940bff03b0598e82
1 /* WWW::Quvi
2 * Copyright (C) 2010,2011 Toni Gundogdu <legatvs@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
20 #include <string>
21 #include <stdexcept>
22 #include <cassert>
24 #include <curl/curl.h>
26 #include "Quvi.h"
28 Query::Query()
29 : _quvi(NULL),
30 _curl(NULL),
31 quvi_code(QUVI_OK),
32 resp_code(-1)
34 _init();
37 Query::Query(const Query& q)
38 : _quvi(NULL),
39 _curl(NULL),
40 quvi_code(QUVI_OK),
41 resp_code(-1)
43 _init();
46 Query& Query::operator=(const Query& q)
48 if (this != &q)
50 _close ();
51 _init ();
53 return *this;
56 Query::~Query()
58 _close();
61 void Query::_init()
63 quvi_code = quvi_init(&_quvi);
65 if (quvi_code != QUVI_OK)
67 const char *e = quvi_strerror(_quvi, static_cast<QUVIcode>(quvi_code));
68 throw std::runtime_error(e);
71 quvi_getinfo(_quvi, QUVIINFO_CURL, &_curl);
72 assert(_curl != NULL);
75 void Query::_close()
77 if (_quvi)
78 quvi_close(&_quvi);
79 assert(_quvi == NULL);
80 _curl = NULL;
83 Media Query::parse(const std::string& url, const Options& opts)
85 if ( !opts.format.empty() )
86 quvi_setopt(_quvi, QUVIOPT_FORMAT, opts.format.c_str() );
88 quvi_setopt(_quvi, QUVIOPT_NOVERIFY, !opts.verify ? 1L:0L);
89 quvi_setopt(_quvi, QUVIOPT_NORESOLVE, !opts.resolve ? 1L:0L);
91 quvi_setopt(_quvi, QUVIOPT_CATEGORY, opts.category);
93 if ( !opts.user_agent.empty() )
94 curl_easy_setopt(_curl, CURLOPT_USERAGENT, opts.user_agent.c_str());
96 if ( !opts.http_proxy.empty() )
97 curl_easy_setopt(_curl, CURLOPT_PROXY, opts.http_proxy.c_str() );
99 curl_easy_setopt(_curl, CURLOPT_VERBOSE, opts.verbose_libcurl ? 1L:0L);
101 quvi_media_t m;
102 quvi_code = quvi_parse(_quvi, const_cast<char*>(url.c_str()), &m);
104 quvi_getinfo(_quvi, QUVIINFO_HTTPCODE, &resp_code);
106 if (quvi_code != QUVI_OK)
108 const char *e = quvi_strerror(_quvi, static_cast<QUVIcode>(quvi_code));
109 last_error = std::string(e);
110 return Media();
113 return Media(m);
116 int Query::next_website(std::string& domain, std::string& formats)
118 char *d=NULL, *f=NULL;
119 const QUVIcode rc = quvi_next_supported_website(_quvi, &d, &f);
120 if (rc == QUVI_OK)
122 domain = d;
123 formats = f;
124 quvi_free(d);
125 quvi_free(f);
127 return static_cast<int>(rc);
130 int Query::supported(const std::string& url)
132 quvi_code = quvi_supported(_quvi, const_cast<char*>(url.c_str()));
134 if (quvi_code != QUVI_OK)
136 const char *e =
137 quvi_strerror(_quvi, static_cast<QUVIcode>(quvi_code));
139 last_error = std::string(e);
142 return static_cast<int>(quvi_code);
145 // vim: set ts=2 sw=2 tw=72 expandtab: