LoginSignup
0
0

More than 1 year has passed since last update.

新渡りの 気温計 on Mastodon

Last updated at Posted at 2023-04-13

ここはgithubではないのですが、めんどくさいのでこちらに直接ソース公開してみます。

crontab で毎時動かす気温計

kion.pl
#!/usr/bin/env perl

$|=1;

#実行時刻の取得
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$yyyy = $year + 1900;
$month = $mon + 1;
 
#ファイル指定
$dir = '/home/username/htdocs/hometemp';
$ym=sprintf("%04d%02d",$yyyy,$month);
mkdir($ymdir);

$log = "$dir/log/$ym.csv";
$tmp = "$dir/tmp.json";
$html = "$dir/kion.html";

#アクセストークンを指定
$apikey = "Nature Remoのアクセストークン";
 
#コマンドと引数を指定
$curl = "/usr/local/bin/curl";
$curlopt = "-X GET";
$url = "https://api.nature.global/1/devices";
$header1 = "-H \"accept: application/json\"";
$header2 = "-H \"Authorization: Bearer $apikey\"";
$jq = "/usr/local/bin/jq";
 
#APIアクセスを実行
print STDERR "[exec]$curl $curlopt $url $header1 $header2\n";
$remoapi = `$curl $curlopt $url $header1 $header2`;
print "$remoapi\n"; 
 
#センサー値の取り出し
my @remo;
for($loop=0; $loop<=2; $loop++) {

	$name = `echo '$remoapi' |$jq -r .[$loop].name`;
	chomp($name);
	continue if($name eq "");

	$firm = `echo '$remoapi' |$jq -r .[$loop].firmware_version`;
	$te = `echo '$remoapi' |$jq .[$loop].newest_events.te.val`;
	$hu = `echo '$remoapi' |$jq .[$loop].newest_events.hu.val`;
	$il = `echo '$remoapi' |$jq .[$loop].newest_events.il.val`;
	$mo = `echo '$remoapi' |$jq .[$loop].newest_events.mo.val`;


	chomp($firm);
	chomp($te);
	chomp($hu);
	chomp($il);
	chomp($mo);
	push(@remo, "$name\t$te\t$hu");

	#ログを出力
	open(LOG, ">>$log");
	flock(LOG, 2);
	print(LOG "$yyyy/$month/$mday $hour:$min:$sec,name=$name,firm=$firm,te=$te,hu=$hu,il=$il,mo=$mo\n");
	close(LOG);
}

#最高、最低気温の取得
foreach(@remo) {
	($name, $te, $hu)=split(/\t/,$_);
	open(LOG, "$log");
	foreach(<LOG>) {
		chomp;
		if(/^$yyyy\/$month\/$mday /) {
			if(/name\=$name,/) {
				next if(length($_) < 10);
				$tetmp=$_;
				$tetmp=~s/.*te=//g;
				$tetmp=~s/,.*//g;
				if($temax{$name} eq "") {
					$temax{$name}=-1000;
				}
				if($temin{$name} eq "") {
					$temin{$name}=1000;
				}
				if($tetmp > $temax{$name}+0) {
					$temax{$name}=$tetmp;
				}
				if($tetmp < $temin{$name}+0) {
					$temin{$name}=$tetmp;
				}
			}
		}
	}
	close(LOG);
}

#HTMLファイル生成
open(FILE, ">$html");
flock(FILE, 2);
print FILE <<EOM;
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta http-equiv="Refresh" content="60;./kion.html">
<title>kion</title>
<style>
body { background:#eec; color:#000}
span.font1 {font-size: 100%; }
span.font2 {font-size: 100%; }
span.max {color:#f00}
span.min {color:#00f}
</style>
</head>
<body>
<span class="font1">@{[sprintf("%02d:%02d",$hour,$min)]}</span>
EOM
foreach(@remo) {
	($name, $te, $hu)=split(/\t/,$_);
	if($hu eq "null") {
		print FILE <<EOM;
<span class="font2">$name=$te℃</span><span class="max">$temax{$name}℃</span><span class="min">$temin{$name}℃</span>
EOM
	} else {
		print FILE <<EOM;
<span class="font2">$name=$te℃/$hu%</span><span class="max">$temax{$name}℃</span><span class="min">$temin{$name}℃</span>
EOM
	}
}
print FILE <<EOM;
</body>
</html>
EOM

close(FILE);

exit:

りののトゥート(perl)

rinotemp.pl
#!/usr/bin/env perl

#実行時刻の取得
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$yyyy = $year + 1900;
$month = $mon + 1;

$_hour=$hour;
$_min=$min;

#ファイル指定
#$dir = '/mnt/ramdisk/hometemp';
$dir = '/home/username/htdocs/hometemp/log';
$ym=sprintf("%04d%02d",$yyyy,$month);

$log = "$dir/$ym.csv";

#アクセストークンを指定
$apikey = "Nature Remoのアクセストークン";
 
#コマンドと引数を指定
$curl = "/usr/local/bin/curl";
$curlopt = "-X GET";
$url = "https://api.nature.global/1/devices";
$header1 = "-H \"accept: application/json\"";
$header2 = "-H \"Authorization: Bearer $apikey\"";
$jq = "/usr/local/bin/jq";
 
#APIアクセスを実行
#print STDERR "[exec]$curl $curlopt $url $header1 $header2\n";
$remoapi = `$curl $curlopt $url $header1 $header2 2>/dev/null`;
 
#センサー値の取り出し
my @remo;
for($loop=0; $loop<=2; $loop++) {

	$name = `echo '$remoapi' |$jq -r .[$loop].name`;
	chomp($name);
	continue if($name eq "");

	$firm = `echo '$remoapi' |$jq -r .[$loop].firmware_version`;
	$te = `echo '$remoapi' |$jq .[$loop].newest_events.te.val`;
	$hu = `echo '$remoapi' |$jq .[$loop].newest_events.hu.val`;
	$il = `echo '$remoapi' |$jq .[$loop].newest_events.il.val`;
	$mo = `echo '$remoapi' |$jq .[$loop].newest_events.mo.val`;


	chomp($firm);
	chomp($te);
	chomp($hu);
	chomp($il);
	chomp($mo);
	push(@remo, "$name\t$te\t$hu");

}

#最高、最低気温の取得
foreach(@remo) {
	($name, $te, $hu)=split(/\t/,$_);
	open(LOG, "$log");
	foreach(<LOG>) {
		chomp;
		if(/^$yyyy\/$month\/$mday /) {
			if(/name\=$name,/) {
				next if(length($_) < 10);
				$time=$_;
				$time=~s/,.*//g;
				$time=~s/.* //g;
				($hour,$min)=split(/:/,$time);

				$tetmp=$_;
				$tetmp=~s/.*te=//g;
				$tetmp=~s/,.*//g;
				if($temax{$name} eq "") {
					$temax{$name}=-1000;
				}
				if($temin{$name} eq "") {
					$temin{$name}=1000;
				}
				if($tetmp > $temax{$name}+0) {
					$temax{$name}=$tetmp;
					$temaxhour{$name}=$hour;
					$temaxmin{$name}=$min;
				}
				if($tetmp < $temin{$name}+0) {
					$temin{$name}=$tetmp;
					$teminhour{$name}=$hour;
					$teminmin{$name}=$min;
				}
			}
		}
	}
	close(LOG);
}
foreach(@remo) {
	($name, $te, $hu)=split(/\t/,$_);
	if($name eq "RemoOut") {
		$ondo=sprintf("%.1f℃(最高%.1f℃(%02d:%02d)/最低%.1f℃(%02d:%02d)) 湿度%d%"
		, $te
		, $temax{$name}, $temaxhour{$name}, $temaxmin{$name}
		, $temin{$name}, $teminhour{$name}, $teminmin{$name}
		, $hu);
		$tee=$te;
	}
}

$toot=sprintf("りのの所の%02d:%02d現在の温度はね<br>$ondoだお!", $_hour, $_min);
if($hour >= 3 && $hour < 11) {
	$toot.="<br>zzz...✧♡";
} else {
	if($tee>=28) {
		$toot.="<br>暑いだお!🔥 ";
	}

	if($tee<=15) {
		$toot.="<br>寒いだお!🧊 ぶるぶる";
	}
}

$toot.="<br><br>#りのの温度記録";

print $toot;
`/usr/local/bin/python3.9 /home/username/bot.py '$toot'`;

汎用mastodonトゥート

bot.py
from mastodon import Mastodon, StreamListener
import sys

# Botの主な挙動をここに記述します。
class Bot(StreamListener):
    # Botの準備してくれる所。所謂おまじない。
    def __init__(self):
        super(Bot, self).__init__()
    # アカウントのローカルタイムラインに動きがあると新規トゥートの情報を持って中の挙動を読み取る。
    def on_update(self, status):
        pass

def Login():
    mastodon = Mastodon(
                access_token = "mastodonのアクセストークン",           # ここにさっき取得したアクセストークンを入れる。
                api_base_url = "https://example.com/"    # ここにMastodonのインスタンスのURLを記述
                )
    return mastodon

def LTLlisten(mastodon):
    bot = Bot()
    mastodon.stream_local(bot)

# ログインする処理
mastodon = Login()
#LTLlisten(mastodon)
for x in sys.argv:
	if not 'bot.py' in x:
		x=x.replace('<br>', '\n')
		mastodon.toot(x)

実際に動いているもの

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0