From 042ad136b9a4d418258b5dcc9d0de93539b84123 Mon Sep 17 00:00:00 2001 From: dmunozv04 <39565245+dmunozv04@users.noreply.github.com> Date: Sat, 2 Apr 2022 15:31:00 +0200 Subject: [PATCH] Added interactive configuration creator/editor --- create_config.py | 104 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 create_config.py diff --git a/create_config.py b/create_config.py new file mode 100644 index 0000000..a98ae7a --- /dev/null +++ b/create_config.py @@ -0,0 +1,104 @@ +import pyatv +import json +import asyncio +from pyatv.const import DeviceModel +from pyatv.scripts import TransformProtocol +import sys + +def load_config(config_file="config.json"): + try: + with open(config_file) as f: + config = json.load(f) + except: + config = {} + return config + +def save_config(config, config_file="config.json"): + with open(config_file, "w") as f: + json.dump(config, f) + +#Taken from postlund/pyatv atvremote.py +async def _read_input(loop: asyncio.AbstractEventLoop, prompt: str): + sys.stdout.write(prompt) + sys.stdout.flush() + user_input = await loop.run_in_executor(None, sys.stdin.readline) + return user_input.strip() + +async def find_atvs(loop): + devices = await pyatv.scan(loop) + if not devices: + print("No devices found") + return + atvs = [] + for i in devices: + #Only get Apple TV's + if i.device_info.model in [DeviceModel.Gen4, DeviceModel.Gen4K, DeviceModel.AppleTV4KGen2]: + #if i.device_info.model in [DeviceModel.AppleTV4KGen2]: #FOR TESTING + if input("Found {}. Do you want to add it? (y/n) ".format(i.name)) == "y": + + identifier = i.identifier + + pairing = await pyatv.pair(i, loop=loop, protocol=pyatv.Protocol.AirPlay) + await pairing.begin() + if pairing.device_provides_pin: + pin = await _read_input(loop, "Enter PIN on screen: ") + pairing.pin(pin) + await pairing.finish() + creds = pairing.service.credentials + atvs.append({"identifier": identifier, "airplay_credentials": creds}) + print("Pairing successful") + await pairing.close() + return atvs + + + + +def main(): + config = load_config() + try: num_atvs = len(config["atvs"]) + except: num_atvs = 0 + if input("Found {} Apple TV(s) in config.json. Add more? (y/n) ".format(num_atvs)) == "y": + loop = asyncio.get_event_loop_policy().get_event_loop() + asyncio.set_event_loop(loop) + task = loop.create_task(find_atvs(loop)) + loop.run_until_complete(task) + atvs = task.result() + try: + for i in atvs: + config["atvs"].append(i) + print("done adding") + except: + print("rewriting atvs (don't worry if none were saved before)") + config["atvs"] = atvs + + try : apikey = config["apikey"] + except: + apikey = "" + if apikey != "" : + if input("Apikey already specified. Change it? (y/n) ") == "y": + apikey = input("Enter your API key: ") + config["apikey"] = apikey + else: + apikey = input("Enter your API key: ") + config["apikey"] = apikey + + try: skip_categories = config["skip_categories"] + except: + skip_categories = [] + + if skip_categories != []: + if input("Skip categories already specified. Change them? (y/n) ") == "y": + categories = input("Enter skip categories (space sepparated) Options: [sponsor, selfpromo, exclusive_access, interaction, poi_highlight, intro, outro, preview, filler, music_offtopic:\n") + skip_categories = categories.split(" ") + else: + categories = input("Enter skip categories (space sepparated) Options: [sponsor, selfpromo, exclusive_access, interaction, poi_highlight, intro, outro, preview, filler, music_offtopic:\n") + skip_categories = categories.split(" ") + config["skip_categories"] = skip_categories + print("config finished") + save_config(config) + + + +if __name__ == "__main__": + print("starting") + main()