examples/ Apply style changes
[www-quvi.git] / Query.cxx
blob05b2adf77d92cc77bb7a066abbf40b6b79d4e39e
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 <stdexcept>
21 #include <cassert>
22 #include <string>
24 #include <curl/curl.h>
26 #include "Quvi.h"
28 #define _init_m \
29 : _quvi(NULL), \
30 _curl(NULL), \
31 quvi_code(QUVI_OK), \
32 resp_code(-1), \
33 ok(1)
35 Query::Query()
36 _init_m
38 _init();
41 Query::Query(const Query& q)
42 _init_m
44 _init();
47 #undef _init_m
49 Query& Query::operator=(const Query& q)
51 if (this != &q)
53 _close ();
54 _init ();
56 return *this;
59 Query::~Query()
61 _close();
64 void Query::_init()
66 quvi_code = quvi_init(&_quvi);
68 if (quvi_code != QUVI_OK)
70 const char *e = quvi_strerror(_quvi, static_cast<QUVIcode>(quvi_code));
71 throw std::runtime_error(e);
74 quvi_getinfo(_quvi, QUVIINFO_CURL, &_curl);
75 assert(_curl != NULL);
77 set_opts(_opts);
80 void Query::_close()
82 if (_quvi)
83 quvi_close(&_quvi);
84 assert(_quvi == NULL);
85 _curl = NULL;
88 void Query::set_opts(const Options& opts)
90 if ( !opts.format.empty() )
91 quvi_setopt(_quvi, QUVIOPT_FORMAT, opts.format.c_str() );
93 quvi_setopt(_quvi, QUVIOPT_NOVERIFY, !opts.verify ? 1L:0L);
94 quvi_setopt(_quvi, QUVIOPT_NORESOLVE, !opts.resolve ? 1L:0L);
96 quvi_setopt(_quvi, QUVIOPT_CATEGORY, opts.category);
98 if ( !opts.user_agent.empty() )
99 curl_easy_setopt(_curl, CURLOPT_USERAGENT, opts.user_agent.c_str());
101 if ( !opts.http_proxy.empty() )
102 curl_easy_setopt(_curl, CURLOPT_PROXY, opts.http_proxy.c_str());
104 curl_easy_setopt(_curl, CURLOPT_VERBOSE, opts.verbose_libcurl ? 1L:0L);
107 Media Query::parse(const std::string& url)
109 quvi_media_t m;
110 quvi_code = quvi_parse(_quvi, const_cast<char*>(url.c_str()), &m);
112 quvi_getinfo(_quvi, QUVIINFO_HTTPCODE, &resp_code);
114 Media res;
116 if (quvi_code != QUVI_OK)
117 _format_error();
118 else
120 res = Media(m);
121 ok = 1;
124 return res;
127 void Query::next_website(std::string& domain,
128 std::string& formats)
130 char *d=NULL, *f=NULL;
131 quvi_code = quvi_next_supported_website(_quvi, &d, &f);
133 if (quvi_code == QUVI_OK)
135 domain = d;
136 quvi_free(d);
137 formats = f;
138 quvi_free(f);
139 ok = 1;
141 else
142 ok = 0;
145 void Query::_format_error()
147 const char *e = quvi_strerror(_quvi, static_cast<QUVIcode>(quvi_code));
148 errmsg = std::string(e);
149 ok = 0;
152 int Query::supported(const std::string& url)
154 quvi_code = quvi_supported(_quvi, const_cast<char*>(url.c_str()));
156 if (quvi_code != QUVI_OK)
157 _format_error();
159 return static_cast<int>(quvi_code);
162 std::string Query::formats(const std::string& url)
164 char *s = NULL;
166 quvi_code =
167 quvi_query_formats(_quvi, const_cast<char*>(url.c_str()), &s);
169 quvi_getinfo(_quvi, QUVIINFO_HTTPCODE, &resp_code);
171 std::string res;
173 if (quvi_code != QUVI_OK)
174 _format_error();
175 else
177 res = s;
178 quvi_free(s);
179 ok = 1;
182 return res;
185 // vim: set ts=2 sw=2 tw=72 expandtab: