egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using format

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
ComputerTech
Master


Joined: 22 Feb 2020
Posts: 374

PostPosted: Sat Apr 10, 2021 12:15 am    Post subject: Using format Reply with quote

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? Laughing

Thanks in advanced
_________________
ComputerTech
Back to top
View user's profile Send private message Send e-mail Visit poster's website
DasBrain
Voice


Joined: 08 Apr 2021
Posts: 12

PostPosted: Sat Apr 10, 2021 8:55 am    Post subject: Reply with quote

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
View user's profile Send private message
ComputerTech
Master


Joined: 22 Feb 2020
Posts: 374

PostPosted: Sat Apr 10, 2021 10:30 am    Post subject: Reply with quote

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 Razz

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 Razz
_________________
ComputerTech
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CrazyCat
Owner


Joined: 13 Jan 2002
Posts: 848
Location: France

PostPosted: Sat Apr 10, 2021 6:04 pm    Post subject: Reply with quote

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
Offer me a coffee - Do not ask me help in PM, we are a community.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
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


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber