From 7d82c7e435d1215869b3e966978c6a2cc9d7ee58 Mon Sep 17 00:00:00 2001 From: Shiv Date: Fri, 24 Apr 2020 11:20:41 +0100 Subject: [PATCH 1/3] allowed for easier input of schedule --- auto-selfcontrol.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/auto-selfcontrol.py b/auto-selfcontrol.py index ee70fff..25d2784 100755 --- a/auto-selfcontrol.py +++ b/auto-selfcontrol.py @@ -109,7 +109,17 @@ def get_duration_minutes(endhour, endminute): def get_schedule_weekdays(schedule): """ returns 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): From e558e2550301387e9e2c0b0ee82d483909966655 Mon Sep 17 00:00:00 2001 From: Shiv Date: Fri, 24 Apr 2020 20:54:21 +0100 Subject: [PATCH 2/3] imports blacklist from text file --- auto-selfcontrol.py | 6 ++- config.json | 93 ++++++++++++++++++--------------------------- 2 files changed, 41 insertions(+), 58 deletions(-) diff --git a/auto-selfcontrol.py b/auto-selfcontrol.py index 25d2784..70a78f8 100755 --- a/auto-selfcontrol.py +++ b/auto-selfcontrol.py @@ -26,7 +26,11 @@ def load_config(config_files): except ValueError as e: exit_with_error("The json config file {configfile} is not correctly formatted." \ "The following exception was raised:\n{exc}".format(configfile=f, exc=e)) - + if config.get("host-blacklist-path",None) is not None: + # overwrite config["host-blacklist"] + blacklist_location = os.path.realpath(os.path.join(os.getcwd(),config["host-blacklist-path"])) + with open(blacklist_location,"rt") as bl: + config["host-blacklist"] = [x.strip() for x in bl] return config diff --git a/config.json b/config.json index 418ef92..42e046b 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": [ + "placeholder" + ], + "host-blacklist-path": "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 From 10715254e192b87d926541a423e1b7998b310962 Mon Sep 17 00:00:00 2001 From: Shiv Date: Fri, 24 Apr 2020 21:09:03 +0100 Subject: [PATCH 3/3] allows import from multiple blacklists --- auto-selfcontrol.py | 14 +++++++++----- config.json | 6 +++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/auto-selfcontrol.py b/auto-selfcontrol.py index 70a78f8..21c5be8 100755 --- a/auto-selfcontrol.py +++ b/auto-selfcontrol.py @@ -26,11 +26,15 @@ def load_config(config_files): except ValueError as e: exit_with_error("The json config file {configfile} is not correctly formatted." \ "The following exception was raised:\n{exc}".format(configfile=f, exc=e)) - if config.get("host-blacklist-path",None) is not None: - # overwrite config["host-blacklist"] - blacklist_location = os.path.realpath(os.path.join(os.getcwd(),config["host-blacklist-path"])) - with open(blacklist_location,"rt") as bl: - config["host-blacklist"] = [x.strip() for x in bl] + 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 diff --git a/config.json b/config.json index 42e046b..fb2a058 100644 --- a/config.json +++ b/config.json @@ -1,10 +1,10 @@ { "username": "shiv", "selfcontrol-path": "/Applications/SelfControl.app", - "host-blacklist": [ - "placeholder" + "host-blacklist": [], + "host-blacklist-paths": [ + "blacklist.txt" ], - "host-blacklist-path": "blacklist.txt", "block-schedules": [ { "weekday": "weekdays",