diff --git a/README.md b/README.md index a7a2a95..ac9ae02 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # **AirStatus for Linux** #### Check your AirPods battery level on Linux -#### What is it? +### What is it? This is a Python 3.6 script, forked from [faglo/AirStatus](https://github.com/faglo/AirStatus) that allows you to check AirPods battery level from your terminal, as JSON output. ### Usage @@ -12,6 +12,8 @@ python3 main.py [output_file] Output will be stored in `output_file` if specified. +This script requires `bleak` package. Install it with: `pip3 install bleak`. + #### Example output ``` diff --git a/main.py b/main.py old mode 100644 new mode 100755 index a6b2e4f..e79d566 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 from bleak import discover from asyncio import new_event_loop, set_event_loop, get_event_loop from time import sleep, time_ns @@ -5,6 +6,8 @@ from json import dumps from sys import argv from datetime import datetime +import sys +import asyncio # Configure update duration (update after n seconds) UPDATE_DURATION = 1 @@ -54,11 +57,14 @@ async def get_device(): # Same as get_device() but it's standalone method instead of async def get_data_hex(): - new_loop = new_event_loop() - set_event_loop(new_loop) - loop = get_event_loop() - a = loop.run_until_complete(get_device()) - loop.close() + if sys.version_info < (3, 7): + new_loop = new_event_loop() + set_event_loop(new_loop) + loop = get_event_loop() + a = loop.run_until_complete(get_device()) + loop.close() + else: + a = asyncio.run(get_device()) return a @@ -128,19 +134,22 @@ def is_flipped(raw): def run(): output_file = argv[-1] - while True: - data = get_data() - - if data["status"] == 1: - json_data = dumps(data) - if len(argv) > 1: - f = open(output_file, "a") - f.write(json_data+"\n") - f.close() - else: - print(json_data) - - sleep(UPDATE_DURATION) + try: + while True: + data = get_data() + + if data["status"] == 1: + json_data = dumps(data) + if len(argv) > 1: + f = open(output_file, "a") + f.write(json_data+"\n") + f.close() + else: + print(json_data) + + sleep(UPDATE_DURATION) + except KeyboardInterrupt: + pass if __name__ == '__main__':