add PREREQ_PM, t/WWW-Quvi-Pod.t to Changes
[www-quvi.git] / Query.cxx
blobbc2298983719fd8f74119c1e3e1c63384b29b21d
1 /*
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/>.
18 #include <string>
19 #include <stdexcept>
20 #include <cassert>
22 #include <curl/curl.h>
24 #include "Quvi.h"
26 Query::Query ()
27 :_quvi(NULL), _curl(NULL), quvi_code (QUVI_OK), resp_code (-1)
28 { _init (); }
30 Query::Query (const Query& q)
31 :_quvi(NULL), _curl(NULL), quvi_code (QUVI_OK), resp_code (-1)
32 { _init (); }
34 Query&
35 Query::operator=(const Query& q) {
36 if (this != &q) {
37 _close ();
38 _init ();
40 return *this;
43 Query::~Query () { _close (); }
45 void
46 Query::_init () {
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);
59 void
60 Query::_close () {
61 if (_quvi) quvi_close (&_quvi);
62 assert (_quvi == NULL);
63 _curl = NULL;
66 Video
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);
82 quvi_video_t qv;
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);
94 return Video();
97 return Video (qv);
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);
104 if (rc == QUVI_OK) {
105 domain = dom;
106 formats = fmt;
107 quvi_free (dom);
108 quvi_free (fmt);
110 return static_cast<int>(rc);