[SOLVED]Problem on string retrievieng by an Url

Just starting out? Need help? Post your questions and find answers here.
Fasasoftware
Posts: 47
Joined: Tue May 26, 2015 11:22 pm

[SOLVED]Problem on string retrievieng by an Url

Post 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)

Last edited by Fasasoftware on Wed Jun 01, 2016 10:20 pm, edited 1 time in total.
User avatar
SparrowhawkMMU
Posts: 291
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: Problem on string retrievieng by an Url

Post 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
Fasasoftware
Posts: 47
Joined: Tue May 26, 2015 11:22 pm

Re: Problem on string retrievieng by an Url

Post 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:
the.weavster
Posts: 229
Joined: Sat Mar 01, 2014 3:02 pm

Re: Problem on string retrievieng by an Url

Post 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.
Fasasoftware
Posts: 47
Joined: Tue May 26, 2015 11:22 pm

Re: Problem on string retrievieng by an Url

Post by Fasasoftware »

Dear the.weavster,

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

Best regards, lestroso :D
Fasasoftware
Posts: 47
Joined: Tue May 26, 2015 11:22 pm

Re: Problem on string retrievieng by an Url

Post 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)

User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Problem on string retrievieng by an Url

Post 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
Fasasoftware
Posts: 47
Joined: Tue May 26, 2015 11:22 pm

Re: Problem on string retrievieng by an Url

Post 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:
the.weavster
Posts: 229
Joined: Sat Mar 01, 2014 3:02 pm

Re: Problem on string retrievieng by an Url

Post 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()
Fasasoftware
Posts: 47
Joined: Tue May 26, 2015 11:22 pm

Re: Problem on string retrievieng by an Url

Post by Fasasoftware »

Thanks also to you ..the.weavster

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

Thanks again,

lestroso :D
Post Reply