Advertisement
Axxxxxx

ExchangeController.php

Oct 26th, 2021
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.54 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Models\Exchange;
  6. use App\Models\Ticker;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Http;
  9.  
  10. class ExchangeController extends Controller
  11. {
  12.     const EOD_TOKEN = '6114dc62336677.73843440';
  13.  
  14.     // prod
  15.     const EOD_EXCHANGE_URL = 'https://eodhistoricaldata.com/api/exchanges-list/';
  16.     const EOD_SYMBOL_LIST_URL = 'https://eodhistoricaldata.com/api/exchange-symbol-list/{EXCHANGE_CODE}?api_token={YOUR_API_KEY}&fmt=json';
  17.     //https://eodhistoricaldata.com/api/exchange-symbol-list/US?api_token=6114dc62336677.73843440&fmt=json
  18.  
  19.     // dev
  20.     const _EOD_EXCHANGE_URL = 'https://m.taustyle.com/eod.json';
  21.     const _EOD_SYMBOL_LIST_URL = 'https://m.taustyle.com/US.json';
  22.  
  23.     /**
  24.      * Display a listing of the resource.
  25.      *
  26.      * @return \Illuminate\Http\Response
  27.      */
  28.     public function index()
  29.     {
  30.         $exchanges = Exchange::paginate(5);
  31.  
  32.         return view('exchanges.index', compact('exchanges'));
  33.     }
  34.  
  35.     public function edit($id)
  36.     {
  37.         $exchange = Exchange::find($id);
  38.  
  39.         return view('exchanges.edit', compact('exchange'));
  40.     }
  41.  
  42.     /**
  43.      * Display the specified resource.
  44.      *
  45.      * @param int $id
  46.      *
  47.      * @return \Illuminate\Http\Response
  48.      */
  49.     public function show($id)
  50.     {
  51.         // $exchange = Exchange::find($id);
  52.  
  53.         // $tickers = Ticker::paginate(10);
  54.         // dd($tickers, $exchange);
  55.         // $tickers = Ticker::paginate(3);
  56.  
  57.         // return view('tickers.index', compact('tickers'));
  58.  
  59.         // return view('exchanges.show', compact('exchange'));
  60.     }
  61.  
  62.     /**
  63.      * update tickers for exchange.
  64.      *
  65.      * @param mixed $id
  66.      *
  67.      * @return void
  68.      */
  69.     public function update(Request $request, $id)
  70.     {
  71.         // $ticker = new TickerController();
  72.         // dd($request, $id);
  73.  
  74.         $start = microtime(true);
  75.         $mem_usage = memory_get_usage();
  76.  
  77.         $this->getTickersByExchange($id);
  78.  
  79.         $time = microtime(true) - $start;
  80.         $memory_usage = 'Memory usage: '.round((memory_get_usage() - $mem_usage) / 1024 / 1024, 3).'Mb'."\n";
  81.         $time_execute = sprintf('Time execute %.4F sec.', $time);
  82.  
  83.         return redirect()->route('exchanges.index')->with('success', $memory_usage.$time_execute.' Exchanges ccccccccccccc updated');
  84.     }
  85.  
  86.     /**
  87.      * Fetch all exchanges.
  88.      *
  89.      * @return void
  90.      */
  91.     public function fetch()
  92.     {
  93.         $url = self::EOD_EXCHANGE_URL.'?api_token='.self::EOD_TOKEN.'&fmt=json';
  94.         $url = self::_EOD_EXCHANGE_URL;
  95.         // dd($url);
  96.  
  97.         $response = Http::get($url);
  98.  
  99.         $data = json_decode($response->body());
  100.  
  101.         if (!$data) {
  102.             return;
  103.         }
  104.  
  105.         $data = collect($data);
  106.         $chunks = $data->chunk(50);
  107.         $identificator_fields = ['code'];
  108.         $update_fields = ['name', 'operatingmic', 'country', 'currency'];
  109.  
  110.         foreach ($chunks as $chunk) {
  111.             $_chunk = null;
  112.             $_data = [];
  113.             $_chunk = $chunk->toArray();
  114.             foreach ($_chunk as $v) {
  115.                 $_data[] = [
  116.                     'code' => $v->Code,
  117.                     'name' => $v->Name,
  118.                     'operatingmic' => $v->OperatingMIC,
  119.                     'country' => $v->Country,
  120.                     'currency' => $v->Currency,
  121.                 ];
  122.             }
  123.             Exchange::upsert($_data, $identificator_fields, $update_fields);
  124.         }
  125.  
  126.         return redirect()->route('exchanges.index')->with('success', 'Exchanges updated');
  127.     }
  128.  
  129.     /**
  130.      * store exchange.
  131.      *
  132.      * @return void
  133.      */
  134.     public function store($v)
  135.     {
  136.         $exchange_status = Exchange::updateOrCreate(
  137.             [
  138.                 // 'id' => $v->id,
  139.                 'code' => $v->Code,
  140.             ],
  141.             [
  142.                 'name' => $v->Name,
  143.                 'operatingmic' => $v->OperatingMIC,
  144.                 'country' => $v->Country,
  145.                 'currency' => $v->Currency,
  146.             ]
  147.         );
  148.     }
  149.  
  150.     /**
  151.      * getTickersByExchange.
  152.      *
  153.      * @param mixed $id
  154.      *
  155.      * @return void
  156.      */
  157.     public function getTickersByExchange($id)
  158.     {
  159.         $url = str_replace('{EXCHANGE_CODE}', $id, self::EOD_SYMBOL_LIST_URL);
  160.         $url = str_replace('{YOUR_API_KEY}', self::EOD_TOKEN, $url);
  161.         $url = self::_EOD_SYMBOL_LIST_URL;
  162.         $response = Http::get($url);
  163.  
  164.         $data = json_decode($response->body());
  165.         // s(count($data));
  166.         if (!$data) {
  167.             return;
  168.         }
  169.  
  170.         $data = collect($data);
  171.         $chunks = $data->chunk(50);
  172.  
  173.         $identificator_fields = [
  174.             'code',
  175.         ];
  176.         $update_fields = [
  177.             'name',
  178.             'country',
  179.             'exchange',
  180.             'currency',
  181.             'type',
  182.             'isin',
  183.         ];
  184.  
  185.         foreach ($chunks as $chunk) {
  186.             $_chunk = null;
  187.             $_data = [];
  188.             $_chunk = $chunk->toArray();
  189.  
  190.             foreach ($_chunk as $v) {
  191.                 $_data[] = [
  192.                     'code' => $v->Code,
  193.                     'name' => $v->Name,
  194.                     'country' => $v->Country,
  195.                     'exchange' => $v->Exchange,
  196.                     'currency' => $v->Currency,
  197.                     'type' => $v->Type,
  198.                     'isin' => $v->Isin,
  199.                 ];
  200.             }
  201.             Ticker::upsert($_data, $identificator_fields, $update_fields);
  202.         }
  203.     }
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement