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

Joined: 22 Feb 2020 Posts: 398
|
Posted: Wed Apr 07, 2021 12:33 am Post subject: CT-Weather |
|
|
So i decided to release a Weather script which uses Openweathermap API Key
Code: |
###########################################################
# 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
and yes i plan to add !w set location and other things tomorrow
but do remember, this version is a beta  _________________ ComputerTech |
|
Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3767 Location: Mint Factory
|
Posted: Wed Apr 07, 2021 3:15 am Post subject: |
|
|
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. |
|
Back to top |
|
 |
ComputerTech Master

Joined: 22 Feb 2020 Posts: 398
|
Posted: Wed Apr 07, 2021 11:31 am Post subject: |
|
|
Sure
Code: |
{"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: |
https://api.openweathermap.org/data/2.5/weather?q=london&units=metric&appid=Your-API-Key-Here
|
In your browser  _________________ ComputerTech |
|
Back to top |
|
 |
rainman Voice
Joined: 21 Apr 2021 Posts: 10
|
Posted: Wed Apr 21, 2021 10:35 am Post subject: |
|
|
Script doesn't work
Tcl error [weather:call]: can't read "ctw(msg)": no such element in array |
|
Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1108 Location: France
|
|
Back to top |
|
 |
rainman Voice
Joined: 21 Apr 2021 Posts: 10
|
Posted: Thu Apr 22, 2021 2:43 am Post subject: |
|
|
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 ! |
|
Back to top |
|
 |
ComputerTech Master

Joined: 22 Feb 2020 Posts: 398
|
Posted: Thu Apr 22, 2021 11:00 pm Post subject: |
|
|
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
Code: |
[04:01] (@TechQ) [Weather] London, GB | 3.04C | 2.06mph | clear sky | 81%
|
_________________ ComputerTech |
|
Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1108 Location: France
|
Posted: Fri Apr 23, 2021 2:17 am Post subject: |
|
|
If you release a new version, please correct a little thing to use standard way:
Code: | 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: | 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. _________________ 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 |
|
 |
asL_pLs Voice
Joined: 02 May 2016 Posts: 25
|
Posted: Tue Apr 27, 2021 3:34 pm Post subject: |
|
|
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
Code: |
[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  |
|
Back to top |
|
 |
ComputerTech Master

Joined: 22 Feb 2020 Posts: 398
|
Posted: Tue Apr 27, 2021 6:17 pm Post subject: |
|
|
Here it is
Code: |
###########################################################
# 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: |
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: |
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  _________________ ComputerTech |
|
Back to top |
|
 |
play4free2 Voice

Joined: 23 Nov 2013 Posts: 34
|
Posted: Wed May 05, 2021 5:10 pm Post subject: |
|
|
ComputerTech,
I changed kmph and mph in Metric/Imperial part of the script.
Here:
Code: | 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: | 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 _________________
420-HIGHTIMES IRC Network |
|
Back to top |
|
 |
rainman Voice
Joined: 21 Apr 2021 Posts: 10
|
Posted: Wed May 05, 2021 10:16 pm Post subject: |
|
|
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  |
|
Back to top |
|
 |
ComputerTech Master

Joined: 22 Feb 2020 Posts: 398
|
Posted: Wed May 05, 2021 11:28 pm Post subject: |
|
|
Code: |
###########################################################
# 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: |
!w salt%20lake%20city,US
|
would using formatQuery do this automatically? because i tried that
Code: |
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 |
|
Back to top |
|
 |
play4free2 Voice

Joined: 23 Nov 2013 Posts: 34
|
Posted: Thu May 06, 2021 1:55 am Post subject: |
|
|
This is all I got today messing with it:
Code: | ##########################
# 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: | 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: | if {$text == ""} {
putserv "PRIVMSG $chan \00304Please specify a US postal zip code.\003"
} | for users that only type !w _________________
420-HIGHTIMES IRC Network
Last edited by play4free2 on Wed May 19, 2021 6:21 pm; edited 8 times in total |
|
Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1108 Location: France
|
Posted: Thu May 06, 2021 5:27 am Post subject: |
|
|
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
Quote: | This flag causes ::http::geturl to do a POST request that passes the query as payload verbatim to the server. |
This one works:
Code: | 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 _________________ 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
|
|