| View previous topic :: View next topic |
| Author |
Message |
ComputerTech Master

Joined: 22 Feb 2020 Posts: 393
|
Posted: Sat Apr 10, 2021 12:15 am Post subject: Using format |
|
|
Hey, so i am trying to make it possible for this
| Code: |
!w los+angeles, United States
|
i need + between city name and , at the end (before country)
| Code: |
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?
Thanks in advanced _________________ ComputerTech |
|
| Back to top |
|
 |
DasBrain Voice
Joined: 08 Apr 2021 Posts: 12
|
Posted: Sat Apr 10, 2021 8:55 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
ComputerTech Master

Joined: 22 Feb 2020 Posts: 393
|
Posted: Sat Apr 10, 2021 10:30 am Post subject: |
|
|
First Hiya DasBrain
Second: no i want fhe script to have + between city name and , before country name (if specified)
something like this
| Code: |
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
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  _________________ ComputerTech |
|
| Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1032 Location: France
|
Posted: Sat Apr 10, 2021 6:04 pm Post subject: |
|
|
| 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. _________________ https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|