EDDB

mail@pastecode.io avatar
unknown
php
3 years ago
3.5 kB
6
Indexable
<?php

namespace App\Console\Commands;

use App\Imports\PriceImport;
use App\Models\ED\Comodity;
use App\Models\ED\Price;
use App\Models\ED\Station;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;

class GetPrices extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ed:prices';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    // public function handle()
    // {

    //     $url = "https://eddb.io/archive/v6/listings.csv";

    //     $this->info("Start downloading listings...");

    //     // $content = Http::withoutVerifying()->withHeaders([
    //     //     'Accept-Encoding' => 'gzip, deflate, sdch'
    //     // ])->get($url);
    //     // Storage::put('eddb/listings.csv', $content->body());

    //     $this->info("Finished downloading listings!");
    //     $this->info("Seeding table [listings]...");

    //     $this->output->title('Starting import');
    //     (new PriceImport)->withOutput($this->output)->import('eddb/listings.csv', 'local', \Maatwebsite\Excel\Excel::CSV);
    //     // (new PriceImport)->withOutput($this->output)->import('eddb/listings.csv', 'local', \Maatwebsite\Excel\Excel::CSV);
    //     $this->output->success('Import successful');

    //     return 0;
    // }
    public function handle()
    {

        $url = "https://eddb.io/archive/v6/listings.csv";

        $this->info("Start downloading listings...");

        // $content = Http::withoutVerifying()->withHeaders([
        //     'Accept-Encoding' => 'gzip, deflate, sdch'
        // ])->get($url);
        // Storage::put('eddb/listings.csv', $content->body());

        $this->info("Finished downloading listings!");
        $this->info("Seeding table [listings]...");

        $this->output->title('Starting import');
        // ебет разбираться как выяснить кол-во строк в файле
        $bar = $this->output->createProgressBar(6899862);
        $bar->start();
        if (($handle = fopen(Storage::path('eddb/listings.csv'), "r")) !== FALSE) {
            while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
                
                $station = Station::where('external_id', $row[1])->first();
                $commodity = Comodity::where('external_id', $row[2])->first();

                if ($station !== null && $commodity !== null) {
                    Price::create([
                        "station_id" => $station->id,
                        "commodity_id" => $commodity->id,
                        "buy_price" => intval($row[5]),
                        "sell_price" => intval($row[6])
                    ]);
                    $bar->advance();
                }
            }
            fclose($handle);
        }
        $bar->finish();
        // (new PriceImport)->withOutput($this->output)->import('eddb/listings.csv', 'local', \Maatwebsite\Excel\Excel::CSV);
        // (new PriceImport)->withOutput($this->output)->import('eddb/listings.csv', 'local', \Maatwebsite\Excel\Excel::CSV);
        $this->output->success('Import successful');

        return 0;
    }
}