2 * Copyright (C) 2010 Toni Gundogdu.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <curl/curl.h>
27 :_quvi(NULL
), _curl(NULL
), quvi_code (QUVI_OK
), resp_code (-1)
30 Query::Query (const Query
& q
)
31 :_quvi(NULL
), _curl(NULL
), quvi_code (QUVI_OK
), resp_code (-1)
35 Query::operator=(const Query
& q
) {
43 Query::~Query () { _close (); }
48 quvi_code
= quvi_init (&_quvi
);
50 if (quvi_code
!= QUVI_OK
) {
51 const char *e
= quvi_strerror (_quvi
, static_cast<QUVIcode
>(quvi_code
));
52 throw std::runtime_error (e
);
55 quvi_getinfo (_quvi
, QUVIINFO_CURL
, &_curl
);
56 assert (_curl
!= NULL
);
61 if (_quvi
) quvi_close (&_quvi
);
62 assert (_quvi
== NULL
);
67 Query::parse (const std::string
& url
, const Options
& opts
) {
69 if ( !opts
.format
.empty () )
70 quvi_setopt (_quvi
, QUVIOPT_FORMAT
, opts
.format
.c_str () );
72 quvi_setopt (_quvi
, QUVIOPT_NOVERIFY
, !opts
.verify
? 1L:0L);
74 if ( !opts
.user_agent
.empty() )
75 curl_easy_setopt (_curl
, CURLOPT_USERAGENT
, opts
.user_agent
.c_str());
77 if ( !opts
.http_proxy
.empty() )
78 curl_easy_setopt (_curl
, CURLOPT_PROXY
, opts
.http_proxy
.c_str () );
80 curl_easy_setopt (_curl
, CURLOPT_VERBOSE
, opts
.verbose_libcurl
? 1L:0L);
84 quvi_code
= quvi_parse (_quvi
, const_cast<char*>(url
.c_str ()), &qv
);
86 quvi_getinfo (_quvi
, QUVIINFO_HTTPCODE
, &resp_code
);
88 if (quvi_code
!= QUVI_OK
) {
90 const char *e
= quvi_strerror (_quvi
, static_cast<QUVIcode
>(quvi_code
));
92 last_error
= std::string (e
);
101 Query::next_website (std::string
& domain
, std::string
& formats
) {
102 char *dom
=NULL
, *fmt
=NULL
;
103 const QUVIcode rc
= quvi_next_supported_website (_quvi
, &dom
, &fmt
);
110 return static_cast<int>(rc
);