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.

Using format

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Using format

Post by ComputerTech »

Hey, so i am trying to make it possible for this

Code: Select all

!w los+angeles, United States
i need + between city name and , at the end (before country)

Code: Select all


package require json
package require tls
package require http

set ctw(api) "YOUR-API-KEY-HERE"

bind PUB - "!w" weather:call

proc weather:call {nick host hand chan text} {
   global ctw
  http::register https 443 [list ::tls::socket]
  set uargs [http::formatQuery q [lindex [split $text] 0] units=metric appid=$ctw(api)]
         set data [http::data [http::geturl "http://api.openweathermap.org/data/2.5/weather?" -query $uargs -timeout 10000]]
  http::cleanup $data
  http::unregister https
         set data2 [::json::json2dict $data]
         set name [dict get $data2 "name"]
         set sys [dict get $data2 "sys"]
         set country [dict get $sys "country"]
         set main [dict get $data2 "main"]
         set temp [dict get $main "temp"]
         set humidity [dict get $main "humidity"]
         set wind [dict get $data2 "wind"]
        set speed [dict get $wind "speed"]
        set current [dict get [lindex [dict get $data2 weather] 0] description]
                    putserv "PRIVMSG $chan :\[\00309Weather\003\] ${name}, $country | ${temp}$ctw(met2) | ${speed}$ctw(met3) | $current | ${humidity}%"
}
so i was trying formatQuery, am i close to right? :lol:

Thanks in advanced
ComputerTech
D
DasBrain
Voice
Posts: 13
Joined: Thu Apr 08, 2021 12:31 pm

Post by DasBrain »

1) What do you want your trigger to look like?

Really just "!w los+angeles, United States"?

2) The API is described here:
https://openweathermap.org/current

Use [http::geturl http://api.openweathermap.org/data/2.5/weather?[http::formatQuery ...]], as the API seems to look like it uses GET requests.

3) Don't cleanup $data, cleanup the result of [http::geturl]

4) Use callbacks - [http::geturl -command]

5) DON'T UNREGISTER THE https PROTOCOL.
It will break other scripts.
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

First Hiya DasBrain

Second: no i want fhe script to have + between city name and , before country name (if specified)

something like this

Code: Select all

set ctw(location) [join $text ,]
but somehow keep + between cities and then , at the end, dunno how the best way to specify the end of the city name, but all ideas are welcome :P

EDIT

and yeah i know you're suppose to put , between city and country names, although i found out by using + between city name, and , at the end, works better :P
ComputerTech
User avatar
CrazyCat
Revered One
Posts: 1216
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

DasBrain wrote:5) DON'T UNREGISTER THE https PROTOCOL.
It will break other scripts.
In a perfect world, every script using https must register https protocol on demand and unregister it when finishing with http.
You instanciate http each time, so you register https each time. And unregister when closing your connection.

If all scripts were done using this small rule, everything will be ok.
Post Reply