1
|
|
|
<?php |
2
|
|
|
namespace Fmaj\LaposteDatanovaBundle\Command; |
3
|
|
|
|
4
|
|
|
use Fmaj\LaposteDatanovaBundle\Service\Downloader; |
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Symfony 2.3+ DownloadDatasetCommand |
13
|
|
|
* |
14
|
|
|
* @author Florian Ajir <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class DownloadDatasetCommand extends ContainerAwareCommand |
17
|
|
|
{ |
18
|
|
|
/** @var Downloader $downloader */ |
19
|
|
|
private $downloader; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Command configuration |
23
|
|
|
*/ |
24
|
|
|
protected function configure() |
25
|
|
|
{ |
26
|
|
|
$this |
27
|
|
|
->setName('datanova:download:dataset') |
28
|
|
|
->setDescription('Download dataset records to use it locally') |
29
|
|
|
->addArgument( |
30
|
|
|
'dataset', |
31
|
|
|
InputArgument::REQUIRED, |
32
|
|
|
'Which dataset to download?' |
33
|
|
|
) |
34
|
|
|
->addArgument( |
35
|
|
|
'format', |
36
|
|
|
InputArgument::OPTIONAL, |
37
|
|
|
'Data file format : CSV (default), JSON', |
38
|
|
|
'CSV' |
39
|
|
|
) |
40
|
|
|
->addArgument( |
41
|
|
|
'q', |
42
|
|
|
InputArgument::OPTIONAL, |
43
|
|
|
'query filter, by default all results will be download' |
44
|
|
|
) |
45
|
|
|
->addOption( |
46
|
|
|
'force-replace', |
47
|
|
|
'f', |
48
|
|
|
InputOption::VALUE_NONE, |
49
|
|
|
'If set, the command will replace local storage' |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
57
|
|
|
{ |
58
|
|
|
$this->downloader = $this->getContainer()->get('data_nova.service.downloader'); |
59
|
|
|
$dataset = $input->getArgument('dataset'); |
60
|
|
|
$format = strtolower($input->getArgument('format')); |
61
|
|
|
$query = $input->getArgument('q'); |
62
|
|
|
$download = $this->downloader->download( |
63
|
|
|
$dataset, |
64
|
|
|
$format, |
65
|
|
|
$input->getArgument('q'), |
66
|
|
|
$input->getOption('force-replace') |
67
|
|
|
); |
68
|
|
|
$filepath = $this->downloader->findDownload($dataset, $format, $query); |
69
|
|
|
if ($download) { |
70
|
|
|
$output->writeln(sprintf( |
71
|
|
|
'Dataset %s downloaded to "%s" : %d bytes', |
72
|
|
|
$dataset, |
73
|
|
|
$filepath, |
74
|
|
|
filesize($filepath) |
75
|
|
|
)); |
76
|
|
|
} else { |
77
|
|
|
if (false !== $filepath) { |
78
|
|
|
if (false === $input->getOption('force-replace')) { |
79
|
|
|
$output->writeln('Existing dataset. To overwrite it, try with --force-replace option'); |
80
|
|
|
} else { |
81
|
|
|
$output->writeln('Error during update of existing dataset.'); |
82
|
|
|
} |
83
|
|
|
} else { |
84
|
|
|
$output->writeln('Error during dataset download.'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|