Recent

Author Topic: CEF component - the first step  (Read 1669 times)

loaded

  • Hero Member
  • *****
  • Posts: 870
Re: CEF component - the first step
« Reply #15 on: June 19, 2025, 10:15:22 am »
It it really necessary to have this dinosaur in your project? ....

Hey Master Wp, It's a bit harsh to call a masterpiece like CEF4 a dinosaur. 🤔
I think it's a great piece of work, yes it's a bit difficult to use at first but once you get used to it it's addictive and works flawlessly with all old and new technology sites.😉
By the way, I would like to thank my brother salvadordf for this unique work. Best regards.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

Thaddy

  • Hero Member
  • *****
  • Posts: 17414
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: CEF component - the first step
« Reply #16 on: June 19, 2025, 10:54:49 am »
I think wp was merely referring to the "excessive" size, when counting in all dependencies, not to deprecated code.
To display tickers from yahoo financial, you only need fcl-web/net and process json.
You definitely don't want to use the behemoth - which may be a better wording - that is CEF4 for that.
You only need 20-30 lines of code.

For multi-featured full browser processing CEF4 is a great choice but not fit for this particular very simple purpose.
« Last Edit: June 19, 2025, 11:03:04 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Thaddy

  • Hero Member
  • *****
  • Posts: 17414
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: CEF component - the first step
« Reply #17 on: June 19, 2025, 11:19:34 am »
@Nicole

Now I contradict myself for the number of lines...just a bit.... but here is a full example to retrieve the EUR/USD exchange rate from Yahoo finance.
I recommend getting a real API key from yahoo, but here I also implemented a - free - fallback.
Note do not try the fallback for real-time results:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. {$ifdef windows}{$apptype console}{$endif}
  3.  
  4. uses
  5.   Classes,
  6.   SysUtils,
  7.   fphttpclient,
  8.   fpjson,
  9.   jsonparser,
  10.   openssl,
  11.   opensslsockets;
  12.  
  13. const
  14.   YAHOO_API_KEY = 'your_api_key_here'; // Replace with actual key
  15.   YAHOO_FINANCE_API = 'https://query1.finance.yahoo.com/v7/finance/quote?symbols=EURUSD=X';
  16.   FALLBACK_API = 'https://api.exchangerate-api.com/v4/latest/EUR';
  17.  
  18. function GetYahooExchangeRate: Double;
  19. var
  20.   Client: TFPHttpClient;
  21.   Response: String;
  22.   JSONData: TJSONData;
  23.   QuotesArray: TJSONArray;
  24. begin
  25.   Result := 0;
  26.   Client := TFPHttpClient.Create(nil);
  27.   try
  28.     // Configure Yahoo Finance API request
  29.     Client.RequestHeaders.Clear;
  30.     Client.AddHeader('Accept', 'application/json');
  31.     Client.AddHeader('x-api-key', YAHOO_API_KEY); // API key header
  32.    
  33.     try
  34.       // Make authenticated request
  35.       Response := Client.Get(YAHOO_FINANCE_API);
  36.      
  37.       JSONData := GetJSON(Response);
  38.       try
  39.         // Parse the updated Yahoo Finance API response format
  40.         QuotesArray := (JSONData as TJSONObject).Get('quoteResponse',
  41.                        TJSONObject(nil)).Get('result', TJSONArray(nil));
  42.         if (QuotesArray <> nil) and (QuotesArray.Count > 0) then
  43.           Result := (QuotesArray.Items[0] as TJSONObject).Get('regularMarketPrice', 0.0);
  44.       finally
  45.         JSONData.Free;
  46.       end;
  47.     except
  48.       on E: Exception do
  49.         Writeln('Yahoo API Error: ', E.Message);
  50.     end;
  51.   finally
  52.     Client.Free;
  53.   end;
  54. end;
  55.  
  56. function GetFallbackExchangeRate: Double;
  57. var
  58.   Client: TFPHttpClient;
  59.   Response: String;
  60.   JSONData: TJSONData;
  61. begin
  62.   Result := 0;
  63.   Client := TFPHttpClient.Create(nil);
  64.   try
  65.     Client.RequestHeaders.Clear;
  66.     Client.AddHeader('User-Agent', 'CDDatabase/1.0');
  67.    
  68.     try
  69.       Response := Client.Get(FALLBACK_API);
  70.      
  71.       JSONData := GetJSON(Response);
  72.       try
  73.         Result := (JSONData as TJSONObject).Get('rates',
  74.                   TJSONObject(nil)).Get('USD', 0.0);
  75.       finally
  76.         JSONData.Free;
  77.       end;
  78.     except
  79.       on E: Exception do
  80.         Writeln('Fallback API Error: ', E.Message);
  81.     end;
  82.   finally
  83.     Client.Free;
  84.   end;
  85. end;
  86.  
  87. var
  88.   ExchangeRate: Double;
  89. begin
  90.   Writeln('Attempting to fetch EUR/USD rate...');
  91.  
  92.   // Try Yahoo with API key first
  93.   ExchangeRate := GetYahooExchangeRate;
  94.  
  95.   // If Yahoo fails, use fallback
  96.   if ExchangeRate = 0 then
  97.   begin
  98.     Writeln('Yahoo API failed, using fallback...');
  99.     ExchangeRate := GetFallbackExchangeRate;
  100.   end;
  101.  
  102.   if ExchangeRate > 0 then
  103.     Writeln(Format('Current EUR/USD: %.4f', [ExchangeRate]))
  104.   else
  105.     Writeln('All API attempts failed');
  106. end.
For real-time results you will need to pay!!! and get the professional API.
That is normal, but this also works.

I used a mix and match of three sources, my own, yahoo and deepseek (for the fallback.)

I would personally add some more robustness too. But this is already quite complete.
If you want to use the above for the real Yahoo API, I guess you just need to add the real key.
If you already have a key - like I have - I can amend the example to be a bit shorter if you want.
There is a place holder, though.
And you do not need CEF4 for this.

I wrote this from my professional background in finance software, just to test if I could still do it.
(I am a pensioner)

I can reduce this to ~40 lines and there is very little overhead. Mostly OpenSSL.

Plz note that if you do not have an API key, an exception is raised and handled.
This is intentional. (one always gets stupid remarks or questions in such cases)
« Last Edit: June 19, 2025, 01:17:42 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Nicole

  • Hero Member
  • *****
  • Posts: 1211
Re: CEF component - the first step
« Reply #18 on: June 19, 2025, 09:05:31 pm »
thank you so much for the unit!
This is very useful to me.

At the moment I collect such data requests.

Do you need a FOREX request from Frankfurt?
If yes, I can post it.

Further I have an Alpha Vantage (thank you WP for your help!) and at the moment I work on MarketStack.
And there is one half-baked for stock-key-data, like ISIN, CPID ... no quotes, but all keys of the world.

 

TinyPortal © 2005-2018