calling an api from a stack?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

calling an api from a stack?

Tore Nilsen via use-livecode
I am trying to make a call to the yandex translate api for a stack i am working on.
i could do this pretty handily in HTML but I want to do it in livecode.

I tried working with this but using yandex api instead of google

https://stackoverflow.com/questions/35957054/is-it-possible-to-create-a-translator-in-livecode-for-my-app-which-translates-an

no joy; this got me no further.

https://forums.livecode.com/viewtopic.php?t=14451


thanks for any ideas. yandex api is here
https://tech.yandex.com/translate/

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: calling an api from a stack?

Tore Nilsen via use-livecode
/*
A quick and dirty example of using yandex translator
Fill in your key then use the 2 functions to try things out.
Put this in your card or stack script, then call the desired function
passing in the required parameters.
*/
constant kKey="put your key here"

constant kGetTransURL="
https://translate.yandex.net/api/v1.5/tr.json/translate?key=[[kKey]]&text=[[urlencode(pText)]]&lang=[[pLang]]&format=plain&options=1
"
constant kGuessLanguageUrl="
https://translate.yandex.net/api/v1.5/tr.json/detect?key=[[kKey]]&text=[[urlencode(pText)]]&hint=[[pHints]]
"

/* The translate function.
pText is the text to translate
pLange is the target language. It can either be in "source-target" version,
or just target, allowing the system to guess the source language
Returns json and pops it into the message box.
It also has the option turned on to tell you its best guess language,
so that if you use the version (for example) en-es to go from english to
spanish
you can see if it agrees with you on the source language.
If you look at the constant kGetTransUrl, you can see
that when the merge is done, the text is urlencoded as part of the merge.
Do this for all text being sent for translation/analysis.
*/

function translate pText,pLang
   get URL merge(kGetTransUrl)
   return it & cr & the result
end translate

/*
 Just tries to guess the source language.
 pText again, is the text to send
 pHints is an optional comma delimited list
 of languages that you suspect might be the source.
 IE "es,fr" It is safe to send an empty string for pHints
 and let yandex check against all languages.
*/
function guessLanguage pText,pHints
   get URL merge(kGuessLanguageUrl)
   return it & cr & the result
end guessLanguage

On Fri, Jun 15, 2018 at 8:19 AM Eric A. Engle via use-livecode <
[hidden email]> wrote:

> I am trying to make a call to the yandex translate api for a stack i am
> working on.
> i could do this pretty handily in HTML but I want to do it in livecode.
>
> I tried working with this but using yandex api instead of google
>
>
> https://stackoverflow.com/questions/35957054/is-it-possible-to-create-a-translator-in-livecode-for-my-app-which-translates-an
>
> no joy; this got me no further.
>
> https://forums.livecode.com/viewtopic.php?t=14451
>
>
> thanks for any ideas. yandex api is here
> https://tech.yandex.com/translate/
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: calling an api from a stack?

Tore Nilsen via use-livecode
In reply to this post by Tore Nilsen via use-livecode
This works, sort of:

  put URL "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20180527T091305Z.7f33f9fb3f66f0bb.d573f1d9a6336a981504916600c45f49255938b3&text=hello%20world&lang=en-de" into tResult
  put tResult into card field 1
  --put JsonImport(tResult) into  tResults
  --put JsonToArray(tResults) into tProfile
  put tResult & return into cd fld 1
  set the itemdelimiter to "["
  put the last item of tResult after cd fld 1

for some reason JsonImport and JsonToArray return nothing. I can however set delimiter to [ and then programmatically delete the first and last 3 chars after the last item of tResult. But why can't, or how, must I extract from Json?

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: calling an api from a stack?

Tore Nilsen via use-livecode
jsontoarray returns an array, you'll have to dig through the array to find
where pieces of information reside, but it pretty straightforward after you
poke around.

For example.. If you instead do this..
put jsontoarray(tResult) into tArray

you have an array variable named tArray.
If you-- put the keys of tArray you'll see the first level of keys.
One of the keys is named "text" and it has a numeric subkey named 1
So if you-- put tArray["text"][1] into field 1
The translation will be placed into the field.

YOu can use the "put the keys"  syntax to find subkeys too, so if you-- put
the keys of tArray["text"]  you get back the single subkey 1. If you were
to-- put the keys of tArray["text"][1] you would get nothing back because
there are no subkeys.  Instead, a value has been placed into key
["text"][1] which is your translated text.  Its pretty easy to poke around
an array this way and figure things out, especially since the array for
this is pretty simple. (also look at "elements" in the dictionary)

On Fri, Jun 15, 2018 at 11:07 AM Eric A. Engle via use-livecode <
[hidden email]> wrote:

> This works, sort of:
>
>   put URL "
> https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20180527T091305Z.7f33f9fb3f66f0bb.d573f1d9a6336a981504916600c45f49255938b3&text=hello%20world&lang=en-de"
> into tResult
>   put tResult into card field 1
>   --put JsonImport(tResult) into  tResults
>   --put JsonToArray(tResults) into tProfile
>   put tResult & return into cd fld 1
>   set the itemdelimiter to "["
>   put the last item of tResult after cd fld 1
>
> for some reason JsonImport and JsonToArray return nothing. I can however
> set delimiter to [ and then programmatically delete the first and last 3
> chars after the last item of tResult. But why can't, or how, must I extract
> from Json?
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: calling an api from a stack?

Tore Nilsen via use-livecode
Again, this is why I don't like to nest functions. You cannot tell what the function returned without pulling it apart.

Bob S


> On Jun 15, 2018, at 10:40 , Mike Bonner via use-livecode <[hidden email]> wrote:
>
> jsontoarray returns an array, you'll have to dig through the array to find
> where pieces of information reside, but it pretty straightforward after you
> poke around.
>
> For example.. If you instead do this..
> put jsontoarray(tResult) into tArray
>
> you have an array variable named tArray.


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: calling an api from a stack?

Tore Nilsen via use-livecode
If you mean using merge inside the function, I guess one could do the merge
beforehand and pass it in as a parameter, or alternatively do the merge in
the function and return the generated url string as part of the returned
value.  IE change it like so..
function translate pText,pLang
   put merge(kGetTransUrl) into tUrl
   get url tUrl
   return "URLString: " & tUrl & cr & it
end translate

Then just dump the first line if things are working right.  Hmm. Actually,
pre-merging is nice because it would allow the use of "load" with callbacks
for multiple url calls. Use the callback to process, part of which lets you
see the URL associated with the callback.  Sounds like a better option than
using a function call for sure.

On Fri, Jun 15, 2018 at 2:54 PM Bob Sneidar via use-livecode <
[hidden email]> wrote:

> Again, this is why I don't like to nest functions. You cannot tell what
> the function returned without pulling it apart.
>
> Bob S
>
>
> > On Jun 15, 2018, at 10:40 , Mike Bonner via use-livecode <
> [hidden email]> wrote:
> >
> > jsontoarray returns an array, you'll have to dig through the array to
> find
> > where pieces of information reside, but it pretty straightforward after
> you
> > poke around.
> >
> > For example.. If you instead do this..
> > put jsontoarray(tResult) into tArray
> >
> > you have an array variable named tArray.
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: calling an api from a stack?

Tore Nilsen via use-livecode
Yes, but you found more utility than I was thinking of. My thing is that when tracing/debugging, I need to see what a function returns, and I can only do that if I put the returned value or it or the result into a variable of some sort.

Bob S


> On Jun 15, 2018, at 14:28 , Mike Bonner via use-livecode <[hidden email]> wrote:
>
> If you mean using merge inside the function, I guess one could do the merge
> beforehand and pass it in as a parameter, or alternatively do the merge in
> the function and return the generated url string as part of the returned
> value.  IE change it like so..
> function translate pText,pLang
>   put merge(kGetTransUrl) into tUrl
>   get url tUrl
>   return "URLString: " & tUrl & cr & it
> end translate
>
> Then just dump the first line if things are working right.  Hmm. Actually,
> pre-merging is nice because it would allow the use of "load" with callbacks
> for multiple url calls. Use the callback to process, part of which lets you
> see the URL associated with the callback.  Sounds like a better option than
> using a function call for sure.


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: calling an api from a stack?

Tore Nilsen via use-livecode
Thanks for the tip, and clarification.


On Fri, Jun 15, 2018 at 3:34 PM Bob Sneidar via use-livecode <
[hidden email]> wrote:

> Yes, but you found more utility than I was thinking of. My thing is that
> when tracing/debugging, I need to see what a function returns, and I can
> only do that if I put the returned value or it or the result into a
> variable of some sort.
>
> Bob S
>
>
> > On Jun 15, 2018, at 14:28 , Mike Bonner via use-livecode <
> [hidden email]> wrote:
> >
> > If you mean using merge inside the function, I guess one could do the
> merge
> > beforehand and pass it in as a parameter, or alternatively do the merge
> in
> > the function and return the generated url string as part of the returned
> > value.  IE change it like so..
> > function translate pText,pLang
> >   put merge(kGetTransUrl) into tUrl
> >   get url tUrl
> >   return "URLString: " & tUrl & cr & it
> > end translate
> >
> > Then just dump the first line if things are working right.  Hmm.
> Actually,
> > pre-merging is nice because it would allow the use of "load" with
> callbacks
> > for multiple url calls. Use the callback to process, part of which lets
> you
> > see the URL associated with the callback.  Sounds like a better option
> than
> > using a function call for sure.
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode