Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\Models\Exchange;
- use App\Models\Ticker;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Http;
- class ExchangeController extends Controller
- {
- const EOD_TOKEN = '6114dc62336677.73843440';
- // prod
- const EOD_EXCHANGE_URL = 'https://eodhistoricaldata.com/api/exchanges-list/';
- const EOD_SYMBOL_LIST_URL = 'https://eodhistoricaldata.com/api/exchange-symbol-list/{EXCHANGE_CODE}?api_token={YOUR_API_KEY}&fmt=json';
- //https://eodhistoricaldata.com/api/exchange-symbol-list/US?api_token=6114dc62336677.73843440&fmt=json
- // dev
- const _EOD_EXCHANGE_URL = 'https://m.taustyle.com/eod.json';
- const _EOD_SYMBOL_LIST_URL = 'https://m.taustyle.com/US.json';
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- $exchanges = Exchange::paginate(5);
- return view('exchanges.index', compact('exchanges'));
- }
- public function edit($id)
- {
- $exchange = Exchange::find($id);
- return view('exchanges.edit', compact('exchange'));
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- *
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- // $exchange = Exchange::find($id);
- // $tickers = Ticker::paginate(10);
- // dd($tickers, $exchange);
- // $tickers = Ticker::paginate(3);
- // return view('tickers.index', compact('tickers'));
- // return view('exchanges.show', compact('exchange'));
- }
- /**
- * update tickers for exchange.
- *
- * @param mixed $id
- *
- * @return void
- */
- public function update(Request $request, $id)
- {
- // $ticker = new TickerController();
- // dd($request, $id);
- $start = microtime(true);
- $mem_usage = memory_get_usage();
- $this->getTickersByExchange($id);
- $time = microtime(true) - $start;
- $memory_usage = 'Memory usage: '.round((memory_get_usage() - $mem_usage) / 1024 / 1024, 3).'Mb'."\n";
- $time_execute = sprintf('Time execute %.4F sec.', $time);
- return redirect()->route('exchanges.index')->with('success', $memory_usage.$time_execute.' Exchanges ccccccccccccc updated');
- }
- /**
- * Fetch all exchanges.
- *
- * @return void
- */
- public function fetch()
- {
- $url = self::EOD_EXCHANGE_URL.'?api_token='.self::EOD_TOKEN.'&fmt=json';
- $url = self::_EOD_EXCHANGE_URL;
- // dd($url);
- $response = Http::get($url);
- $data = json_decode($response->body());
- if (!$data) {
- return;
- }
- $data = collect($data);
- $chunks = $data->chunk(50);
- $identificator_fields = ['code'];
- $update_fields = ['name', 'operatingmic', 'country', 'currency'];
- foreach ($chunks as $chunk) {
- $_chunk = null;
- $_data = [];
- $_chunk = $chunk->toArray();
- foreach ($_chunk as $v) {
- $_data[] = [
- 'code' => $v->Code,
- 'name' => $v->Name,
- 'operatingmic' => $v->OperatingMIC,
- 'country' => $v->Country,
- 'currency' => $v->Currency,
- ];
- }
- Exchange::upsert($_data, $identificator_fields, $update_fields);
- }
- return redirect()->route('exchanges.index')->with('success', 'Exchanges updated');
- }
- /**
- * store exchange.
- *
- * @return void
- */
- public function store($v)
- {
- $exchange_status = Exchange::updateOrCreate(
- [
- // 'id' => $v->id,
- 'code' => $v->Code,
- ],
- [
- 'name' => $v->Name,
- 'operatingmic' => $v->OperatingMIC,
- 'country' => $v->Country,
- 'currency' => $v->Currency,
- ]
- );
- }
- /**
- * getTickersByExchange.
- *
- * @param mixed $id
- *
- * @return void
- */
- public function getTickersByExchange($id)
- {
- $url = str_replace('{EXCHANGE_CODE}', $id, self::EOD_SYMBOL_LIST_URL);
- $url = str_replace('{YOUR_API_KEY}', self::EOD_TOKEN, $url);
- $url = self::_EOD_SYMBOL_LIST_URL;
- $response = Http::get($url);
- $data = json_decode($response->body());
- // s(count($data));
- if (!$data) {
- return;
- }
- $data = collect($data);
- $chunks = $data->chunk(50);
- $identificator_fields = [
- 'code',
- ];
- $update_fields = [
- 'name',
- 'country',
- 'exchange',
- 'currency',
- 'type',
- 'isin',
- ];
- foreach ($chunks as $chunk) {
- $_chunk = null;
- $_data = [];
- $_chunk = $chunk->toArray();
- foreach ($_chunk as $v) {
- $_data[] = [
- 'code' => $v->Code,
- 'name' => $v->Name,
- 'country' => $v->Country,
- 'exchange' => $v->Exchange,
- 'currency' => $v->Currency,
- 'type' => $v->Type,
- 'isin' => $v->Isin,
- ];
- }
- Ticker::upsert($_data, $identificator_fields, $update_fields);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement