Page 1 of 2

[SOLVED]Problem on string retrievieng by an Url

Posted: Mon May 23, 2016 3:06 pm
by Fasasoftware
Hi , i'm tryng to retrieve data from an webgadget. i need to get the first value after aud/usd like this : 0.72108 and then put it in to a variable to work with it...

Any idea??? thanks a lot in advance... Lestroso :oops:

Code: Select all



#myWindow = 0
#myWebGadget = 0
#myTimer = 0

Global URL.s = "http://webrates.truefx.com/rates/connect.html?=html&c=AUD/USD"
valore$= URL

Procedure Reload()
;   Debug "Reload..."
  SetGadgetText(#myWebGadget, URL)
EndProcedure
Left(valore$,6) ; BUT THIS DON'T WORK!!!
OpenWindow(#myWindow, 0, 0, 1000, 500, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget(#myWebGadget, 10, 10, 900, 480, URL)
AddWindowTimer(#myWindow, #myTimer, 1000) ; 1 second..
BindEvent(#PB_Event_Timer, @Reload(), #myWindow, #myWebGadget)


Re: Problem on string retrievieng by an Url

Posted: Mon May 23, 2016 3:36 pm
by SparrowhawkMMU
I think that you would be far better off using their API - see the link at the bottom of the Downloads page

Trying to parse HTML is going to give you:

a) a large (relatively speaking) overhead in bandwidth
b) latency issues over a leaner API
c) problems if they change their layout

Re: Problem on string retrievieng by an Url

Posted: Mon May 23, 2016 3:55 pm
by Fasasoftware
Thanks,

but i have already registred me to that site Truefx , and i have a custom api url of It.

Please... is there a manner to retrieve data from that url?? Thanks a lot....

Lestroso :oops:

Re: Problem on string retrievieng by an Url

Posted: Tue May 24, 2016 7:14 am
by the.weavster
Request the result in csv format:

http://webrates.truefx.com/rates/connec ... /USD&f=csv

and then you can use StringField() to get at the data you want.

There's an api reference here (page 4) that details what each field is.

Re: Problem on string retrievieng by an Url

Posted: Tue May 24, 2016 7:26 am
by Fasasoftware
Dear the.weavster,

I thank you so much....now i'll try your idea... thanks a lot again.

Best regards, lestroso :D

Re: Problem on string retrievieng by an Url

Posted: Tue May 24, 2016 7:41 am
by Fasasoftware
Hi, i tryed this but Don't work....can somebody help me please??' i can't retrieve data from this Url..or better i don't know how....
This code give me: "http://webrates.truefx.com/rates/connec ... /USD&f=csv" as result... but i need 0,72290 or better 72290


best regards, lestroso :oops:

Code: Select all


#myWindow = 0
#myWebGadget = 0
#myTimer = 0


Global URL.s = "http://webrates.truefx.com/rates/connect.html?c=AUD/USD&f=csv"


Procedure Reload()
   ;Debug "Reload..."
  SetGadgetText(#myWebGadget, URL)
   Debug StringField(URL, 1, " ")

EndProcedure

OpenWindow(#myWindow, 0, 0, 1000, 500, "Sbok", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
WebGadget(#myWebGadget, 10, 10, 900, 480, URL)
AddWindowTimer(#myWindow, #myTimer, 1000) ; 1 second..
BindEvent(#PB_Event_Timer, @Reload(), #myWindow, #myWebGadget)


Re: Problem on string retrievieng by an Url

Posted: Tue May 24, 2016 3:19 pm
by Peter
it's not so easy to retrieve this data via JavaScript because of security reasons.

With this code:

Code: Select all

#myWindow = 0
#myListIconGadget = 0
#myTimer = 0

Procedure HttpGetEvent(Success, Result$, UserData)
  
  If Success
    Debug Result$
    ; AUD/USD,1464101882768,0.71,855,0.71,863,0.71450,0.72290,0.72233
    Result$ = ReplaceString(Result$, ",", #LF$)
    AddGadgetItem(#myListIconGadget, -1, Result$)
  Else
    Debug "HTTPRequest(): Error"
  EndIf
  
EndProcedure

Procedure Reload()
  
  Protected URL.s       = "http://webrates.truefx.com/rates/connect.html"
  Protected Parameter.s = "c=AUD/USD&f=csv"
  
  HTTPRequest(#PB_HTTP_Get, URL, Parameter, @HttpGetEvent())
  
EndProcedure

OpenWindow(#myWindow, 0, 0, 1000, 500, "Sbok", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ListIconGadget(#myListIconGadget, 10, 10, 900, 480, "CsvField1", 100)

For Counter = 1 To 8
  AddGadgetColumn(#myListIconGadget, Counter, "CsvField" + Str(Counter + 1), 100)
Next

AddWindowTimer(#myWindow, #myTimer, 1000) ; 1 second..

BindEvent(#PB_Event_Timer, @Reload(), #myWindow, #myTimer)
you get the following error:
Browser wrote:XMLHttpRequest cannot load http://webrates.truefx.com/rates/connec ... 4102044910. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:9080' is therefore not allowed access.
Therefore you have to create a little proxy (for example in php) and put it on your webserver:

Code: Select all

<?php
header('Access-Control-Allow-Origin: *');
echo file_get_contents('http://webrates.truefx.com/rates/connect.html?c=AUD/USD&f=csv');
?>
Then you can connect your proxy from SpiderBasic:

Code: Select all

#myWindow = 0
#myListIconGadget = 0
#myTimer = 0

Procedure HttpGetEvent(Success, Result$, UserData)
  
  If Success
    Debug Result$
    Result$ = ReplaceString(Result$, ",", #LF$)
    AddGadgetItem(#myListIconGadget, -1, Result$)
  Else
    Debug "HTTPRequest(): Error"
  EndIf
  
EndProcedure

Procedure Reload()
  
  Protected URL.s       = "http://localhost/proxy.php"
  Protected Parameter.s = ""
  
  HTTPRequest(#PB_HTTP_Get, URL, Parameter, @HttpGetEvent())
  
EndProcedure

OpenWindow(#myWindow, 0, 0, 1000, 500, "Sbok", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ListIconGadget(#myListIconGadget, 10, 10, 900, 480, "CsvField1", 100)

For Counter = 1 To 8
  AddGadgetColumn(#myListIconGadget, Counter, "CsvField" + Str(Counter + 1), 100)
Next

AddWindowTimer(#myWindow, #myTimer, 1000) ; 1 second..

BindEvent(#PB_Event_Timer, @Reload(), #myWindow, #myTimer)
Image

Greetings ... Peter

Re: Problem on string retrievieng by an Url

Posted: Tue May 24, 2016 6:54 pm
by Fasasoftware
Dear Peter, your very kind!!!Thousand thanks!!

This works very fine..... but i have still a little problem.....

I need to put Csvfield3 and Csvfield4 into a numeric variable and truncate the first "0." and join the two results ,like "71804" in real time....(every second)...to make some opearations.....

Can you help me please??thanks a lot again... :oops:

Re: Problem on string retrievieng by an Url

Posted: Tue May 24, 2016 8:05 pm
by the.weavster
Fasasoftware wrote:I need to put Csvfield3 and Csvfield4 into a numeric variable and truncate the first "0." and join the two results ,like "71804" in real time....(every second)...to make some opearations.....
You can use StringField() again with "." as the delimiter to get what's before and after the decimal point.

Concatenation is simply: string1 + string2

The commands to convert strings to numbers are Val(), ValD() and ValF()

Re: Problem on string retrievieng by an Url

Posted: Tue May 24, 2016 8:30 pm
by Fasasoftware
Thanks also to you ..the.weavster

Now i'll try your idea and test if working...

Thanks again,

lestroso :D