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.

CT-Weather

Support & discussion of released scripts, and announcements of new releases.
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

CT-Weather

Post by ComputerTech »

So i decided to release a Weather script which uses Openweathermap API Key

Code: Select all

###########################################################
# CT-Weather                                              #
###########################################################
# Author:  ComputerTech                                   #
# Email:   ComputerTech312@Gmail.com                      #
# Github:  https://github.com/computertech312             #
# Version: 0.1                                            #
# Release: 06/04/2021                                     #
###########################################################
# Description:                                            #
#                                                         #
#              - A Weather script which uses the          #
#                OpenWeathermap API Key.                  #
#              - https://openweathermap.org/api           #
#                                                         #
# History:                                                #
#                                                         #
#               - 0.1: First release.                     #
#                                                         #
###########################################################
# Start of configuration #
##########################

##########################
# Trigger
###
set ctw(trig) "!w"


##########################
# Flags
###
# Owner     = n
# Master    = m
# Op        = o
# Voice     = v
# Friend    = f
# Everyone  = -
##
set ctw(flag) "-"


##########################
# API Key
###
set ctw(api) "Your-API-Key"


##########################
# Output Method
###
# 0 = Notice
# 1 = Privmsg
# 2 = Channel
##
set ctgg(msg) "2"


##########################
# Metric Type
###
# 0 = Metric
# 1 = Imperial
##
set ctw(met) "0"


##########################
#  End of configuration  #
###########################################################

package require json
package require tls
package require http

bind PUB $ctw(flag) $ctw(trig) weather:call

proc weather:call {nick host hand chan text} {
	global ctw
  http::register https 443 [list ::tls::socket]
	set ctw(location) [join $text ,]
  switch -- $ctw(met) {
	"0" {set ctw(metr) "metric"
	     set ctw(met2) "C"
	     set ctw(met3) "mph"}
	"1" {set ctw(metr) "imperial"
	     set ctw(met2) "F"
	     set ctw(met3) "kmph"}
	}
  switch -- $ctw(msg) {
	"0" {set ctw(out) "NOTICE $nick"}
	"1" {set ctw(out) "PRIVMSG $nick"}
	"2" {set ctw(out) "PRIVMSG $chan"}
	}
         set ctw(url) "http://api.openweathermap.org/data/2.5/weather?q=$ctw(location)&units=$ctw(metr)&appid=$ctw(api)"
         set data [http::data [http::geturl "$ctw(url)" -timeout 10000]]
  http::cleanup $data
         set data2 [::json::json2dict $data]
         set basic [dict get $data2 "weather"]
         set description [dict get $basic "description"]
         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 sys [dict get $data2 "sys"]
         set country [dict get $sys "country"]
         set name [dict get $data2 "name"]
         
	http::unregister https
                    putserv "$ctw(out) :\00309\[Weather\]\003 ${name}, $country | ${temp}$ctw(met2) | $description | Humidity: ${humidity}% | Wind: ${speed}$ctw(met3) "
}

###########################################################
Grab a Free API Key from the link in the description, all bug finds and suggestions are more than welcome :wink:

and yes i plan to add !w set location and other things tomorrow

but do remember, this version is a beta ;)
ComputerTech
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

I signed up for a free plan but I keep getting the 401 error, even if the key is correct. Anyway, could you copy/paste the JSON result (it's $data2 variable) of any query to have a look at something?
Once the game is over, the king and the pawn go back in the same box.
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

Sure

Code: Select all

{"coord":{"lon":-0.1257,"lat":51.5085},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":6.76,"feels_like":3.07,"temp_min":5.56,"temp_max":8.89,"pressure":1024,"humidity":42},"visibility":10000,"wind":{"speed":6.17,"deg":300},"clouds":{"all":85},"dt":1617808949,"sys":{"type":1,"id":1414,"country":"GB","sunrise":1617772914,"sunset":1617820973},"timezone":3600,"id":2643743,"name":"London","cod":200}
If you want, i can PM you my API key :)

also try this

Code: Select all

https://api.openweathermap.org/data/2.5/weather?q=london&units=metric&appid=Your-API-Key-Here
In your browser :P
ComputerTech
r
rainman
Voice
Posts: 11
Joined: Wed Apr 21, 2021 10:33 am

Post by rainman »

Script doesn't work

Tcl error [weather:call]: can't read "ctw(msg)": no such element in array
User avatar
CrazyCat
Revered One
Posts: 1302
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

I think ComputerTech paste&copy too quick :)

Find the line:

Code: Select all

set ctgg(msg) "2"
Replace it with:

Code: Select all

set ctw(msg) "2"
r
rainman
Voice
Posts: 11
Joined: Wed Apr 21, 2021 10:33 am

Post by rainman »

hehe thank you :)

Tcl error [weather:call]: missing value to go with key

even tho i have a key and its been there for few hours

joys of errors !
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

uh apologies, i made a new version of CT-weather and seem to have forgot to post the new one here, i'll do it in the morning :wink:

Code: Select all

[04:01] (@TechQ) [Weather] London, GB | 3.04C | 2.06mph | clear sky | 81%
ComputerTech
User avatar
CrazyCat
Revered One
Posts: 1302
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

If you release a new version, please correct a little thing to use standard way:

Code: Select all

set ctw(url) "http://api.openweathermap.org/data/2.5/weather?q=$ctw(location)&units=$ctw(metr)&appid=$ctw(api)"
set data [http::data [http::geturl "$ctw(url)" -timeout 10000]]
Don't create the url with all its args, do:

Code: Select all

set ctw(url) "http://api.openweathermap.org/data/2.5/weather"
set params [::http::formatQuery q $ctw(location) units $ctw(metr) appid $ctw(api)]
set data [http::data [http::geturl $ctw(url) -query $params -timeout 10000]]
Like this, you can have ctw(url) as a script setting (don't have to redefine it each time the proc is called) and you use the good way to create your url.

Another tip:
Actually, you do set ctw(url) in your procedure, but ctw() is your array of global settings. When you set a variable used only in a local procedure, don't use a global variable. A global variable must be modified only when it's used by another proc.
User avatar
aslpls
Halfop
Posts: 47
Joined: Mon May 02, 2016 9:41 am

Post by aslpls »

ComputerTech wrote:uh apologies, i made a new version of CT-weather and seem to have forgot to post the new one here, i'll do it in the morning :wink:

Code: Select all

[04:01] (@TechQ) [Weather] London, GB | 3.04C | 2.06mph | clear sky | 81%



[19:33:15] Tcl error [weather:call]: key "weather" not known in dictionary

please post an updated script :)
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

Here it is

Code: Select all

###########################################################
# CT-Weather                                              #
###########################################################
# Author:  ComputerTech                                   #
# Email:   ComputerTech312@Gmail.com                      #
# Github:  https://github.com/computertech312             #
# Version: 0.2                                            #
# Release: 27/04/2021                                     #
###########################################################
# Description:                                            #
#                                                         #
#              - A Weather script which uses the          #
#                OpenWeathermap API Key.                  #
#              - https://openweathermap.org/api           #
#                                                         #
# History:                                                #
#                                                         #
#               - 0.1: First release.                     #
#                                                         #
###########################################################
namespace eval ctweather {
##########################
# Start of configuration #
##########################

##########################
# Trigger
###
variable ::trig "!w"


##########################
# Flags
###
# Owner     = n
# Master    = m
# Op        = o
# Voice     = v
# Friend    = f
# Everyone  = -
##
variable ::flag "-"


##########################
# API Key
###
variable ::api "Your-API-Key"


##########################
# Output Method
###
# 0 = Notice
# 1 = Privmsg
# 2 = Channel
##
variable ::msg "2"


##########################
# Metric Type
###
# 0 = Metric
# 1 = Imperial
##
variable ::met "0"


##########################
#  End of configuration  #
###########################################################

package require json
package require tls
package require http

bind PUB $::flag $::trig [namespace current]::weather:call

proc weather:call {nick host hand chan text} {
	http::register https 443 [list ::tls::socket]
	switch -- $::met {
		"0" {variable metr "metric"
			variable met2 "C"
			variable met3 "mph"}
		"1" {variable metr "imperial"
			variable met2 "F"
			variable met3 "kmph"}
	}
	switch -- $::msg {
		"0" {variable out "NOTICE $nick"}
		"1" {variable out "PRIVMSG $nick"}
		"2" {variable out "PRIVMSG $chan"}
	}
         variable url "http://api.openweathermap.org/data/2.5/weather?q=[join $text ,]&units=$metr&appid=$::api"
         variable data [http::data [http::geturl "$url" -timeout 10000]]
	variable data [http::data [http::geturl "$url" -timeout 10000]]
	http::cleanup $data
	http::unregister https
	variable data2 [::json::json2dict $data]
	variable cod [dict get $data2 "cod"]

	if {$cod == "404"} {
		putserv "PRIVMSG $chan \00304Page not found, be more specific\003"
	}
	if {$cod == "200"} {
		variable name [dict get $data2 "name"]
		variable sys [dict get $data2 "sys"]
		variable country [dict get $sys "country"]
		variable main [dict get $data2 "main"]
		variable temp [dict get $main "temp"]
		variable humidity [dict get $main "humidity"]
		variable wind [dict get $data2 "wind"]
		variable speed [dict get $wind "speed"]
		variable weather2 [dict get $data2 "weather"]
		variable current [dict get [lindex [dict get $data2 weather] 0] description]
		variable name	[encoding convertfrom utf-8 $name]
		putserv "$out :\[\00309Weather\003\] ${name}, $country | ${temp}$met2 | ${speed}$met3 | $current | ${humidity}%"
	}
}

}

###########################################################
CrazyCat: i tried your suggestion and only got a error back

Code: Select all

TechQ cod 401 message {Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.}
Improper syntax? i am unsure :?

Anyway here's a example

Code: Select all

TechQ [Weather] London, GB | 9.73C | 6.69mph | broken clouds | 66%
I plan to try add a option for users to add their default locations soon :wink:
ComputerTech
User avatar
play4free2
Voice
Posts: 34
Joined: Sat Nov 23, 2013 1:42 am
Contact:

Post by play4free2 »

ComputerTech,

I changed kmph and mph in Metric/Imperial part of the script.

Here:

Code: Select all

proc weather:call {nick host hand chan text} {
   http::register https 443 [list ::tls::socket]
   switch -- $::met {
      "0" {variable metr "metric"
         variable met2 "C"
         variable met3 "kmph"}
      "1" {variable metr "imperial"
         variable met2 "F"
         variable met3 "MPH"}
   } 
I also added back the descriptions you had here:

Code: Select all

putserv "$out :\[\00309Weather\003\] ${name}, $country | Temp: ${temp}$met2 | Wind: ${speed}$met3 | Conditions: $current | Humidity: ${humidity}%"
The issue I am having is the script will not show the correct city using a zip code and it will not return the correct location if the city has more then a one word name as in.

!w Salt Lake City, UT returns:
[Weather] Salt, ES | Temp: 57.72F | Wind: 2.3MPH | Conditions: few clouds | Humidity: 88%

I would also like to add the degree symbol "°" as in: Temp: 57.72°F.
But I have had no luck trying to fix the city issue or adding the degree symbol.

Thanks for the script and any suggestions you may have to fix/add one or both of them,
play4free2
Image
420-HIGHTIMES IRC Network
r
rainman
Voice
Posts: 11
Joined: Wed Apr 21, 2021 10:33 am

Post by rainman »

i did some changes to the way the output shows as well

.w burwood au

[Weather] Location: Burwood, AU - Temperature: 16.23C - Wind: 0.81mph - Outside: clear sky - Humidity: 72%

.w sydney au

[Weather] Location: Sydney, AU - Temperature: 19.89C - Wind: 8.75mph - Outside: broken clouds - Humidity: 83%

Only issue i have is it can't tell the difference between different states when they have the same name sadly :(
User avatar
ComputerTech
Master
Posts: 399
Joined: Sat Feb 22, 2020 10:29 am
Contact:

Post by ComputerTech »

Code: Select all

###########################################################
# CT-Weather                                              #
###########################################################
# Author:  ComputerTech                                   #
# Email:   ComputerTech312@Gmail.com                      #
# Github:  https://github.com/computertech312             #
# Version: 0.2                                            #
# Release: 27/04/2021                                     #
###########################################################
# Description:                                            #
#                                                         #
#              - A Weather script which uses the          #
#                OpenWeathermap API Key.                  #
#              - https://openweathermap.org/api           #
#                                                         #
# History:                                                #
#                                                         #
#               - 0.1: First release.                     #
#                                                         #
###########################################################
namespace eval ctweather {
##########################
# Start of configuration #
##########################

##########################
# Trigger
###
variable ::trig "!w"


##########################
# Flags
###
# Owner     = n
# Master    = m
# Op        = o
# Voice     = v
# Friend    = f
# Everyone  = -
##
variable ::flag "-"


##########################
# API Key
###
variable ::api "59264c8c829cbcd87204931c232b8ae5"


##########################
# Output Method
###
# 0 = Notice
# 1 = Privmsg
# 2 = Channel
##
variable ::msg "2"


##########################
# Metric Type
###
# 0 = Metric
# 1 = Imperial
##
variable ::met "0"


##########################
#  End of configuration  #
###########################################################

package require json
package require tls
package require http

bind PUB $::flag $::trig [namespace current]::weather:call

proc weather:call {nick host hand chan text} {
   http::register https 443 [list ::tls::socket]
   switch -- $::met {
      "0" {variable metr "metric"
         variable met2 "C"
         variable met3 "mph"}
      "1" {variable metr "imperial"
         variable met2 "F"
         variable met3 "kmph"}
   }
   switch -- $::msg {
      "0" {variable out "NOTICE $nick"}
      "1" {variable out "PRIVMSG $nick"}
      "2" {variable out "PRIVMSG $chan"}
   }
 set text [regsub { ([^ ]+)$} "$text" {,\1}]
 set text [regsub -all { } "$text" "%20"]
         variable url "http://api.openweathermap.org/data/2.5/weather?q=$text&units=$metr&appid=$::api"
         variable data [http::data [http::geturl "$url" -timeout 10000]]
   http::cleanup $data
   http::unregister https
   variable data2 [::json::json2dict $data]
 variable cod [dict get $data2 "cod"]

   if {$cod == "404"} {
      putserv "PRIVMSG $chan \00304Page not found, be more specific\003"
   }
   if {$cod == "200"} {
      variable name [dict get $data2 "name"]
      variable sys [dict get $data2 "sys"]
      variable country [dict get $sys "country"]
      variable main [dict get $data2 "main"]
      variable temp [dict get $main "temp"]
      variable humidity [dict get $main "humidity"]
      variable wind [dict get $data2 "wind"]
      variable speed [dict get $wind "speed"]
      variable weather2 [dict get $data2 "weather"]
      variable current [dict get [lindex [dict get $data2 weather] 0] description]
      variable name   [encoding convertfrom utf-8 $name]
      putserv "$out :\[\00309Weather\003\] ${name}, $country | ${temp}$met2 | ${speed}$met3 | $current | ${humidity}%"
   }
}
}

###########################################################
this works barely, problem with it is, if i did !w rio de janerio brazil ,it works, but if i did rio de janerio it wont work, since i add , at janerio, so yeah


i learnt that i need %20 between spaces, so like this

Code: Select all

!w salt%20lake%20city,US
would using formatQuery do this automatically? because i tried that

Code: Select all

namespace eval ctweather {
	##########################
	# Start of configuration #
	##########################

	##########################
	# Trigger
	###
	variable ::trig "!w"


	##########################
	# Flags
	###
	# Owner     = n
	# Master    = m
	# Op        = o
	# Voice     = v
	# Friend    = f
	# Everyone  = -
	##
	variable ::flag "-"


	##########################
	# API Key
	###
	variable ::api "8c2a600d5d63d7fb13432fd58dcc419b"


	##########################
	# Output Method
	###
	# 0 = Notice
	# 1 = Privmsg
	# 2 = Channel
	##
	variable ::msg "2"


	##########################
	# Metric Type
	###
	# 0 = Metric
	# 1 = Imperial
	##
	variable ::met "0"


	##########################
	#  End of configuration  #
	###########################################################

	package require json
	package require tls
	package require http

	bind PUB $::flag $::trig [namespace current]::weather:call

	proc weather:call {nick host hand chan text} {
		http::register https 443 [list ::tls::socket]
		switch -- $::met {
			"0" {variable metr "metric"
				variable met2 "C"
				variable met3 "mph"}
			"1" {variable metr "imperial"
				variable met2 "F"
				variable met3 "kmph"}
		}
		switch -- $::msg {
			"0" {variable out "NOTICE $nick"}
			"1" {variable out "PRIVMSG $nick"}
			"2" {variable out "PRIVMSG $chan"}
		}
		variable url "http://api.openweathermap.org/data/2.5/weather"
		variable params [::http::formatQuery q $text units $metr appid $::api]
		variable data [http::data [http::geturl $url -query $params -timeout 10000]]
		http::cleanup $data
		http::unregister https
		variable data2 [::json::json2dict $data]
		variable cod [dict get $data2 "cod"]

		if {$cod == "404"} {
			putserv "PRIVMSG $chan \00304Page not found, be more specific\003"
		}
		if {$cod == "200"} {
			variable name [dict get $data2 "name"]
			variable sys [dict get $data2 "sys"]
			variable country [dict get $sys "country"]
			variable main [dict get $data2 "main"]
			variable temp [dict get $main "temp"]
			variable humidity [dict get $main "humidity"]
			variable wind [dict get $data2 "wind"]
			variable speed [dict get $wind "speed"]
			variable weather2 [dict get $data2 "weather"]
			variable current [dict get [lindex [dict get $data2 weather] 0] description]
			variable name   [encoding convertfrom utf-8 $name]
			putserv "$out :\[\00309Weather\003\] ${name}, $country | ${temp}$met2 | ${speed}$met3 | $current | ${humidity}%"
		}
	}
}
The above code using formatQuery doesn't output anything, nor any errors
i dunno, all help is appreciated :/
ComputerTech
User avatar
play4free2
Voice
Posts: 34
Joined: Sat Nov 23, 2013 1:42 am
Contact:

Post by play4free2 »

This is all I got today messing with it:

Code: Select all

##########################
# Start of configuration #
##########################

##########################
# Trigger
###
variable ::trig "!w"


##########################
# Flags
###
# Owner     = n
# Master    = m
# Op        = o
# Voice     = v
# Friend    = f
# Everyone  = -
##
variable ::flag "-"


##########################
# API Key
###
variable ::api "Your-API-Key"


##########################
# Output Method
###
# 0 = Notice
# 1 = Privmsg
# 2 = Channel
##
variable ::msg "2"


##########################
# Metric Type
###
# 0 = Metric
# 1 = Imperial
##
variable ::met "1"


##########################
#  End of configuration  #
###########################################################

package require json
package require tls
package require http

bind PUB $::flag $::trig weather:call

proc weather:call {nick host hand chan text} {
	if {![channel get $chan weather]} { return }
	http::register https 443 [list ::tls::socket]
	switch -- $::met {
		"0" {variable metr "metric"
			variable met2 "C"
			variable met3 "mph"}
		"1" {variable metr "imperial"
			variable met2 "F"
			variable met3 "kp/h"}
	}
	switch -- $::msg {
		"0" {variable out "NOTICE $nick"}
		"1" {variable out "PRIVMSG $nick"}
		"2" {variable out "PRIVMSG $chan"}
	}
	variable url "http://api.openweathermap.org/data/2.5/weather"
	variable params [::http::formatQuery zip $text units $metr appid $::api]
	variable data [http::data [http::geturl "$url?$params" -timeout 10000]]
	http::cleanup $data
	http::unregister https
	variable data2 [::json::json2dict $data]
	variable cod [dict get $data2 "cod"]
	
	if {$text == ""} {
		putserv "PRIVMSG $chan \00304Please specify a US postal zip code.\003"
	}
	if {$cod == "404"} {
		putserv "PRIVMSG $chan \00304Please specify a US postal zip code.\003"
	}
	if {$cod == "200"} {
		variable name [dict get $data2 "name"]
		variable sys [dict get $data2 "sys"]
		variable country [dict get $sys "country"]
		variable main [dict get $data2 "main"]
		variable temp [dict get $main "temp"]
		variable humidity [dict get $main "humidity"]
		variable wind [dict get $data2 "wind"]
		variable speed [dict get $wind "speed"]
		variable weather2 [dict get $data2 "weather"]
		variable current [dict get [lindex [dict get $data2 weather] 0] description]
		variable name   [encoding convertfrom utf-8 $name]
		putserv "$out :\[\00309Weather\003\] Location: ${name}, $country | Temperature: ${temp}$met2 | Wind: ${speed}$met3 | Conditions: $current | Humidity: ${humidity}%"
	}
}

setudef flag weather
###########################################################
I changed this line to where it works with US zip codes only:

Code: Select all

variable params [::http::formatQuery zip $text units $metr appid $::api]
Edit:
I removed the namespace and added a user def. flag. Also added this line:

Code: Select all

	if {$text == ""} {
		putserv "PRIVMSG $chan \00304Please specify a US postal zip code.\003"
	}
for users that only type !w
Last edited by play4free2 on Wed May 19, 2021 6:21 pm, edited 8 times in total.
Image
420-HIGHTIMES IRC Network
User avatar
CrazyCat
Revered One
Posts: 1302
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

ComputerTech wrote:The above code using formatQuery doesn't output anything, nor any errors
i dunno, all help is appreciated :/
hum, can I say "RTFM" ? https://www.tcl-lang.org/man/tcl/TclCmd/http.htm#M29
This flag causes ::http::geturl to do a POST request that passes the query as payload verbatim to the server.
This one works:

Code: Select all

set url "http://api.openweathermap.org/data/2.5/weather"
set params [::http::formatQuery q $text units $metr APPID $::api]
putlog "$url?$params"
set tok [http::geturl "$url?$params" -timeout 10000]
Short thing about your script: you create a namespace but put all your variables in the global ? That's weird. You loose all benefits from namespace
Post Reply