This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Converting CrazyCat's Translation Script into using API key

Support & discussion of released scripts, and announcements of new releases.
Post Reply
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Converting CrazyCat's Translation Script into using API key

Post by ComputerTech »

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: Select all

#
# 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: Select all

<Tech> [05:41:37] Tcl error [gTranslator::translate]: unexpected '<' in TOP mode
Hope this helps
User avatar
CrazyCat
Revered One
Posts: 1305
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

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/lang ... anslate/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 :)
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

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: Select all

<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)
<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/m ... anslate.py

maybe could do similiar for your Script CrazyCat :)
User avatar
CrazyCat
Revered One
Posts: 1305
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

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: Select all

[[["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: Select all

{{{Hello France} {bonjour la france} null null 1}} null fr null null null 1.0 {} {fr null 1.0 fr}
Post Reply