Calling Javascript functions

BlitzMax Forums/BlitzMax Programming/Calling Javascript functions

plash(Posted 2008) [#1]
I can get the correct page for the translation of a word from English into Greek using http://translate.google.com/translate_t?sl=en&tl=el&q=Hello

But when I try to parse the response text from that link, using libxml (looking for '<div id=result_box ...'), it yells at me for innumerable parse errors.

I can also do this:


But that would have to be hosted somewhere for it to work, no?


Brucey(Posted 2008) [#2]
You know that the docs ( http://code.google.com/apis/ajaxlanguage/documentation/ ) explain about their JSON encoded results that are available through the non-javascript interface?

Now, if there was only a JSON parser for BlitzMax....


plash(Posted 2008) [#3]
Awesome!
http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=Hello&langpair=en%7Cel


http://www.blitzbasic.com/codearcs/codearcs.php?code=2066

Cheers.


Brucey(Posted 2008) [#4]
Geepers, someone wrote one *in* BlitzMax...

I only went so far as to wrap one... - maybe I'm getting lazy :-p


plash(Posted 2008) [#5]
Any idea why OpenStream keeps returning null?



(Concept taken from: http://www.blitzbasic.com/Community/posts.php?topic=67848#761689)


Yan(Posted 2008) [#6]
See here for examples of how to form HTTP requests and handle the server's response*.


You'll also need to format the string to be translated to handle spaces etc, will you not?


Finally, that JSON parser seems like an awful lot of code to use if you'll only ever be looking for the text between '"translatedText":"' and '"'. :o/



*You could use a 'http::' stream directly, of course, but you'd lose some error checking.


plash(Posted 2008) [#7]
Finally, that JSON parser seems like an awful lot of code to use if you'll only ever be looking for the text between '"translatedText":"' and '"', to me.
Yep, but I think I'll be using it for config and whatnot aswell..

Nothing I changed made a connection (using "http::" and writeline("GET /" + url + "HTTP/1.0 (or 1.1) ~r~n~r~n")), OpenStream still returns null. I think I'll be using my modified THttp type (codearcs), as it seemed to work before for getting pages.

I swear I had it getting pages just a few hours ago.. now its not working. I checked against your HTTP header code, all seems to be in place...

(See the Get method)



plash(Posted 2008) [#8]
Nevermind. There was a space in my url (word="Hello there!").. lol!

Thanks all!


Yan(Posted 2008) [#9]
Nothing I changed made a connection (using "http::" and writeline("GET /" + url + "HTTP/1.0 (or 1.1) ~r~n~r~n")), OpenStream still returns null.
I think you're a bit confused about the difference between a tcp and a http stream...
Local stream:TStream = OpenStream("http::ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello&langpair=en%7Cel")
Assert stream, "Couldn't establish connection"

Print stream.ReadLine()

stream.Close()


It'd probably be a good idea to check that the header returned is free from error (HTTP/1.X 200 OK).


Brucey(Posted 2008) [#10]
You might also consider this part of the docs...
An area to pay special attention to relates to correctly identifying yourself in your requests. Applications MUST always include a valid and accurate http referer header in their requests.



plash(Posted 2008) [#11]
I think you're a bit confused about the difference between a tcp and a http stream...

Ahh. That's much simpler..

Local word:String = "Hello there, mate!".Replace(" ", "+")
Local Text:String = GetHTTPString("ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + word + "&langpair=en%7Cel")

SaveText(Text, "data.txt")

Local json:TJSON = TJSON.Create(Text)

SaveText(json.GetValue("responseData.translatedText").ToString(), "data.txt")


Function GetHTTPString:String(url:String)
  Local stream:TStream = OpenStream("http::" + url)

	If stream <> Null
	  Local result:String = stream.ReadLine()
		
		Return result
		
	Else
	   Return Null
	   
	End If
	
End Function


EDIT:
It'd probably be a good idea to check that the header returned is free from error (HTTP/1.X 200 OK).
How would you check the returned header using the code you posted? (AFAIK it just returns the content)

EDIT2: I suppose that would be {"responseData": {"translatedText":"&#915;&#949;&#953;&#940;"}, "responseDetails": null, "responseStatus": 200}??