08/03/2022, 08:02
(This post was last modified: 08/03/2022, 08:55 by CrazyCat.
Edit Reason: It doesn't work. I tried to contact the author without success.
)
It doesn't work. I tried to contact the author without success.
tcl
#spotify.tcl
# by Destiny #Rencontres
# EpiKnet
#############################
namespace eval ::spotify {}
# Activer le contrôle de flood ? (0 = non / 1 = oui)
set ::spotify::antiflood 1
# Seuil de déclenchement de l'antiflood.
# Exemple : "4:180" = 4 commandes maximum en 180 secondes; les suivantes seront
# ignorées.
set ::spotify::flood_threshold "6:180"
# Intervalle de temps minimum entre l'affichage de 2 messages
# avertissant que l'antiflood a été déclenché (ne réglez pas
# cette valeur trop bas afin de ne pas être floodé par les messages
# d'avertissement de l'antiflood...)
set ::spotify::antiflood_msg_interval 30
lassign [split $::spotify::flood_threshold ":"] ::spotify::max_instances ::spotify::instance_length
proc ::spotify::adapt_time_resolution {duration {short 0}} {
set duration [::tcl::string::trimleft $duration 0]
set milliseconds [::tcl::string::range $duration end-2 end]
set duration [::tcl::string::range $duration 0 end-3]
if { $duration eq "" } {
set duration 0
}
set days [expr {abs($duration / 86400)}]
set hours [expr {abs(($duration % 86400) / 3600)}]
set minutes [expr {abs(($duration % 3600) / 60)}]
set seconds [expr {$duration % 60}]
set valid_units 0
set counter 1
set output {}
foreach unit [list $days $hours $minutes $seconds] {
if {
($unit > 0)
|| (($counter == 4) && (!$valid_units))
} then {
switch -- $counter {
1 {
if { !$short } {
lappend output "$unit [plural $unit "jour" "jours"]"
} else {
lappend output "${unit}j"
}
}
2 {
if { !$short } {
lappend output "$unit [plural $unit "heure" "heures"]"
} else {
lappend output "${unit}h"
}
}
3 {
if { !$short } {
lappend output "$unit [plural $unit "minute" "minutes"]"
} else {
lappend output "${unit}mn"
}
}
4 {
if { [::tcl::string::trimright $milliseconds 0] == "" } {
set show_ms 0
} else {
set show_ms 1
}
set milliseconds "[string repeat 0 [expr {3-[::tcl::string::length $milliseconds]}]]$milliseconds"
if { $show_ms } {
if { !$short } {
lappend output "${unit}.$milliseconds [plural "${unit}$milliseconds" "seconde" "secondes"]"
} else {
lappend output "${unit}.${milliseconds}s"
}
} else {
if { !$short } {
lappend output "$unit [plural $unit "seconde" "secondes"]"
} else {
lappend output "${unit}s"
}
}
}
}
incr valid_units
}
incr counter
}
if { !$short } {
if { $valid_units > 1 } {
# Texte "et"
set output [linsert $output end-1 "et"]
}
return [join $output]
} else {
return [join $output ""]
}
}
proc ::spotify::announce {nick uhost hand chan text} {
if { ($::spotify::antiflood) && ([::spotify::antiflood $chan]) } {
return
} else {
if {[channel get $chan spotify] != 1 } { return }
if {[regexp -- {https://play.spotify.com/track/([A-Za-z0-9]+)} $text match spotify]} {
set html [spotify:getdata "http://evolu7ion.fr/spotify_api_http_bridge.php?$spotify"]
set resulting_dict [::json::json2dict $html]
set album [::tcl::dict::get $resulting_dict album name]
set artist [::tcl::dict::get {*}[::tcl::dict::get $resulting_dict artists] name]
set title [::tcl::dict::get $resulting_dict name]
set duration_ms [::tcl::dict::get $resulting_dict duration_ms]
set duration [adapt_time_resolution $duration_ms 1]
# regexp -nocase {<meta property="og:description" content="(.*?), a song by\s(.*?)\son Spotify." />} $html match title artist
if {![info exists title]} {
putlog "\[spotify\] unable to get title/artist info from: http://open.spotify.com/track/$spotify"
} else {
puthelp "privmsg $chan :\002\00300,10Spotify:\017 (\002Artiste: \002\00314$artist) \00307-\003 (\002Titre: \002\00314$title) \00307-\003 (\002Album: \002\00314$album) \00307-\003 (\002Durée: \002\00314$duration)"
}
}
}
}
proc ::spotify::getdata {data} {
::http::config -useragent "Mozilla/5.0 (X11; U; Linux i686; ru-RU; rv:1.8.1) Gecko/2006101023 Firefox/2.0"
set url [::http::geturl $data -timeout 5000]
set source_code [::http::data $url]
::http::cleanup $source_code
return $source_code
}
package require http
package require json
setudef flag spotify
bind pubm -|- "% *https://*.spotify.com/track/*" ::spotify::announce
proc ::spotify::antiflood {chan} {
set chan_hash [md5 $chan]
# l'antiflood est dans un statut neutre, on l'initialise
if { ![::tcl::info::exists ::spotify::instance($chan_hash)] } {
set ::spotify::instance($chan_hash) 0
set ::spotify::antiflood_msg($chan_hash) 0
}
if { $::spotify::instance($chan_hash) >= $::spotify::max_instances } {
if { !$::spotify::antiflood_msg($chan_hash) } {
set ::spotify::antiflood_msg($chan_hash) 2
puthelp "PRIVMSG $chan :Contrôle de flood activé pour la requête \002Spotify\002 : pas plus de $::spotify::max_instances requête(s) toutes les $::spotify::instance_length secondes."
if { [set msgresettimer [::spotify::utimerexists "::spotify::antiflood_msg_reset $chan_hash"]] ne ""} { killutimer $msgresettimer }
utimer $::spotify::antiflood_msg_interval [list ::spotify::antiflood_msg_reset $chan_hash]
} elseif { $::spotify::antiflood_msg($chan_hash) == 1 } {
set ::spotify::antiflood_msg($chan_hash) 2
puthelp "PRIVMSG $chan :Le contrôle de flood est toujours actif, merci de patienter."
if { [set msgresettimer [::spotify::utimerexists "::spotify::antiflood_msg_reset $chan_hash"]] ne ""} { killutimer $msgresettimer }
utimer $::spotify::antiflood_msg_interval [list ::spotify::antiflood_msg_reset $chan_hash]
}
return "1"
} else {
incr ::spotify::instance($chan_hash)
utimer $::spotify::instance_length [list ::spotify::antiflood_close_instance $chan_hash]
return "0"
}
}
proc ::spotify::antiflood_close_instance {chan_hash} {
incr ::spotify::instance($chan_hash) -1
# si le nombre d'instances retombe à 0, on efface les variables instance et
# antiflood_msg afin de ne pas encombrer la mémoire inutilement
if { $::spotify::instance($chan_hash) == 0 } {
unset ::spotify::instance($chan_hash)
unset ::spotify::antiflood_msg($chan_hash)
# le nombre d'instances est retombé en dessous du seuil critique, on
# réinitialise antiflood_msg
} else {
set ::spotify::antiflood_msg($chan_hash) 0
if { [set msgresettimer [::spotify::utimerexists "::spotify::antiflood_msg_reset [set chan_hash]"]] ne ""} { killutimer $msgresettimer }
}
return
}
proc ::spotify::antiflood_msg_reset {chan_hash} {
set ::spotify::antiflood_msg($chan_hash) 1
return
}
proc ::spotify::utimerexists {command} {
foreach timer_ [utimers] {
if { ![::tcl::string::compare $command [lindex $timer_ 1]] } then {
return [lindex $timer_ 2]
}
}
return
}
putlog "spotify.tcl pour #Rencontres sur irc.epiknet.org"