Skip to content

Commit

Permalink
Merge pull request #5 from jason-weiser/relative-fix
Browse files Browse the repository at this point in the history
Undid relative path changes
  • Loading branch information
jason-weiser authored Sep 28, 2024
2 parents 5e5f649 + d47dbd7 commit 1478ac5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
21 changes: 14 additions & 7 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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"):
Expand All @@ -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:
Expand Down
13 changes: 8 additions & 5 deletions bot/run_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"] \
Expand Down Expand Up @@ -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

Expand Down
16 changes: 8 additions & 8 deletions bot/tweet_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,32 @@ 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])
working_data = data[:]
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__":
Expand Down
10 changes: 8 additions & 2 deletions bot/useful_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]

0 comments on commit 1478ac5

Please sign in to comment.