Rename tests
[www-quvi.git] / Query.cxx
blob24d8804a0711d6ee44d456f7126ab8c7289ad449
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 void Query::set_opts(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);
102 Media Query::parse(const std::string& url)
104 quvi_media_t m;
105 quvi_code = quvi_parse(_quvi, const_cast<char*>(url.c_str()), &m);
107 quvi_getinfo(_quvi, QUVIINFO_HTTPCODE, &resp_code);
109 if (quvi_code != QUVI_OK)
111 _format_error();
112 return Media();
115 return Media(m);
118 int Query::next_website(std::string& domain, std::string& formats)
120 char *d=NULL, *f=NULL;
121 const QUVIcode rc = quvi_next_supported_website(_quvi, &d, &f);
122 if (rc == QUVI_OK)
124 domain = d;
125 formats = f;
126 quvi_free(d);
127 quvi_free(f);
129 return static_cast<int>(rc);
132 void Query::_format_error()
134 const char *e = quvi_strerror(_quvi, static_cast<QUVIcode>(quvi_code));
135 last_error = std::string(e);
138 int Query::supported(const std::string& url)
140 quvi_code = quvi_supported(_quvi, const_cast<char*>(url.c_str()));
142 if (quvi_code != QUVI_OK)
143 _format_error();
145 return static_cast<int>(quvi_code);
148 int Query::formats(const std::string& url, std::string& dst)
150 char *s;
152 quvi_code =
153 quvi_query_formats(_quvi, const_cast<char*>(url.c_str()), &s);
155 if (quvi_code == QUVI_OK)
157 dst = s;
158 quvi_free(s);
160 else
161 _format_error();
163 return static_cast<int>(quvi_code);
166 // vim: set ts=2 sw=2 tw=72 expandtab: