Bricktop
Bricktop

Reputation: 153

Trying to make a variable from curl wttr.in output

I want to get the outside temperature into a variable.

curl wttr.in/berlin?format=%t

produces a perfect clean output of, for example +8°C (at command prompt) that I'd like. %t is for temperature. however this

curl wttr.in/berlin?format=%t>temp.txt
set /p temp=<temp.txt

produces -7┬░C which I don't like. I just wonder if I could fix this for command instead and skip the character set problem

for /f %%x in ('curl wttr.in/berlin?format=%t') do set temp=%%x

but this one suddenly produces general multirow result instead of just the simple temperature, along with error

 Could not resolve host: %t

ultimately I will need multiple variables from wttr.in so it would be most efficient to extract them all at once, and set variables accordingly, for example

curl wttr.in/berlin?format="%t+%C+%w"

where %C is conditions, %w is wind. does this mean that fixing the for loop is the way to go for simplicity?

Upvotes: 1

Views: 772

Answers (1)

Bricktop
Bricktop

Reputation: 153

chcp 65001
for /f "tokens=1-9,*" %%a in ('"curl wttr.in/berlin?format^="%%t+%%M+%%p+%%D+%%S+%%z+%%s+%%d+%%C""') do (
    set "temperature=%%a"
    set "tempfeel=%%b"
    set "moonday=%%c"
    set "precipitation=%%d"
    set "dawn=%%e"
    set "sunrise=%%f"
    set "zenith=%%g"
    set "sunset=%%h"
    set "dusk=%%i"
    set "condition=%%j"
)

Unicode fixes the unicode issue, ^ fixes the for loop, along with "tokens=1-9,*" for collecting all text based variables "%%t+%%M+%%p+%%D+%%S+%%z+%%s+%%d+%%C", where condition is a phrase, collected by the *.

Upvotes: 1

Related Questions