this is what i have in use
Code: Select all
namespace eval news {
set news(key) "MY-KEY"
set news(url) "https://newsapi.org/v2/top-headlines?sources=%source&sortBy=latest&apiKey=%key"
set news(timeout) 5000
package require http
package require json
package require tls
proc tls:socket args {
set opts [lrange $args 0 end-2]
set host [lindex $args end-1]
set port [lindex $args end]
::tls::socket -servername $host {*}$opts $host $port
}
bind pub * !news [namespace current]::fetch
proc fetch {nick uhost hand chan text} {
variable news
if {[scan $text {%s} source] != 1} {
puthelp "PRIVMSG $chan :Usage: !news <source> | Pick one from https://newsapi.org/sources"
return
}
::http::register https 443 [namespace current]::tls:socket
set url [string map [list "%source" "$source" "%key" "$news(key)"] $news(url)]
set token [::http::geturl $url -timeout $news(timeout)]
set data [::http::data $token]
set json [::json::json2dict $data]
::http::cleanup $token
::http::unregister https
if {[lsearch [dict get $json] "articles"] > -1} {
foreach item [dict get $json articles] {
dict with item {
puthelp "PRIVMSG $chan: Ttitle: [encoding convertfrom $title] | desc: [encoding convertfrom $description] | url: $url"
}
}
} else {
puthelp "PRIVMSG $chan :The specified source is invalid, pick one from https://newsapi.org/sources"
# To see exact error message uncomment next line
#puthelp "PRIVMSG $chan: Error: [dict get $json message]"
}
}
}