-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconsole.php
238 lines (212 loc) · 9.01 KB
/
console.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
$console = new Application('VG2021', '0.1');
// ----------------------- GEO -----------------------
// ---------------------------------------------------
$console
->register('geo:dl')
->setDescription('Downloads archives from geovoile')
->addArgument('race', InputArgument::REQUIRED, 'Race id')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$app->setRace($input->getArgument('race'));
$app['srv.geovoile']->download();
})
;
$console
->register('geo:import')
->setDescription('Downloads archives from geovoile')
->addArgument('race', InputArgument::REQUIRED, 'Race id')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$app->setRace($input->getArgument('race'));
$app['srv.geovoile']->parse();
})
;
$console
->register('regadata:dl')
->setDescription('Downloads the xls files from transat-bretagnemartinique.com')
->addArgument('race', InputArgument::REQUIRED, 'Race id')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$app->setRace($input->getArgument('race'));
foreach ($app['srv.xls']->listMissingXlsx() as $f) {
$output->writeln('<info>Downloading ' . strtr($app['race']['url_xls'], ['%file%' => $f]) . '</info>');
file_put_contents(
$app['srv.xls']->xlsDir . '/' . $f,
file_get_contents(
strtr($app['race']['url_xls'], ['%file%' => $f])
)
);
}
$output->writeln('<info>Done</info>');
})
;
$console
->register('regadata:vor:dl')
->setDescription('Downloads the json file from volvooceanrace.com')
->addArgument('race', InputArgument::REQUIRED, 'Race id')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$app->setRace($input->getArgument('race'));
$app['srv.json']->downloadAndParse($input->getArgument('race'));
$output->writeln('<info>Done</info>');
})
;
$console
->register('regadata:convert')
->setDescription('Exports xls files to mongo')
->addOption('file', null, InputOption::VALUE_OPTIONAL, 'To import a specific file')
->addOption('force', null, InputOption::VALUE_NONE, 'Force conversion (in case document already exists)')
->addArgument('race', InputArgument::REQUIRED, 'Race id')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$app->setRace($input->getArgument('race'));
$app['srv.xls']->xls2mongo(
$input->getOption('file'),
$input->getOption('force')
);
})
;
$console
->register('regadata:export')
->setDescription('Exports to kml + json')
->addOption('file', null, InputOption::VALUE_OPTIONAL, 'To import a specific file')
->addOption('force', null, InputOption::VALUE_NONE, 'Force conversion (in case document already exists)')
->addArgument('race', InputArgument::REQUIRED, 'Race id')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$app->setRace($input->getArgument('race'));
$app['srv.xls']->mongo2json(
$input->getOption('force'), 'timestamp'
);
})
;
$console
->register('regadata:ping_sitemap')
->addArgument('race', InputArgument::REQUIRED, 'Race id')
->addOption('debug', null, InputOption::VALUE_NONE, 'If set, it will NOT send the tweets')
->setDescription('Generates sitemaps + Ping sitemap to different search engines!')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$app->setRace($input->getArgument('race'));
$cmd = 'wget -q -O /dev/null "' . $app['config']['schema'] . $app['race']['host'] . '/gensitemap"';
$output->writeln('<info>' . $cmd . '</info>');
system($cmd);
$xmls = glob(__DIR__ . '/../web/xml/' . $app['race']['id'] . '*');
foreach ($xmls as $xml) {
if (basename($xml) === $app['race']['id'] . $app['config']['smFileName']) {
continue;
}
$url = $app['config']['schema'] . $app['race']['host'] . $app['config']['smDir'] . '/' . basename($xml);
$app['misc']::sitemapPing($url, $input->getOption('debug'));
}
})
;
// ---------------------------------------------------
// ----------------------- VG -----------------------
$console
->register('vg:sails2mongo')
->setDescription('exports sails CSV to mongo')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$app->setRace('vg2012');
// $s = $app['mongo']->regatta->sails;
$sails = file(__DIR__ . '/init/sails_vg2012.csv', FILE_IGNORE_NEW_LINES);
$header = [];
foreach ($sails as $sail) {
if (empty($header)) {
$header = explode(',', $sail);
continue;
}
$_sail = [];
$_sail = explode(',', $sail);
$app['repo.sail']->insert(array_combine($header, $_sail));
}
$output->writeln('ok');
})
;
$console
->register('vg:rotate_icons')
->setDescription('Creates icons rotated from 0 to 360º')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$color = new Imagine\Image\Color('000', 100);
for ($i = 0; $i <= 360; ++$i) {
$output->writeln('<info>Generating web/icons/boat_' . $i . '.png</info>');
$image = $app['imagine']
->open(__DIR__ . '/../web/img/boat_marker.png')
->rotate($i, $color)
->save(__DIR__ . '/../web/icons/boat_' . $i . '.png')
;
}
})
;
$console
->register('vg:tweet')
->addArgument('race', InputArgument::REQUIRED, 'Race id')
->addOption('debug', null, InputOption::VALUE_NONE, 'If set, it will NOT send the tweets')
->setDescription('Gets the latest report and tweet about it')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$app->setRace($input->getArgument('race'));
// ---- get last report + max distance boat
$report = $app['repo.report']->getLast();
if (false !== $app['race']['modes']) {
foreach ($report as $r) {
$tmp[ $r['class'] ][ (string) $r['_id'] ] = $r;
}
foreach ($tmp as $class => $report) {
$max = $app['repo.report']->extractMaxByKey($report, '24hour_distance');
tweetIt($app, $input, $output, $max);
}
// $report = $tmp;
} else {
$max = $app['repo.report']->extractMaxByKey($report, '24hour_distance');
tweetIt($app, $input, $output, $max);
}
})
;
function tweetIt($app, $input, $output, $max)
{
if ($max['24hour_distance'] <= 0) {
return false;
}
$tweet = 'Latest ranking available, fastest boat in the last 24h %skipper% (%miles% nm) %url% %hashtag%';
$params = [
'%hashtag%' => '#' . $app['race']['hashtag'] . (false === $app['race']['modes'] ? '' : ' #' . strtoupper($max['class'])),
'%skipper%' => $app['misc']->getTwitter($max['sail']),
'%miles%' => $max['24hour_distance'],
];
// ---- translate tweet to french + tweet
if (false === $app['race']['modes']) {
$params['%url%'] = $app['race']['tweetUrlFr'];
} else {
$params['%url%'] = $app['race']['tweetUrlFr'][ $max['class'] ];
}
$_tweet = $app['translator']->trans($tweet, $params, 'messages', 'fr');
if (isset($app['race']['showTwailorHashtag']) && true === $app['race']['showTwailorHashtag'] && strlen($_tweet) <= 131) {
$_tweet .= ' #twailor';
}
$output->writeln('<info>' . $_tweet . ' (' . strlen($_tweet) . ')</info>');
if (!$input->getOption('debug')) {
if (strlen($_tweet) <= 140) {
$code = $app['tmhoauth']->request('POST', $app['tmhoauth']->url('1.1/statuses/update'), [
'status' => $_tweet,
]);
}
}
// ---- translate tweet to english + tweet
if (false === $app['race']['modes']) {
$params['%url%'] = $app['race']['tweetUrlEn'];
} else {
$params['%url%'] = $app['race']['tweetUrlEn'][ $max['class'] ];
}
$_tweet = $app['translator']->trans($tweet, $params, 'en');
if (isset($app['race']['showTwailorHashtag']) && true === $app['race']['showTwailorHashtag'] && strlen($_tweet) <= 131) {
$_tweet .= ' #twailor';
}
$output->writeln('<info>' . $_tweet . ' (' . strlen($_tweet) . ')</info>');
if (!$input->getOption('debug')) {
if (strlen($_tweet) <= 140) {
$code = $app['tmhoauth']->request('POST', $app['tmhoauth']->url('1.1/statuses/update'), [
'status' => $_tweet,
]);
}
}
}
return $console;