diff --git a/src/app/Console/Commands/DataCountries.php b/src/app/Console/Commands/DataCountries.php new file mode 100644 index 00000000..3c6bbd60 --- /dev/null +++ b/src/app/Console/Commands/DataCountries.php @@ -0,0 +1,142 @@ +info("Fetching currencies from $currencies_url..."); + + // fetch currency table and create an index by country page url + $page = file_get_contents($currencies_url); + + if (!$page) { + $this->error("Failed to fetch currencies"); + return; + } + + $table_regexp = '!
(.+) | \s*]*>!', $row);
+
+ if (count($cells) == 5) {
+ // actual currency table
+ $currency = preg_match('/([A-Z]{3})/', $cells[0], $m) ? $m[1] : '';
+
+ if (preg_match('/(\d+)/', $cells[1], $m)) {
+ $isocode = $m[1];
+ $currencies[$m[1]] = $currency;
+ }
+
+ preg_match_all('!]+href="(/wiki/[^"]+)"[^>]*>!', $cells[4], $links, PREG_PATTERN_ORDER);
+
+ foreach ($links[1] as $link) {
+ $currencies[strtolower($link)] = $currency;
+ }
+ } elseif (count($cells) == 7) {
+ // replacements table
+ $currency = preg_match('/([A-Z]{3})/', $cells[6], $m) ? $m[1] : '';
+
+ if (preg_match('/(\d+)/', $cells[1], $m)) {
+ $currencies[$m[1]] = $currency;
+ }
+ }
+ }
+ }
+ }
+
+ $namecol = 0;
+ $codecol = 1;
+ $numcol = 3;
+ $lang = 'en';
+
+ $this->info("Fetching countries from $countries_url...");
+
+ $page = file_get_contents($countries_url);
+
+ if (!$page) {
+ $this->error("Failed to fetch countries");
+ return;
+ }
+
+ if (preg_match($table_regexp, $page, $matches)) {
+ preg_match_all('!(.+) | \s* | \s*]*>!', $row); + + if (count($cells) < 5) { + continue; + } + + $regexp = '!]+href="(/wiki/[^"]+)"[^>]*>([^>]+)!i'; + $content = preg_match($regexp, $cells[$namecol], $m) ? $m : null; + + if (preg_match('/>([A-Z]{2})', $cells[$codecol], $m)) { + $code = $m[1]; + } elseif (preg_match('/^([A-Z]{2})/', $cells[$codecol], $m)) { + $code = $m[1]; + } else { + continue; + } + + if ($content) { + $isocode = preg_match('/(\d+)/', $cells[$numcol], $m) ? $m[1] : ''; + list(, $link, $name) = $content; + $countries[$code][$lang] = $name; + + if (!empty($currencies[$isocode])) { + $countries[$code]['currency'] = $currencies[$isocode]; + } elseif (!empty($currencies[strtolower($link)])) { + $countries[$code]['currency'] = $currencies[strtolower($link)]; + } + } + } + } + + $file = resource_path('countries.php'); + + $this->info("Generating resource file $file..."); + + $out = " $names) { + if (!empty($names['en']) && !empty($names['currency'])) { + $out .= sprintf(" '%s' => ['%s','%s'],\n", $code, $names['currency'], addslashes($names['en'])); + } + } + $out .= "];\n"; + + file_put_contents($file, $out); + } +} |