chat open Ai questions ( currency rates )

chat open Ai questions ( currency rates )

Postby Silvio.Falconi » Wed May 10, 2023 12:03 pm

Someone asked me to download the exchange rates for various currencies online and therefore, not knowing how to do it, I asked OpenAi

I asked OpenAi this question "can you make me a function in Harbour/ clipper language that takes the currency rates from the internet without using the access of Open Exchange Rates but of another non-paid service ?"

and gave me this answer
but i can't compile the source

Code: Select all  Expand view

#include "hbapi.h"
#include "hbapierr.h"
#include "hbapiitm.h"
#include "hbstack.h"
#include "curl/curl.h"

FUNCTION GetExchangeRates( cFromCurrency, cToCurrency )
    LOCAL cUrl := "https://api.exchangeratesapi.io/latest?base=" + cFromCurrency + "&symbols=" + cToCurrency
    LOCAL curl := curl_easy_init()
    LOCAL cResult := ""

    IF curl == NIL
        RETURN NIL
    ENDIF

    curl_easy_setopt( curl, CURLOPT_URL, cUrl )
    curl_easy_setopt( curl, CURLOPT_FOLLOWLOCATION, 1 )
    curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, { |buffer, size, nmemb, userp| buffer } )
    curl_easy_setopt( curl, CURLOPT_WRITEDATA, @cResult )

    IF curl_easy_perform( curl ) == CURLE_OK
        LOCAL oJson := JsonToObj( cResult )
        RETURN oJson.rates[cToCurrency]
    ENDIF

    curl_easy_cleanup( curl )
    RETURN NIL
END FUNCTION
 



another questions

can you make me a function in Harbor/ clipper language that takes the currency rates from the internet without using the access of Open Exchange Rates but of another non-paid service (bank of Italy) and without using the libcurl library passing as parameter the date and the type of currency for example EUR

open Ai gave me this source

Code: Select all  Expand view

FUNCTION GetExchangeRates( cCurrency, dDate )
    LOCAL cUrl := "https://www.bancaditalia.it/compiti/operazioni-cambi/serie-storiche/" + ;
                  TRANSFORM( YEAR( dDate ) ) + "-" + TRANSFORM( MONTH( dDate ), "@0" ) + "-" + ;
                  TRANSFORM( DAY( dDate ), "@0" ) + "/euro_valute_1/Valute_" + cCurrency + ".csv"
    LOCAL cResult := UrlGet( cUrl )
    LOCAL aLines := {}
    LOCAL nLineCount := 0
    LOCAL nRowIndex := 0

    // Divide la stringa in righe
    aLines := Explode( cResult, CRLF )
    nLineCount := Len( aLines )

    // Cerca la riga contenente i dati dei tassi di cambio
    FOR nRowIndex := 1 TO nLineCount
        IF AT( "data validita'", aLines[nRowIndex] ) > 0
            EXIT
        ENDIF
    NEXT

    // Se la riga è stata trovata, estrai i dati dei tassi di cambio
    IF nRowIndex <= nLineCount
        LOCAL aValues := {}
        LOCAL nValueCount := 0
        LOCAL nValueIndex := 0

        // Divide la riga in colonne
        aValues := Explode( aLines[nRowIndex], ";" )
        nValueCount := Len( aValues )

        // Cerca il valore corrispondente alla valuta richiesta
        FOR nValueIndex := 1 TO nValueCount
            IF AT( cCurrency, aValues[nValueIndex] ) > 0
                RETURN Val( aValues[nValueIndex + 1] )
            ENDIF
        NEXT
    ENDIF

    // Se i dati dei tassi di cambio non sono stati trovati, restituisci 0
    RETURN 0
END FUNCTION
 
Last edited by Silvio.Falconi on Wed May 10, 2023 12:25 pm, edited 2 times in total.
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7053
Joined: Thu Oct 18, 2012 7:17 pm

Re: chat open Ai questions

Postby Silvio.Falconi » Wed May 10, 2023 12:17 pm

a further request using the API of the bank of Italy and this was the answer

The Bank of Italy offers an exchange rate access service through the REST API available at the address https://tassidicambio.bancaditalia.it/t ... /rest/v1.0. This service allows you to get up-to-date exchange rates for different currencies.
You can use Harbour/Clipper's NetHttpReq() function to make an HTTP request to the Banca d'Italia REST API and then use Harbour/Clipper's JSON parsing functions to parse the request response and retrieve rate data of exchange.
Here is an example of a function in Harbour/Clipper that uses NetHttpReq() to make an HTTP request to the Banca d'Italia REST API and then extracts the exchange rate data using Harbour/Clipper's JSON parsing functions:


I found a document how to extrapolate the data but I didn't understand much https://tassidicambio.bancaditalia.it/terzevalute-wf-ui-web/assets/files/Istruzioni_tecnico-operative.pdf


Code: Select all  Expand view


FUNCTION GetExchangeRates( cCurrency, dDate )
    LOCAL cUrl := "https://tassidicambio.bancaditalia.it/terzevalute-wf-web/rest/v1.0/cross/" + ;
                  cCurrency + "/EUR/" + DTOC( dDate, "YYYYMMDD" )
    LOCAL cResult := NetHttpReq( cUrl )
    LOCAL oJson := JsonObj()
    LOCAL aRates := {}
    LOCAL nRateCount := 0
    LOCAL nRateIndex := 0

    // Analizza la risposta JSON della richiesta HTTP
    JsonParse( oJson, cResult )

    // Estrai i dati dei tassi di cambio dalla risposta JSON
    aRates := oJson->rates
    nRateCount := Len( aRates )

    // Cerca il valore corrispondente alla valuta richiesta
    FOR nRateIndex := 1 TO nRateCount
        IF aRates[nRateIndex]->currencyCode == cCurrency
            RETURN aRates[nRateIndex]->rate
        ENDIF
    NEXT

    // Se i dati dei tassi di cambio non sono stati trovati, restituisci 0
    RETURN 0
END FUNCTION
 


In this example, the GetExchangeRates function takes two arguments: cCurrency, which indicates the target currency for the conversion, and dDate, which indicates the date for which to retrieve the reference exchange rates.

The function then builds a request URL to the Bank of Italy REST API, passing as parameters the target currency, the base currency (EUR) and the requested date.

Next, the function uses the Harbour/Clipper NetHttpReq() function to make the HTTP request to the Banca d'Italia REST API.

Finally, the function uses Harbour/Clipper's JSON parsing functions to parse the request response and retrieve the exchange rate data corresponding to the requested currency. If no exchange rate data was found, the function returns 0.








But not run because there are function I not Know , how I can resolve it?
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7053
Joined: Thu Oct 18, 2012 7:17 pm

Re: chat open Ai questions ( currency rates )

Postby Otto » Wed May 10, 2023 2:39 pm

Silvio,
this is working for me:

Code: Select all  Expand view

#include "FiveWin.ch"

 
function Main()
    local cWebContent :=  WebPageContents("https://api.exchangerate-api.com/v4/latest/EUR"  )
    ?  cWebContent
 
return nil
   
 
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6330
Joined: Fri Oct 07, 2005 7:07 pm

Re: chat open Ai questions ( currency rates )

Postby Silvio.Falconi » Wed May 10, 2023 5:12 pm

Image

Code: Select all  Expand view
#include "FiveWin.ch"
Function main()

   GetCurrency("EUR")

   return nil

function GetCurrency(cCurrency)
        local cRet
        local cData
        local cFile := "test.txt"
        local cBuff
        local nHndl
        local aShow
        local nI
        local cUrl := "https://api.exchangerate-api.com/v4/latest/"+cCurrency
        local ctext,adata


    IF IsInternet()

               cData := WebPageContents( cUrl )

      if Empty( cData )
          ? "Invalid URL"
       else
            cData := subStr( cData, at( "rates",  cData )+8 )
            nHndl := fCreate( cFile )
            fWrite( nHndl,cData )
            fClose( nHndl )
            IF ( nHndl := fOpen( cFile ) ) < 1
                msgStop( "UNABLE TO OPEN: " + cFile  )
             ELSE
                aShow := {}
                cText    := HB_MEMOREAD( cFile)
                cText    := FW_ALLTRIM( cText )
                cText    := StrTran( cText, CRLF,  "," )
                aData    := HB_ATokens( cText,  "," )

                  for n= 1 to len(adata)
                     aadd(aShow, HB_ATokens( adata[n],  ":" )  )
                  next

                xbrowser aShow

     ENDIF
         endif
      else
         MsgAlert("check Internet!","")
          endif
        return nil

//-------------------------------------------------------------------------//


 

I saw if you not register your account free the rates is bad, for a sample 1EUR USD= 1.1
with the registration 1 EUR -> USD= 1.096


How do I remove quotes from strings in the first column?
Last edited by Silvio.Falconi on Thu May 11, 2023 9:08 am, edited 1 time in total.
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7053
Joined: Thu Oct 18, 2012 7:17 pm

Re: chat open Ai questions ( currency rates )

Postby Otto » Thu May 11, 2023 8:30 am

Silvio, you can use another STRTRAN.
cText := StrTran(cText, '"', '' )
Regards,
Otto

Code: Select all  Expand view

aShow := {}
                cText    := MEMOREAD( cFile)
               
                cText    := ALLTRIM( cText )
                cText    := StrTran( cText, CRLF,  "," )
               
                    cText    := StrTran(cText, '"', '' )    
               
               
                aData    := HB_ATokens( cText,  "," )
 
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6330
Joined: Fri Oct 07, 2005 7:07 pm

Re: chat open Ai questions ( currency rates )

Postby Silvio.Falconi » Thu May 11, 2023 9:13 am

Otto wrote:Silvio, you can use another STRTRAN.
cText := StrTran(cText, '"', '' )
Regards,
Otto

Code: Select all  Expand view

aShow := {}
                cText    := MEMOREAD( cFile)
               
                cText    := ALLTRIM( cText )
                cText    := StrTran( cText, CRLF,  "," )
               
                    cText    := StrTran(cText, '"', '' )    
               
               
                aData    := HB_ATokens( cText,  "," )
 




another
see this
Image
how take the date ?

I tried with
cRet := subStr( cRet, at( "update_utc", cRet )+12 )
cRet := allTrim( subStr( cRet, 1, at( 'time', cRet )-7 ) )
but I have the date string bad
Last edited by Silvio.Falconi on Thu May 11, 2023 9:24 am, edited 1 time in total.
Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)
I use : FiveWin for Harbour November 2023 - January 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
User avatar
Silvio.Falconi
 
Posts: 7053
Joined: Thu Oct 18, 2012 7:17 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 42 guests