Created
January 11, 2017 05:15
-
-
Save tanaikech/5c084ed61ae302b959fb6c726803708c to your computer and use it in GitHub Desktop.
ワンライナーコードでNetatmoからデータを取得, 表示する ref: http://qiita.com/tanaike/items/539dd06a1ffd7b7451e9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> setlocal & curl -s -d "grant_type=password&client_id='#####'&client_secret='#####'&username='#####'&password='#####'&scope=read_station" "https://api.netatmo.net/oauth2/token" | for /f "usebackq tokens=*" %a in (`jq -r ".access_token"`) do @set a="%a" | curl -s -d "access_token=%a&device_id='#####'" "https://api.netatmo.net/api/getstationsdata" > dat.txt & for /f "usebackq tokens=*" %b in (`jq -r ".body.devices[0].dashboard_data.Temperature" dat.txt`) do @set b="%b" | echo: & set /p nb=Indoor: Temperature %b [degree C],<nul & for /f "usebackq tokens=*" %b in (`jq -r ".body.devices[0].dashboard_data.Humidity" dat.txt`) do @set b="%b" | set /p nb=Humidity %b [%],<nul & for /f "usebackq tokens=*" %b in (`jq -r ".body.devices[0].dashboard_data.Pressure" dat.txt`) do @set b="%b" | set /p nb=Pressure %b [hPa]<nul & for /f "usebackq tokens=*" %b in (`jq -r ".body.devices[0].modules[0].dashboard_data.Temperature" dat.txt`) do @set b="%b" | echo: & set /p nb=Outdoor: Temperature %b [degree C],<nul & for /f "usebackq tokens=*" %b in (`jq -r ".body.devices[0].modules[0].dashboard_data.Humidity" dat.txt`) do @set b="%b" | set /p nb=Humidity %b [%]<nul & del dat.txt | |
Indoor: Temperature 12 [degree C], Humidity 56 [%], Pressure 1000.2 [hPa] | |
Outdoor: Temperature 12.3 [degree C], Humidity 56 [%] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ curl -s -d "grant_type=password&client_id='#####'&client_secret='#####'&username='#####'&password='#####'&scope=read_station" "https://api.netatmo.net/oauth2/token"|curl -s -d "access_token=`jq -r '.access_token'`&device_id='#####'" "https://api.netatmo.net/api/getstationsdata"|jq -r '"\nIndoor: Temperature "+(.body.devices[0].dashboard_data.Temperature|tostring)+" [degree C], Humidity "+(.body.devices[0].dashboard_data.Humidity|tostring)+" [%], Pressure "+(.body.devices[0].dashboard_data.Pressure|tostring)+" [hPa]\nOutdoor: Temperature "+(.body.devices[0].modules[0].dashboard_data.Temperature|tostring)+" [degree C], Humidity "+(.body.devices[0].modules[0].dashboard_data.Humidity|tostring)+" [%]"' | |
Indoor: Temperature 12 [degree C], Humidity 56 [%], Pressure 1000.2 [hPa] | |
Outdoor: Temperature 12.3 [degree C], Humidity 56 [%] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"client_id" : "#####", | |
"client_secret": "#####", | |
"username" : "#####", | |
"password" : "#####", | |
"device_id" : "#####" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal | |
set accountfile="account.json" | |
for /f "usebackq tokens=*" %%a in (`jq -r ".client_id" %accountfile%`) do @set client_id=%%a | |
for /f "usebackq tokens=*" %%a in (`jq -r ".client_secret" %accountfile%`) do @set client_secret=%%a | |
for /f "usebackq tokens=*" %%a in (`jq -r ".username" %accountfile%`) do @set username=%%a | |
for /f "usebackq tokens=*" %%a in (`jq -r ".password" %accountfile%`) do @set password=%%a | |
for /f "usebackq tokens=*" %%a in (`jq -r ".device_id" %accountfile%`) do @set device_id=%%a | |
set authurl="https://api.netatmo.net/oauth2/token" | |
set tokenfile="token.json" | |
set datafile="data.json" | |
if not exist %tokenfile% ( | |
echo Get new access token | |
curl -s -d "grant_type=password&client_id=%client_id%&client_secret=%client_secret%&username=%username%&password=%password%&scope=read_station" "%authurl%" > %tokenfile% | |
) | |
for /f "usebackq tokens=*" %%a in (`jq -r ".access_token" %tokenfile%`) do @set atoken=%%a | |
for /f "usebackq tokens=*" %%a in (`jq -r ".refresh_token" %tokenfile%`) do @set rtoken=%%a | |
for /f "usebackq tokens=*" %%a in (`jq -r ".expires_in" %tokenfile%`) do @set expiration=%%a | |
for %%i in (%tokenfile%) do @set tokent=%%~ti | |
set y1=%tokent:~0,4% | |
set /a m1=1%tokent:~5,2%-100 | |
set /a d1=1%tokent:~8,2%-100 | |
set /a j1=1%tokent:~11,2%-100 | |
set /a f1=1%tokent:~14,2%-100 | |
set b1=0 | |
call :ConvUnixTime %y1% %m1% %d1% %j1% %f1% %b1% | |
set /a limittime=%UT%+%expiration% | |
set y2=%date:~0,4% | |
set /a m2=1%date:~5,2%-100 | |
set /a d2=1%date:~8,2%-100 | |
set /a j2=1%time:~0,2%-100 | |
set /a f2=1%time:~3,2%-100 | |
set b2=0 | |
call :ConvUnixTime %y2% %m2% %d2% %j2% %f2% %b2% | |
set currenttime=%UT% | |
if %limittime% lss %currenttime% ( | |
echo Using refresh token | |
curl -s -d "grant_type=refresh_token&refresh_token=%rtoken%&client_id=%client_id%&client_secret=%client_secret%" "%authurl%" > %tokenfile% | |
for /f "usebackq tokens=*" %%a in (`jq -r ".access_token" %tokenfile%`) do @set atoken=%%a | |
) | |
curl -s -d "access_token=%atoken%&device_id=%device_id%" "https://api.netatmo.net/api/getstationsdata" > %datafile% | |
for /f "usebackq tokens=*" %%a in (`jq -r ".body.devices[0].dashboard_data.Temperature" %datafile%`) do @set intemp=%%a | |
for /f "usebackq tokens=*" %%a in (`jq -r ".body.devices[0].dashboard_data.Humidity" %datafile%`) do @set inhumi=%%a | |
for /f "usebackq tokens=*" %%a in (`jq -r ".body.devices[0].dashboard_data.Pressure" %datafile%`) do @set pres=%%a | |
echo Indoor: Temperature %intemp% [degree C], Humidity %inhumi% [%%], Pressure %pres% [hPa] | |
for /f "usebackq tokens=*" %%a in (`jq -r ".body.devices[0].modules[0].dashboard_data.Temperature" %datafile%`) do @set extemp=%%a | |
for /f "usebackq tokens=*" %%a in (`jq -r ".body.devices[0].modules[0].dashboard_data.Humidity" %datafile%`) do @set exhumi=%%a | |
echo Outdoor: Temperature %extemp% [degree C], Humidity %exhumi% [%%] | |
goto :EOF | |
:ConvUnixTime | |
set y=%1 | |
set m=%2 | |
set d=%3 | |
set j=%4 | |
set f=%5 | |
set b=%6 | |
set /a utime=86400*(365*%y%+(%y%/4)-(%y%/100)+(%y%/400)+(306*(%m%+1)/10)-428+%d%-719161)+3600*%j%+60*%f%+%b%-(9*3600) | |
set "UT=%utime%" | |
goto :EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Indoor: Temperature 12 [degree C], Humidity 56 [%], Pressure 1000.2 [hPa] | |
Outdoor: Temperature 12.3 [degree C], Humidity 56 [%] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
set +x | |
accountfile="account.json" | |
client_id=`jq -r ".client_id" $accountfile` | |
client_secret=`jq -r ".client_secret" $accountfile` | |
username=`jq -r ".username" $accountfile` | |
password=`jq -r ".password" $accountfile` | |
device_id=`jq -r ".device_id" $accountfile` | |
authurl="https://api.netatmo.net/oauth2/token" | |
tokenfile="token.json" | |
datafile="data.json" | |
if [ ! -f $tokenfile ]; then | |
echo "Get new access token" | |
curl -s -d "grant_type=password&client_id=${client_id}&client_secret=${client_secret}&username=${username}&password=${password}&scope=read_station" "${authurl}" > ${tokenfile} | |
fi | |
atoken=`jq -r ".access_token" $tokenfile` | |
rtoken=`jq -r ".refresh_token" $tokenfile` | |
expiration=`jq -r ".expires_in" $tokenfile` | |
filedate=`date +%Y-%m-%d_%H:%M:%S -r $tokenfile` | |
filedate=${filedate/_/ } | |
filedate=`date -d "$filedate" +%s` | |
limittime=`expr $filedate + $expiration` | |
currenttime=`date +%s` | |
if [ $limittime -lt $currenttime ]; then | |
echo "Using refresh token" | |
curl -s -d "grant_type=refresh_token&refresh_token=${rtoken}&client_id=${client_id}&client_secret=${client_secret}" "${authurl}" > $tokenfile | |
atoken=`jq -r ".access_token" $tokenfile` | |
fi | |
curl -s -d "access_token=${atoken}&device_id=${device_id}" "https://api.netatmo.net/api/getstationsdata" > $datafile | |
intemp=`jq -r ".body.devices[0].dashboard_data.Temperature" $datafile` | |
inhumi=`jq -r ".body.devices[0].dashboard_data.Humidity" $datafile` | |
pres=`jq -r ".body.devices[0].dashboard_data.Pressure" $datafile` | |
echo "Indoor: Temperature $intemp [degree C], Humidity $inhumi [%], Pressure $pres [hPa]" | |
extemp=`jq -r ".body.devices[0].modules[0].dashboard_data.Temperature" $datafile` | |
exhumi=`jq -r ".body.devices[0].modules[0].dashboard_data.Humidity" $datafile` | |
echo "Outdoor: Temperature $extemp [degree C], Humidity $exhumi [%]" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Indoor: Temperature 12 [degree C], Humidity 56 [%], Pressure 1000.2 [hPa] | |
Outdoor: Temperature 12.3 [degree C], Humidity 56 [%] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment