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
24 #include <curl/curl.h>
29 :_quvi(NULL
), _curl(NULL
), quvi_code (QUVI_OK
), resp_code (-1)
34 Query::Query (const Query
& q
)
35 :_quvi(NULL
), _curl(NULL
), quvi_code (QUVI_OK
), resp_code (-1)
41 Query::operator=(const Query
& q
)
59 quvi_code
= quvi_init (&_quvi
);
61 if (quvi_code
!= QUVI_OK
)
63 const char *e
= quvi_strerror (_quvi
, static_cast<QUVIcode
>(quvi_code
));
64 throw std::runtime_error (e
);
67 quvi_getinfo (_quvi
, QUVIINFO_CURL
, &_curl
);
68 assert (_curl
!= NULL
);
74 if (_quvi
) quvi_close (&_quvi
);
75 assert (_quvi
== NULL
);
80 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 quvi_setopt (_quvi
, QUVIOPT_CATEGORY
, opts
.category
);
90 if ( !opts
.user_agent
.empty() )
91 curl_easy_setopt (_curl
, CURLOPT_USERAGENT
, opts
.user_agent
.c_str());
93 if ( !opts
.http_proxy
.empty() )
94 curl_easy_setopt (_curl
, CURLOPT_PROXY
, opts
.http_proxy
.c_str () );
96 curl_easy_setopt (_curl
, CURLOPT_VERBOSE
, opts
.verbose_libcurl
? 1L:0L);
100 quvi_code
= quvi_parse (_quvi
, const_cast<char*>(url
.c_str ()), &qv
);
102 quvi_getinfo (_quvi
, QUVIINFO_HTTPCODE
, &resp_code
);
104 if (quvi_code
!= QUVI_OK
)
106 const char *e
= quvi_strerror (_quvi
, static_cast<QUVIcode
>(quvi_code
));
108 last_error
= std::string (e
);
117 Query::next_website (std::string
& domain
, std::string
& formats
)
119 char *dom
=NULL
, *fmt
=NULL
;
120 const QUVIcode rc
= quvi_next_supported_website (_quvi
, &dom
, &fmt
);
128 return static_cast<int>(rc
);
131 // vim: set ts=2 sw=2 tw=72 expandtab: