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.

tcl script to call data from an api

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
s
sirducu
Voice
Posts: 4
Joined: Sat Jun 18, 2022 6:33 pm

tcl script to call data from an api

Post by sirducu »

Hello guys,

I am looking for a script that can call data from an API, and post to a certain channel, I don't need it to post on multiple channels.
The API doesn't need a key to call from it.
If anyone can help, that would be awesome, if not, please point me to a place where I can learn TCL scripting.

Thank you!
User avatar
CrazyCat
Revered One
Posts: 1306
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Re: tcl script to call data from an api

Post by CrazyCat »

Without knowing which API you want to use and wich datas in it, we can't help you.
User avatar
CrazyCat
Revered One
Posts: 1306
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Re: tcl script to call data from an api

Post by CrazyCat »

Ok, let me formulate it in another way: I need the url of the API, and I've to know which datas you want to retrieve from the json.
s
sirducu
Voice
Posts: 4
Joined: Sat Jun 18, 2022 6:33 pm

Re: tcl script to call data from an api

Post by sirducu »

I kind of made it working :D
with a little help from this forum and the internet.. but maybe you guys can help me make it a little better ..
here is the code .
package require http
package require json
putlog "newsbot loaded"
proc getinfo { url } {
	for { set i 1 } { $i <= 5 } { incr i } {
        	set rawpage [http::data [http::geturl "$url" -timeout 50000]]
        	if {[string length rawpage] > 0} { break }
        }
        if {[string length $rawpage] == 0} { error "newsapi returned ZERO no data :( or we couldnt connect properly" }
	set json [json::many-json2dict $rawpage]
	return $json

}

proc newsb {min hour day month dow} {
    set chan "channel"
    set url "YOURURL"
    set news [getinfo $url]

    for {set i 0} {$i < 1} {incr i} {
        set title [encoding convertfrom [lindex $news 0 $i 1]]
	    set sursa [lindex $news 0 $i 3]
	    set link [lindex $news 0 $i 5]
    	    if { $i == 0 } {
	            set output "Top stories : $title - $sursa (@$link)"
    	    } else {
       		    set output "$title - $sursa (@ $link)"
    	}
        puthelp "PRIVMSG $chan :$output"
	}

}

bind cron -"*/15 * * * *" newsb
Moderation: added [ tcl ] and [ /tcl ] tags
User avatar
CrazyCat
Revered One
Posts: 1306
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Re: tcl script to call data from an api

Post by CrazyCat »

Here is a small script:
namespace eval radu {

   variable raduurl "http://radu.ovh:8080/crypto"
   variable raduchan "#test"
   
   package require http
   package require json
   
   proc feedget {url} {
      set tok [::http::geturl $::radu::raduurl -timeout 1000]
      upvar 0 $tok state
      if {$state(status) ne "ok"} {
         putlog "HTTP Error : $state(url) made $state(status) error"
         return "error"
      }
      set datas [::http::data $tok]
      ::http::cleanup $tok
      return [json2dict $datas]
   }
   
   proc newsb {min hour day month dow} {
      set datas [::radu::feedget $::radu::raduurl]
      if {$datas eq "error" } {
         return
      }
      set datas [::json::json2dict $datas]
      foreach e $datas {
         putserv "PRIVMSG $::radu::raduurl :[dict get $e titlu] ([dict get $e sursa])
      }
      
   }

   bind cron - "*/15 * * * *" ::radu::newsb
}
It can be improved to use asynchronous callback so the script won't be blocked if the timeout delay is increased
Post Reply