1 /* cclive
2  * Copyright (C) 2010-2013  Toni Gundogdu <legatvs@gmail.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #include <ccinternal>
19 
20 #include <iostream>
21 #include <boost/program_options/variables_map.hpp>
22 
23 #include <ccoptions>
24 #include <ccquvi>
25 #include <cclog>
26 #include <ccfile>
27 #include <ccutil>
28 
29 namespace cc
30 {
31 
32 namespace po = boost::program_options;
33 
get(quvi::media & media,void * curl)34 void get(quvi::media& media, void *curl)
35 {
36   const po::variables_map map = cc::opts.map();
37 
38   const int max_retries  = map["max-retries"].as<int>();
39   const int retry_wait   = map["retry-wait"].as<int>();
40 
41   const bool no_download = opts.flags.no_download;
42   const bool exec        = map.count("exec");
43 
44   int retry = 0;
45 
46   while (retry <= max_retries)
47     {
48       cc::file file(media);
49 
50       if (file.nothing_todo())
51         {
52           if (exec)
53             cc::exec(file);
54 #define E_NOTHING_TODO "media retrieved completely already"
55           throw std::runtime_error(E_NOTHING_TODO);
56 #undef E_NOTHING_TODO
57         }
58 
59       // Download media.
60 
61       if (retry > 0)
62         {
63           cc::log
64               << "Retrying "
65               << retry
66               << " of "
67               << max_retries
68               << " ... "
69               << std::flush;
70 
71           cc::wait(retry_wait);
72         }
73 
74       cc::log << file.to_s(media) << std::endl;
75       ++retry;
76 
77       if (!no_download)
78         {
79           if (!file.write(media, curl))
80             continue; // Retry.
81 
82           if (exec)
83             cc::exec(file);
84         }
85 
86       break; // Stop retrying.
87     }
88 }
89 
90 } // namespace cc
91 
92 // vim: set ts=2 sw=2 tw=72 expandtab:
93