Add QUVIOPT_NOSHORTENED support
[www-quvi.git] / Query.cxx
blob5763fc8539779f691d2f4884d61723d53564da3d
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)
29 _init ();
32 Query::Query (const Query& q)
33 :_quvi(NULL), _curl(NULL), quvi_code (QUVI_OK), resp_code (-1)
35 _init ();
38 Query&
39 Query::operator=(const Query& q)
41 if (this != &q)
43 _close ();
44 _init ();
46 return *this;
49 Query::~Query ()
51 _close ();
54 void
55 Query::_init ()
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);
70 void
71 Query::_close ()
73 if (_quvi) quvi_close (&_quvi);
74 assert (_quvi == NULL);
75 _curl = NULL;
78 Video
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);
96 quvi_video_t qv;
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);
109 return Video();
112 return Video (qv);
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);
120 if (rc == QUVI_OK)
122 domain = dom;
123 formats = fmt;
124 quvi_free (dom);
125 quvi_free (fmt);
127 return static_cast<int>(rc);