過去にChinachu向けのスクリプトを少し書いたが、この度ちょっと家を空けるため、それを見越して録画が動作していない場合に自動でシャットダウンする仕組みを作成した。導入方法などはまた今度。
過去のスクリプトに少し手を入れる格好。
#!/usr/bin/env python3 from urllib.request import urlopen import json import io import time class ChinachuStatus: def __init__(self, urlBase): self.urlBase = urlBase def getNextReserve(self): response = urlopen(self.urlBase + '/reserves.json') res = json.load(io.TextIOWrapper(response, response.getheader('content-type').split('charset=')[1])) now = time.time() for e in [ int(ent['start'] / 1000) for ent in res if ent['start'] / 1000 > now ]: return e def isChinachuBusy(self): response = urlopen(self.urlBase + '/recording.json') res = json.load(io.TextIOWrapper(response, response.getheader('content-type').split('charset=')[1])) if len(res) > 0: return True if self.getNextReserve() - 600 < time.time(): return True return False if __name__ == '__main__': url = 'http://watson.local:10772/api' print(ChinachuStatus(url).getNextReserve()) print(ChinachuStatus(url).isChinachuBusy())
Code: ChinachuStatus.py
#!/usr/bin/env python3 from ChinachuStatus import ChinachuStatus if __name__ == '__main__': url = 'http://localhost:10772/api' print(ChinachuStatus(url).getNextReserve() - 300)
Code: chinachuNext
#!/usr/bin/env python3 import sys from ChinachuStatus import ChinachuStatus if __name__ == '__main__': url = 'http://localhost:10772/api' res = ChinachuStatus(url).isChinachuBusy() if res: print("Chinachu is busy now.") sys.exit(1) print("Chinachu is free.") sys.exit(0)
Code: chinachuFree
*/5 * * * * /usr/local/bin/chinachuFree && /bin/date | /usr/bin/mail -s "chinachu is going to poweroff" haruo31 && /sbin/shutdown -h now
Code: ~# crontab -e
コメントを残す