diff --git a/auto-selfcontrol.py b/auto-selfcontrol.py index 9b360d4..a47d841 100755 --- a/auto-selfcontrol.py +++ b/auto-selfcontrol.py @@ -41,7 +41,15 @@ def load_config(path): exit_with_error("The JSON config file {configfile} is not correctly formatted." "The following exception was raised:\ \n{exc}".format(configfile=path, exc=exception)) - + if config.get("host-blacklist-paths",None) is not None: + # add to config["host-blacklist"] if it exists + blacklist = set(config["host-blacklist"]) if config.get("host-blacklist", None) is not None else set() + for blacklist_filename in config["host-blacklist-paths"]: + blacklist_location = os.path.realpath(os.path.join(os.getcwd(),blacklist_filename)) + with open(blacklist_location,"rt") as bl: + for host in bl: + blacklist.add(host.strip()) + config["host-blacklist"] = list(blacklist) return config @@ -247,8 +255,20 @@ def get_end_date_of_schedule(schedule): def get_schedule_weekdays(schedule): + """Return a list of weekdays the specified schedule is active.""" - return [schedule["weekday"]] if schedule.get("weekday", None) is not None else range(1, 8) + if schedule.get("weekday", None) is not None: + if schedule["weekday"] == "weekdays": + return range(1,6) + elif schedule["weekday"] == "weekends": + return range(6,8) + elif type(schedule["weekday"]) == list: + return schedule["weekday"] + elif type(schedule["weekday"]) == int: + return [schedule["weekday"]] + else: + return range(1,8) + def set_selfcontrol_setting(key, value, username): diff --git a/config.json b/config.json index 418ef92..fb2a058 100644 --- a/config.json +++ b/config.json @@ -1,59 +1,38 @@ { - "username": "MY_USERNAME", - "selfcontrol-path": "/Applications/SelfControl.app", - "host-blacklist": [ - "twitter.com", - "reddit.com" - ], - "block-schedules":[ - { - "weekday": 1, - "start-hour": 8, - "start-minute": 0, - "end-hour": 17, - "end-minute": 0 - }, - { - "weekday": 2, - "start-hour": 8, - "start-minute": 0, - "end-hour": 17, - "end-minute": 0 - }, - { - "weekday": 3, - "start-hour": 8, - "start-minute": 0, - "end-hour": 17, - "end-minute": 0 - }, - { - "weekday": 4, - "start-hour": 8, - "start-minute": 0, - "end-hour": 17, - "end-minute": 0 - }, - { - "weekday": 5, - "start-hour": 8, - "start-minute": 0, - "end-hour": 17, - "end-minute": 0 - }, - { - "weekday": 6, - "start-hour": 8, - "start-minute": 0, - "end-hour": 17, - "end-minute": 0 - }, - { - "weekday": 7, - "start-hour": 8, - "start-minute": 0, - "end-hour": 17, - "end-minute": 0 - } - ] + "username": "shiv", + "selfcontrol-path": "/Applications/SelfControl.app", + "host-blacklist": [], + "host-blacklist-paths": [ + "blacklist.txt" + ], + "block-schedules": [ + { + "weekday": "weekdays", + "start-hour": 10, + "start-minute": 0, + "end-hour": 13, + "end-minute": 0 + }, + { + "weekday": "weekdays", + "start-hour": 14, + "start-minute": 30, + "end-hour": 18, + "end-minute": 0 + }, + { + "weekday": "weekdays", + "start-hour": 20, + "start-minute": 0, + "end-hour": 22, + "end-minute": 0 + }, + { + "weekday": "weekends", + "start-hour": 10, + "start-minute": 0, + "end-hour": 13, + "end-minute": 0 + } + ] } \ No newline at end of file