Formatting of code : You can (and must) use [ tcl ] an [ /tcl ] tags (without spaces in tags) to format your TCL codes and have syntax coloration x


DictionaryAPI.tcl - latest version
#1
Hi, need help on this.. thanks


<@ mabrook> .define
<GAWAIN> DictionaryAPI: flag 'definiciones' not set on '#mychannel'


BOT DCC:   <GAWAIN> [11:44:40] DictionaryAPI: flag 'definiciones' not set on '#mychannel'





Code:
#########################################################################################
# Name            Dictionary-API
# Description    Uses Dictionary v2 API to search and Get word definitions
# auteur        ZarTek <Deffice@GMail.Com>
# version        1.2
#########################################################################################

if { [::tcl::info::commands ::DictionaryAPI::unload] eq "::DictionaryAPI::unload" } { ::DictionaryAPI::unload }
if { [package vcompare [regexp -inline {^[[:digit:]\.]+} $::version] 1.6.20] == -1 } { putloglev o * "\00304\[DictionaryAPI - erreur\]\003 La version de votre Eggdrop est\00304 ${::version}\003; DictionaryAPI ne fonctionnera correctement que sur les Eggdrops version 1.6.20 ou supérieure." ; return }
if { [::tcl::info::tclversion] < 8.5 } { putloglev o * "\00304\[DictionaryAPI - erreur\]\003 DictionaryAPI nécessite que Tcl 8.5 (ou plus) soit installé pour fonctionner. Votre version actuelle de Tcl est\00304 ${::tcl_version}\003." ; return }
if { [catch { package require tls}] } { putloglev o * "\00304\[DictionaryAPI - erreur\]\003 DictionaryAPI nécessite le package tls 1.7 (ou plus) pour fonctionner. Le chargement du script a été annulé." ; return }
if { [catch { package require http}] } { putloglev o * "\00304\[DictionaryAPI - erreur\]\003 DictionaryAPI nécessite le package http 2.9 (ou plus) pour fonctionner. Le chargement du script a été annulé." ; return }
if { [catch { package require json}] } { putloglev o * "\00304\[DictionaryAPI - erreur\]\003 DictionaryAPI nécessite le package json 1.3 (ou plus) pour fonctionner. Le chargement du script a été annulé." ; return }
namespace eval ::DictionaryAPI {
######################################################
## Flag
    setudef flag definiciones
######################################################
     ###############################################################################
### Configuration
###############################################################################

    # Commande publique
    variable public_cmd                ".define .Definiciones"

    # Autorisations pour la commande publique
    variable public_cmd_auth        "-"
    
    # Langue de sortie
    # en,hi,es,fr,ja,ru,de,it,ko,pt-br,ar,tr
    variable Lang                    "en"

    # Annonce prefix-> devant les annonce irc
    variable Annonce_Prefix            "\00301,00DictionaryAPI\003> "

    ##
    # ANNONCES "BLOCK" STYLE VARIABLES
    # Add colors etc
    ##
    # "\${DICT_DEFINITION}"    : definition
    # "\${DICT_NUMBER}"        : Le numero de la definition
    # "\${DICT_WORD}"        : le mot en question
    # "\${DICT_TYPE}"        : partOfSpeech
    # "\${DICT_EXAMPLE}"    : Exemple de la definition
    # "\${DICT_SYNONYMS}"    : Les synonimes de la definition
    # "\${DICT_ANTONYMS}"    : Les antonimes de la definition
    # "\${DICT_PHONETICS}"    : DICT_PHONETICS de la definition
    # "\n"                    : Retour a la ligne, nouvelle phrase??
    ###
    variable BLOCK_DEFINITION    " > \${DICT_DEFINITION}"
    variable BLOCK_NUMBER        " \${DICT_NUMBER} "
    variable BLOCK_WORD            "\002\00301,00\"\${DICT_WORD}\"\003\002"
    variable BLOCK_TYPE            " - \${DICT_TYPE}"
    variable BLOCK_EXAMPLE        " > \00302\"\${DICT_EXAMPLE}\"\003"
    variable BLOCK_SYNONYMS        " > (\${DICT_SYNONYMS})"
    variable BLOCK_ANTONYMS        " != \${DICT_ANTONYMS}"
    variable BLOCK_PHONETICS    " (\${DICT_PHONETICS})"
    ##
    # ANNONCES VARIABLES POSITION (if exists)
    ##
    # "\${BLOCK_DEFINITION}"
    # "\${BLOCK_NUMBER}"
    # "\${BLOCK_WORD}"
    # "\${BLOCK_TYPE}"
    # "\${BLOCK_EXAMPLE}"
    # "\${BLOCK_SYNONYMS}"
    # "\${BLOCK_ANTONYMS}"
    # "\${BLOCK_PHONETICS}"
    # "\n"                    : Retour a la ligne, nouvelle phrase
    ###
    # Multi exemple:
    
    #variable Annonce_Show            "\${DICT_WORD}\${DICT_PHONETICS}\${DICT_TYPE}\${DICT_SYNONYMS}\${DICT_ANTONYMS}\${DICT_NUMBER}\${DICT_DEFINITION}\${DICT_EXAMPLE}"
    #variable Annonce_Show            "\${DICT_WORD}\${DICT_PHONETICS}\${DICT_SYNONYMS}\${DICT_ANTONYMS}\${DICT_NUMBER}\${DICT_DEFINITION}\${DICT_EXAMPLE}"
    variable Annonce_Show            "\${DICT_NUMBER}\${DICT_WORD}\${DICT_TYPE}\n\${DICT_DEFINITION}\n\${DICT_EXAMPLE}"

    # Annonce notfound
    ## "\${WORD_SEARCH}"                    : word not found
    ## "\${URL_Link}"                        : url not found
    
    #variable Annonce_notfound        "No se encontraron definiciones para 6\${WORD_SEARCH}\002."
    variable Annonce_notfound        "No se encontraron definiciones para 6\${WORD_SEARCH}\002. (\${URL_Link})"
    
    # Nombre de resultats maximun
    variable max_annonce_default    2
    
    # Nombre de resultats maximun defini par l'utilisateur
    variable max_annonce_user        10
    
    # Après combien de secondes décide-t-on que le site web utilisé par le script
    # pour afficher les définitions est offline (ou trop lent) en l'absence de
    # réponse de sa part ?
    variable HTTP_TIMEOUT    10

    # USE HTTPS (1) OR HTTP (0)
    variable USE_HTTP_SSL    1
    # URL (n'y touchez pas à moins d'avoir une bonne raison de le faire)
    variable HTTP_URL_API    "api.dictionaryapi.dev/api/v2/entries"

    # User agent for http
    variable HTTP_USERAGENT    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"
###############################################################################
### Fin de la configuration
###############################################################################


     #############################################################################
    ### Initialisation
     #############################################################################
    variable scriptname        "DictionaryAPI"
    variable scriptauteur    "ZarTek <Deffice@GMail.Com>"
    variable version        "1.2"
    if { $USE_HTTP_SSL == 1 } {
        variable HTTP_URL_API        "https://${HTTP_URL_API}"
    } else {
        variable HTTP_URL_API        "http://${HTTP_URL_API}"
    }
    setudef flag definiciones
    proc unload {args} {
        putlog "Désallocation des ressources de ${::DictionaryAPI::scriptname}..."
        foreach binding [lsearch -inline -all -regexp [binds *[set ns [::tcl::string::range [namespace current] 2 end]]*] " \{?(::)?$ns"] {
            unbind [lindex $binding 0] [lindex $binding 1] [lindex $binding 2] [lindex $binding 4]
        }
        foreach running_utimer [utimers] {
            if { [::tcl::string::match "*[namespace current]::*" [lindex $running_utimer 1]] } { killutimer [lindex $running_utimer 2] }
        }
        namespace delete ::DictionaryAPI
    }
}
proc ::DictionaryAPI::GetURL { URL_Link } {
    if { $::DictionaryAPI::USE_HTTP_SSL == 1 } { ::http::register https 443 [list ::tls::socket -autoservername true] }
    # on modifie l'urlencoding car dicoreverso.net ne comprend que l'utf-8 dans ses URLs.
    array set httpconfig [::http::config]
    ::http::config -urlencoding utf-8 -useragent $::DictionaryAPI::HTTP_USERAGENT
    # on restaure l'urlencoding comme il était avant qu'on y touche
    ::http::config -urlencoding $httpconfig(-urlencoding)
    if { [catch { set URL_TOKEN [::http::geturl $URL_Link -timeout [expr $::DictionaryAPI::HTTP_TIMEOUT * 1000]] } ERR] } {
        putlog    "::DictionaryAPI::GetURL \00314La connexion à \00312\037[set URL_Link]\037\003\00314 n'a pas pu être établie. Il est possible que le site rencontre un problème technique.\003"
        putlog    "::DictionaryAPI::ERROR $ERR"
        if { $::DictionaryAPI::USE_HTTP_SSL == 1 } { ::http::unregister https }
        return 0
    }
    
        # on extrait la partie qui nous intéresse et sur laquelle on va travailler
        set URL_DATA    [::http::data $URL_TOKEN]
        set URL_DATA    [encoding convertfrom utf-8 $URL_DATA]
        ::http::cleanup $URL_TOKEN
        if { $::DictionaryAPI::USE_HTTP_SSL == 1 } { ::http::unregister https }
        return $URL_DATA;
    
}

###############################################################################
### Procédure principale
###############################################################################
proc ::DictionaryAPI::SetBlock { WHAT VALUE } {
    set DICT_$WHAT $VALUE
        set ::DictionaryAPI::DICT_$WHAT [subst [subst $[join ::DictionaryAPI::BLOCK_$WHAT]]]
}
proc ::DictionaryAPI::Search {nick host hand chan arg} {
    if { ![channel get $chan definiciones] } {
        set MSG    "DictionaryAPI: flag 'definiciones' not set on '$chan'"
        putlog $MSG
        putserv "PRIVMSG $chan :$MSG"
        return
    }
    set WORD_SEARCH        [stripcodes * $arg]
    if {$WORD_SEARCH == ""} {
        putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}HELP    : [join $::DictionaryAPI::public_cmd "|"] <word> "
        putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}LANG    : \[-lang=<en,hi,es,fr,ja,ru,de,it,ko,pt-br,ar,tr>\] | default lang: $::DictionaryAPI::Lang "
        putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}LIMIT   : \[-limit=<1-$::DictionaryAPI::max_annonce_user>\] | default limit: $::DictionaryAPI::max_annonce_default "
        putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}EXAMPLE : [lindex $::DictionaryAPI::public_cmd 0] test -lang=ru -limit=6"
        putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}HELP    : [join $::DictionaryAPI::public_cmd "|"] SetLang <en,hi,es,fr,ja,ru,de,it,ko,pt-br,ar,tr> | Set lang per default"
        putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}EXAMPLE : [lindex $::DictionaryAPI::public_cmd 0] SetLang es"
        return
    }
    if {[string match -nocase [lindex $arg 0] "SetLang"] } {
        if {[lindex $arg 1] != ""} {
            set ::DictionaryAPI::Lang [lindex $arg 1]
            putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix} Default lang is now : $::DictionaryAPI::Lang"
        } else {
            putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix} Current lang is : $::DictionaryAPI::Lang"
        }
        
        return
    }
    set RE {-limit[\s|=](\S+)}
    if { [regexp -nocase -- $RE $WORD_SEARCH -> limit]} {
        regsub -nocase -- $RE $WORD_SEARCH {} WORD_SEARCH
        set WORD_SEARCH        [string trim $WORD_SEARCH]
        if {$limit > $::DictionaryAPI::max_annonce_user} {
            putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix} limit max is $::DictionaryAPI::max_annonce_user"
            set limit $::DictionaryAPI::max_annonce_user
        }
    } else {
        set limit $::DictionaryAPI::max_annonce_default
    }
    set RE {-lang[\s|=](\S+)}
    if { [regexp -nocase -- $RE $WORD_SEARCH -> lang]} {
        regsub -nocase -- $RE $WORD_SEARCH {} WORD_SEARCH
        set WORD_SEARCH        [string trim $WORD_SEARCH]
        set URL_Link        "${::DictionaryAPI::HTTP_URL_API}/${lang}/${WORD_SEARCH}"
    } else {
        set URL_Link        "${::DictionaryAPI::HTTP_URL_API}/${::DictionaryAPI::Lang}/${WORD_SEARCH}"
    }
    

    set URL_DATA        [::DictionaryAPI::GetURL $URL_Link]
    
    foreach { PARENT } [::json::json2dict $URL_DATA] {
        if { $PARENT == "title" } {
            putserv "PRIVMSG $chan :[subst $::DictionaryAPI::Annonce_notfound]"
            return
        }
        # init/reset vars
        variable DICT_DEFINITION    ""
        variable DICT_WORD            ""
        variable DICT_TYPE            ""
        variable DICT_NUMBER        0
        variable DICT_EXAMPLE        ""
        variable DICT_SYNONYMS        ""
        variable DICT_ANTONYMS        ""
        variable DICT_PHONETICS        ""
        set SUBCAT                                    [dict get $PARENT]
        set TMP_phonetics                            [dict get $SUBCAT phonetics]
        ::DictionaryAPI::SetBlock WORD                        [dict get $SUBCAT word]
        if {$TMP_phonetics != "{}"} {
            ::DictionaryAPI::SetBlock PHONETICS                $phonetics
            unset TMP_phonetics
        }
        set SUBMEANINGS                                [dict get $SUBCAT meanings]
        foreach { ENFANT } [dict get $SUBCAT meanings] {
            set TMP_partOfSpeech    [dict get $ENFANT partOfSpeech]
            if {$TMP_partOfSpeech != "{}"} {
                ::DictionaryAPI::SetBlock TYPE                $TMP_partOfSpeech
                unset TMP_partOfSpeech
            }
            foreach { SUBDEFINITION } [dict get $ENFANT definitions] {
                ::DictionaryAPI::SetBlock DEFINITION        [dict get $SUBDEFINITION definition]
                if {
                    [dict exists $SUBDEFINITION synonyms] &&    \
                    [dict get $SUBDEFINITION synonyms] != ""
                } {
                        ::DictionaryAPI::SetBlock SYNONYMS    [join [dict get $SUBDEFINITION synonyms] ", "]
                    }
                if { [dict exists $SUBDEFINITION example] } {
                    ::DictionaryAPI::SetBlock EXAMPLE        [dict get $SUBDEFINITION example]
                }
                ::DictionaryAPI::SetBlock NUMBER            [expr $DICT_NUMBER+1]
                foreach Annonce [split [subst ${::DictionaryAPI::Annonce_Show}] "\n"] {
                    putserv "PRIVMSG $chan :${::DictionaryAPI::Annonce_Prefix}$Annonce"
                }
                if {$limit == $DICT_NUMBER} { return }
            }
        }
    }
}
  ###############################################################################
### Binds
###############################################################################
foreach b $::DictionaryAPI::public_cmd {
    bind pub $::DictionaryAPI::public_cmd_auth $b ::DictionaryAPI::Search
}
bind evnt - prerehash ::DictionaryAPI::unload


putlog "DictionaryAPI $::DictionaryAPI::version by $::DictionaryAPI::scriptauteur loaded"
  Reply


Messages In This Thread
DictionaryAPI.tcl - latest version - by mabrook - 21/08/2021, 13:49
RE: DictionaryAPI.tcl - V1.2.3 - by ZarTek - 24/08/2021, 19:23

Forum Jump:


Users browsing this thread: 1 Guest(s)