egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Converting CrazyCat's Translation Script into using API key

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Support & Releases
View previous topic :: View next topic  
Author Message
ComputerTech
Master


Joined: 22 Feb 2020
Posts: 374

PostPosted: Sat Mar 06, 2021 7:40 pm    Post subject: Converting CrazyCat's Translation Script into using API key Reply with quote

So i have tried to find the where i could insert the API key, as the Script says it uses API,
but i cant find where.

Code:

#
# Google Translator
#
# Translation using Google Translate API
#
# Author: CrazyCat <crazycat@c-p-f.org>
# http://www.eggdrop.fr
# irc.zeolia.net #c-p-f
#
# Usage:
#   !tr <lg> Sentence
#  lg is the destination language
#
# Changelog
# 0.2 : Decodes html entities when needed
#
# TODO:
#   - Lot of things, that's a beta version
#
namespace eval gTranslator {
   
   set lngs { "fr" "en" "es" "de" "it" "nl" "id" }
   
   #---------------------------------------------------------------------#
   #                    ***End of Settings ***                           #
   #   Do not edit below this line unless you know what you are doing! #
   #---------------------------------------------------------------------#
    variable author "CrazyCat"
    variable versionNum "0.2"
    variable versionName "gTranslator"
   
}

namespace eval gTranslator {
   
   bind pub - !tr gTranslator::translate
   
   proc translate { nick uhost handle chan text } {
      package require http
      package require json
      set lngto [string tolower [lindex [split $text] 0]]
      if { [lsearch $::gTranslator::lngs $lngto] == -1 } {
         putserv "PRIVMSG $chan :\002Attention\002 $lngto n'est pas valide"
         return 0
      }
      set text [::http::formatQuery q [join [lrange [split $text] 1 end]]]
      set dturl "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=$text"
      set res [::json::json2dict [::http::data [::http::geturl $dturl]]]
      set lng [dict get $res responseData language]
      if { $lng == $lngto } {
         putserv "PRIVMSG $chan :\002Erreur\002 Traduction de $lng (détecté) vers $lngto"
         return 0
      }   
      set trurl "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=$lng%7C$lngto&$text"
      putlog $trurl
      set res [::json::json2dict [::http::data [::http::geturl $trurl]]]
      putlog $res
      putserv "PRIVMSG $chan :Langue detectée: $lng"
      set substitution {[format %c \1]}
      set trtext [subst [regsub -all -- {&#(\d+);} [dict get $res responseData translatedText] $substitution]]
      putserv "PRIVMSG $chan :$trtext"
   }
   
}

putlog "\002$::gTranslator::versionName $::gTranslator::versionNum\002 loaded"


or if its possible to convert to use a Google Translation API key

https://cloud.google.com/translate

Thanks in advanced Wink


EDIT 1 : getting this error when using the script
Code:

<Tech> [05:41:37] Tcl error [gTranslator::translate]: unexpected '<' in TOP mode


Hope this helps
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CrazyCat
Owner


Joined: 13 Jan 2002
Posts: 848
Location: France

PostPosted: Sun Mar 07, 2021 5:33 am    Post subject: Reply with quote

I don't work anymore on this script since google stopped providing a free API, but I'll have an eye on it.

short points to see:
url is now https://translation.googleapis.com/language/translate/v2
query must use POST
default return format is html, you may have to add format=text to get the translation in plain-text

I can only do a theorical development because google force to give a payment card even for a free trial and I won't do that.

If you have an api key and trust me, you can share it with and then I can do an evolution of this old script Smile
_________________
https://www.eggdrop.fr
Offer me a coffee - Do not ask me help in PM, we are a community.
Back to top
View user's profile Send private message Visit poster's website
ComputerTech
Master


Joined: 22 Feb 2020
Posts: 374

PostPosted: Sun Mar 07, 2021 10:26 am    Post subject: Reply with quote

Mmmm, it's not possible to somehow input text into

https://translate.google.com/

Is that how the old script did?

and if i had a API key, you'd have it straight away Wink (trust you 100%)


EDIT

Was speaking to a Msl scripter and he says the way he uses his google translate script is using this.

Code:

<westor> https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=URL_ENCODE(URL)
<westor> https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q=URL_ENCODE(TEXT)


Quote:

<ComputerTech> so no api key like the youtube one then eh?
<westor> yeah it doesn't require one
<westor> just send some Headers
<westor> a valid User-Agent is required


Hope this helps Wink

EDIT 2

found this from bitbot, that might be of use

https://github.com/jesopo/bitbot/blob/master/modules/translate.py

maybe could do similiar for your Script CrazyCat Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CrazyCat
Owner


Joined: 13 Jan 2002
Posts: 848
Location: France

PostPosted: Tue Mar 09, 2021 7:14 am    Post subject: Reply with quote

The https://translate.googleapis.com/translate_a/single url works but the rendered json is unreadable as there are no key.

I try to do a reusable script, not a fullish txt parser.

Here is the json I get when asking the translation of bonjour la france:
Code:
[[["Hello France","bonjour la france",null,null,1]
]
,null,"fr",null,null,null,1.0,[]
,[["fr"]
,null,[1.0]
,["fr"]
]
]

And no, I won't treat it as a simple string.

Converted into dict, no more useable:
Code:
{{{Hello France} {bonjour la france} null null 1}} null fr null null null 1.0 {} {fr null 1.0 fr}

_________________
https://www.eggdrop.fr
Offer me a coffee - Do not ask me help in PM, we are a community.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Script Support & Releases All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber