ImportCities.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Place;
  5. use DB;
  6. use Illuminate\Support\Str;
  7. class ImportCities extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'import:cities {chunk=1000}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Import Cities to database';
  21. /**
  22. * Checksum of city dataset.
  23. *
  24. */
  25. const CHECKSUM = 'e203c0247538788b2a91166c7cf4b95f58291d998f514e9306d315aa72b09e48bfd3ddf310bf737afc4eefadca9083b8ff796c67796c6bd8e882a3d268bd16af';
  26. /**
  27. * List of shortened countries.
  28. *
  29. * @var array
  30. */
  31. protected $countries = [
  32. 'AE' => 'UAE',
  33. 'BA' => 'Bosnia-Herzegovina',
  34. 'BO' => 'Bolivia',
  35. 'CD' => 'Democratic Republic of Congo',
  36. 'CG' => 'Republic of Congo',
  37. 'FM' => 'Micronesia',
  38. 'GB' => 'United Kingdom',
  39. 'IR' => 'Iran',
  40. 'KP' => 'DRPK',
  41. 'KR' => 'South Korea',
  42. 'LA' => 'Laos',
  43. 'MD' => 'Moldova',
  44. 'PS' => 'Palestine',
  45. 'RU' => 'Russia',
  46. 'SH' => 'Saint Helena',
  47. 'SY' => 'Syria',
  48. 'TW' => 'Taiwan',
  49. 'TZ' => 'Tanzania',
  50. 'US' => 'USA',
  51. 'VE' => 'Venezuela',
  52. 'XK' => 'Kosovo'
  53. ];
  54. /**
  55. * Create a new command instance.
  56. *
  57. * @return void
  58. */
  59. public function __construct()
  60. {
  61. parent::__construct();
  62. }
  63. /**
  64. * Execute the console command.
  65. *
  66. * @return mixed
  67. */
  68. public function handle()
  69. {
  70. $path = storage_path('app/cities.json');
  71. if(hash_file('sha512', $path) !== self::CHECKSUM) {
  72. $this->error('Invalid or corrupt storage/app/cities.json data.');
  73. $this->line('');
  74. $this->info('Run the following command to fix:');
  75. $this->info('git checkout storage/app/cities.json');
  76. return;
  77. }
  78. if (!is_file($path)) {
  79. $this->error('Missing storage/app/cities.json file!');
  80. return;
  81. }
  82. if (Place::count() > 0) {
  83. DB::table('places')->truncate();
  84. }
  85. $this->info('Importing city data into database ...');
  86. $cities = json_decode(file_get_contents($path));
  87. $cityCount = count($cities);
  88. $this->line('');
  89. $this->info("Found {$cityCount} cities to insert ...");
  90. $this->line('');
  91. $bar = $this->output->createProgressBar($cityCount);
  92. $bar->start();
  93. $buffer = [];
  94. $count = 0;
  95. foreach ($cities as $city) {
  96. $buffer[] = [
  97. "name" => $city->name,
  98. "slug" => Str::slug($city->name),
  99. "country" => $this->codeToCountry($city->country),
  100. "lat" => $city->lat,
  101. "long" => $city->lng
  102. ];
  103. $count++;
  104. if ($count % $this->argument('chunk') == 0) {
  105. $this->insertBuffer($buffer);
  106. $bar->advance(count($buffer));
  107. $buffer = [];
  108. }
  109. }
  110. $this->insertBuffer($buffer);
  111. $bar->advance(count($buffer));
  112. $bar->finish();
  113. $this->line('');
  114. $this->line('');
  115. $this->info('Successfully imported ' . $cityCount . ' entries!');
  116. $this->line('');
  117. return;
  118. }
  119. private function insertBuffer($buffer)
  120. {
  121. DB::table('places')->insert($buffer);
  122. }
  123. private function codeToCountry($code)
  124. {
  125. $countries = $this->countries;
  126. if(isset($countries[$code])) {
  127. return $countries[$code];
  128. }
  129. $country = (new \League\ISO3166\ISO3166)->alpha2($code);
  130. $this->countries[$code] = $country['name'];
  131. return $this->countries[$code];
  132. }
  133. }