Update gitignore
[www-quvi.git] / Query.cxx
blob0c5560db9662d39e7f13850d0c1d5985ac3b10c1
1 /* WWW::Quvi
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
17 * 02110-1301 USA
20 #include <string>
21 #include <stdexcept>
22 #include <cassert>
24 #include <curl/curl.h>
26 #include "Quvi.h"
28 Query::Query ()
29 :_quvi(NULL), _curl(NULL), quvi_code (QUVI_OK), resp_code (-1)
31 _init ();
34 Query::Query (const Query& q)
35 :_quvi(NULL), _curl(NULL), quvi_code (QUVI_OK), resp_code (-1)
37 _init ();
40 Query&
41 Query::operator=(const Query& q)
43 if (this != &q)
45 _close ();
46 _init ();
48 return *this;
51 Query::~Query ()
53 _close ();
56 void
57 Query::_init ()
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);
71 void
72 Query::_close ()
74 if (_quvi) quvi_close (&_quvi);
75 assert (_quvi == NULL);
76 _curl = NULL;
79 Video
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);
98 quvi_video_t qv;
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);
110 return Video();
113 return Video (qv);
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);
121 if (rc == QUVI_OK)
123 domain = dom;
124 formats = fmt;
125 quvi_free (dom);
126 quvi_free (fmt);
128 return static_cast<int>(rc);
131 // vim: set ts=2 sw=2 tw=72 expandtab: