From d47dbd76531bcd8eb4009f39d498c0954656ed29 Mon Sep 17 00:00:00 2001 From: Jason Weiser Date: Sat, 28 Sep 2024 00:47:23 -0400 Subject: [PATCH] undid relative path changes --- bot/bot.py | 21 ++++++++++++++------- bot/run_list.py | 13 ++++++++----- bot/tweet_types.py | 16 ++++++++-------- bot/useful_resources.py | 10 ++++++++-- 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 623d24b..5be1142 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -10,6 +10,7 @@ from useful_resources import log_this from useful_resources import pull_config from useful_resources import connection_validator +from pathlib import Path from platforms import Masto, Bluesky, Twitter from run_list import RunList @@ -29,11 +30,17 @@ dest='yes', action='store_true') args=parser.parse_args() +## Find parent directory +running_file = Path(__file__) +current_dir = running_file.resolve().parents[0] +parent_dir = running_file.resolve().parents[1] +pickle_dir = os.path.join(parent_dir, "data/number.p") + ## Load the config options = pull_config('SETUP') ##Define objects -runlist = RunList(options['LIST_LOCATION']) +runlist = RunList(parent_dir, options['LIST_LOCATION']) destinations = ['TWITTER','MASTODON','BLUESKY'] ## Program run @@ -42,7 +49,7 @@ #https://docs.python.org/3/library/pickle.html def make_pickle(): initial_num = 0 - pickle.dump(initial_num, open("../data/number.p","wb")) + pickle.dump(initial_num, open(pickle_dir,"wb")) def actually_post(tweet,platform_to_post): platform_options = pull_config(platform_to_post) @@ -73,18 +80,18 @@ def actually_post(tweet,platform_to_post): def tweet_it(): if options['TYPE'] == "sequential": - post_content = tweet_types.s_run() + post_content = tweet_types.s_run(parent_dir) for i in destinations: actually_post(post_content, i) elif options['TYPE'] == "random": - post_content = tweet_types.r_run(options['LIST_LOCATION']) + post_content = tweet_types.r_run(parent_dir,options['LIST_LOCATION']) for i in destinations: actually_post(post_content, i) def lets_post(): where_list = pull_config('SETUP')['LIST_LOCATION'] - if not os.path.isdir("../data/"): - os.mkdir("../data/") + if not os.path.isdir(os.path.join(parent_dir, "data/")): + os.mkdir(os.path.join(parent_dir, "data/")) else: pass if not (options['TYPE'] == "sequential" or options['TYPE'] == "random"): @@ -108,7 +115,7 @@ def lets_post(): sys.exit() #if there isn't a numbering file in place and you want sequential tweets #this makes a number file - if not os.path.isfile("../data/number.p"): + if not os.path.isfile(pickle_dir): make_pickle() log_this("Numbering started and set to zero") else: diff --git a/bot/run_list.py b/bot/run_list.py index 47ff92a..4284442 100644 --- a/bot/run_list.py +++ b/bot/run_list.py @@ -9,10 +9,12 @@ import os class RunList: - jsonfile = '../data/quotes.json' - def __init__(self, list_location): + jsonfile = 'data/quotes.json' + def __init__(self, parent, list_location): + self.parent = parent self.list_location = list_location - self.cached_list = '../data/cached.txt' + self.cached_list = os.path.join(self.parent,'data/cached.txt') + def validate_char(self, platform, text_row): if len(text_row) > pull_config(platform)["CHARACTER_LIMIT"] \ @@ -123,8 +125,9 @@ def runit(self): json_list = self.list_to_json(working_list) working_list = self.cached_list json_list = self.list_to_json(working_list) - - file = open(self.jsonfile, 'w') + + working_json = os.path.join(self.parent, self.jsonfile) + file = open(working_json, 'w') file.write(json.dumps(json_list, indent=4)) file.close diff --git a/bot/tweet_types.py b/bot/tweet_types.py index 9a106c7..6e9155d 100644 --- a/bot/tweet_types.py +++ b/bot/tweet_types.py @@ -15,8 +15,8 @@ def add_hashtags(base_post, where_posting): else: return base_post -def r_run(list_loc): - with open('../data/quotes.json',"r") as json_file: +def r_run(parent_dir, list_loc): + with open(os.path.join(parent_dir, 'data/quotes.json'),"r") as json_file: data = json.load(json_file) n = random.randint(0,len(data)-1) tweet = "{}".format(data[n]) @@ -24,23 +24,23 @@ def r_run(list_loc): working_data.pop(n) if len(working_data) == 0: - runlist = RunList(list_loc) + runlist = RunList(parent_dir,list_loc) runlist.runit() else: - with open('../data/quotes.json', "w") as f: + with open(os.path.join(parent_dir,'data/quotes.json'), "w") as f: json.dump(working_data, f, indent=4) return tweet -def s_run(): - with open('../data/quotes.json') as json_file: +def s_run(parent_dir): + with open(os.path.join(parent_dir, 'data/quotes.json')) as json_file: data = json.load(json_file) - n = pickle.load('../data/number.p',"rb") + n = pickle.load(open(os.path.join(parent_dir,'data/number.p'),"rb")) tweet = "{}".format(data[n]) if n == len(data)-1: n = 0 else: n += 1 - pickle.dump(n, open('../data/number.p', "wb")) + pickle.dump(n, open(os.path.join(parent_dir,'data/number.p'), "wb")) return tweet if __name__ == "__main__": diff --git a/bot/useful_resources.py b/bot/useful_resources.py index e5cf87a..b8cf4e3 100644 --- a/bot/useful_resources.py +++ b/bot/useful_resources.py @@ -2,10 +2,16 @@ import os import logging import yaml +from pathlib import Path + +## Find parent directory +running_file = Path(__file__) +current_dir = running_file.resolve().parents[0] +parent_dir = running_file.resolve().parents[1] def log_this(input_for_log): ## Logging - logging_file = '../data/bot.log' + logging_file = os.path.join(parent_dir, 'data/bot.log') if not os.path.isfile(logging_file): log_file = open(logging_file, 'x') log_file.close() @@ -27,6 +33,6 @@ def connection_validator(url_to_check): return response.status_code def pull_config(section): - with open("./config.yaml") as config_yml: + with open(os.path.join(current_dir,'config.yaml')) as config_yml: config = yaml.safe_load(config_yml) return config[section]