Last active
January 11, 2018 18:53
-
-
Save happyhater/5a5de7194f0f928044adeee2f5f5aacd to your computer and use it in GitHub Desktop.
Afisarea pretului curent al monede Bitcoin in format TCL pentru Eggdrop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# ZmEu - Bitcoin Currency Price TCL. | |
# | |
# 11.01.2018 - first release. (not tested) | |
# | |
# USE AT YOUR OWN RISK! | |
# | |
## | |
package require Tcl 8.5 | |
package require http | |
package require tls | |
package require json | |
http::register https 443 [list ::tls::socket -require 0 -request 1] | |
http::config -useragent "Mozilla/5.0 (Windows NT 6.3, Win64, x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2114.2 Safari/537.36" | |
bind pub - !bitcoin pret_bitcoin | |
proc s:wget { url } { | |
catch {set token [http::geturl $url -binary 1 -timeout 10000]} error | |
if {![string match -nocase "::http::*" $error]} { | |
putserv "PRIVMSG $chan: Error: [string totitle [string map {"\n" " | "} $error]] \( $url \)" | |
s:debug "Error: [string totitle [string map {"\n" " | "} $error]] \( $url \)" | |
return 0 | |
} | |
if {![string equal -nocase [::http::status $token] "ok"]} { | |
putserv "PRIVMSG $chan: Eroare HTTP: [string totitle [::http::status $token]] \( $url \)" | |
s:debug "Eroare HTTP: [string totitle [::http::status $token]] \( $url \)" | |
http::cleanup $token | |
return 0 | |
} | |
if {[string match "*[http::ncode $token]*" "303|302|301" ]} { | |
upvar #0 $token state | |
foreach {name value} $state(meta) { | |
if {[regexp -nocase ^location$ $name]} { | |
if {![string match "http*" $value]} { | |
if {![string match "/" [string index $value 0]]} { | |
set value "[join [lrange [split $url "/"] 0 2] "/"]/$value" | |
} else { | |
set value "[join [lrange [split $url "/"] 0 2] "/"]$value" | |
} | |
} | |
s:wget $value | |
return | |
} | |
} | |
} | |
if {[string match 4* [http::ncode $token]] || [string match 5* [http::ncode $token]]} { | |
putserv "PRIVMSG $chan: Eroare, nu ai resurse HTTP: [http::ncode $token] \( $url \)" | |
s:debug "Eroare, nu ai resurse HTTP: [http::ncode $token] \( $url \)" | |
return 0 | |
} | |
set data [http::data $token] | |
http::cleanup $token | |
return $data | |
} | |
proc pret_bitcoin {nick uhand handle chan input} { | |
# if {[llength $input]==0} { | |
# putnow "PRIVMSG $chan :!bitcoin pret" | |
# } else { | |
set bitstamphttp [s:wget https://www.bitstamp.net/api/ticker/ ] | |
set bitstamp [json::json2dict $bitstamphttp] | |
set btclast [dict get $bitstamp last] | |
set btcehttp [s:wget https://btc-e.com/api/2/btc_usd/ticker ] | |
set btce [json::json2dict $btcehttp] | |
set btbtclast [dict get [dict get $btce ticker] last] | |
set usdavglast [expr {($btclast + $btbtclast) / 2}] | |
set bittrexusdlast [format "%.4f" [expr {$usdavglast}]] | |
putnow "PRIVMSG $chan :Bitcoin pret: $$bittrexusdlast" | |
# } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment