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)
32 Query::Query (const Query
& q
)
33 :_quvi(NULL
), _curl(NULL
), quvi_code (QUVI_OK
), resp_code (-1)
39 Query::operator=(const Query
& q
)
58 quvi_code
= quvi_init (&_quvi
);
60 if (quvi_code
!= QUVI_OK
)
62 const char *e
= quvi_strerror (_quvi
, static_cast<QUVIcode
>(quvi_code
));
63 throw std::runtime_error (e
);
66 quvi_getinfo (_quvi
, QUVIINFO_CURL
, &_curl
);
67 assert (_curl
!= NULL
);
73 if (_quvi
) quvi_close (&_quvi
);
74 assert (_quvi
== NULL
);
79 Query::parse (const std::string
& url
, const Options
& opts
)
82 if ( !opts
.format
.empty () )
83 quvi_setopt (_quvi
, QUVIOPT_FORMAT
, opts
.format
.c_str () );
85 quvi_setopt (_quvi
, QUVIOPT_NOVERIFY
, !opts
.verify
? 1L:0L);
86 quvi_setopt (_quvi
, QUVIOPT_NOSHORTENED
, !opts
.shortened
? 1L:0L);
88 if ( !opts
.user_agent
.empty() )
89 curl_easy_setopt (_curl
, CURLOPT_USERAGENT
, opts
.user_agent
.c_str());
91 if ( !opts
.http_proxy
.empty() )
92 curl_easy_setopt (_curl
, CURLOPT_PROXY
, opts
.http_proxy
.c_str () );
94 curl_easy_setopt (_curl
, CURLOPT_VERBOSE
, opts
.verbose_libcurl
? 1L:0L);
98 quvi_code
= quvi_parse (_quvi
, const_cast<char*>(url
.c_str ()), &qv
);
100 quvi_getinfo (_quvi
, QUVIINFO_HTTPCODE
, &resp_code
);
102 if (quvi_code
!= QUVI_OK
)
105 const char *e
= quvi_strerror (_quvi
, static_cast<QUVIcode
>(quvi_code
));
107 last_error
= std::string (e
);
116 Query::next_website (std::string
& domain
, std::string
& formats
)
118 char *dom
=NULL
, *fmt
=NULL
;
119 const QUVIcode rc
= quvi_next_supported_website (_quvi
, &dom
, &fmt
);
127 return static_cast<int>(rc
);