mirror of https://gitlab.com/bashrc2/epicyon
Snake case
parent
0d5038f204
commit
3dfbb2bfc8
108
schedule.py
108
schedule.py
|
@ -19,32 +19,32 @@ from outbox import post_message_to_outbox
|
||||||
|
|
||||||
|
|
||||||
def _update_post_schedule(base_dir: str, handle: str, httpd,
|
def _update_post_schedule(base_dir: str, handle: str, httpd,
|
||||||
maxScheduledPosts: int) -> None:
|
max_scheduled_posts: int) -> None:
|
||||||
"""Checks if posts are due to be delivered and if so moves them to the outbox
|
"""Checks if posts are due to be delivered and if so moves them to the outbox
|
||||||
"""
|
"""
|
||||||
scheduleIndexFilename = \
|
schedule_index_filename = \
|
||||||
base_dir + '/accounts/' + handle + '/schedule.index'
|
base_dir + '/accounts/' + handle + '/schedule.index'
|
||||||
if not os.path.isfile(scheduleIndexFilename):
|
if not os.path.isfile(schedule_index_filename):
|
||||||
return
|
return
|
||||||
|
|
||||||
# get the current time as an int
|
# get the current time as an int
|
||||||
curr_time = datetime.datetime.utcnow()
|
curr_time = datetime.datetime.utcnow()
|
||||||
daysSinceEpoch = (curr_time - datetime.datetime(1970, 1, 1)).days
|
days_since_epoch = (curr_time - datetime.datetime(1970, 1, 1)).days
|
||||||
|
|
||||||
scheduleDir = base_dir + '/accounts/' + handle + '/scheduled/'
|
schedule_dir = base_dir + '/accounts/' + handle + '/scheduled/'
|
||||||
indexLines = []
|
index_lines = []
|
||||||
deleteSchedulePost = False
|
delete_schedule_post = False
|
||||||
nickname = handle.split('@')[0]
|
nickname = handle.split('@')[0]
|
||||||
with open(scheduleIndexFilename, 'r') as fp:
|
with open(schedule_index_filename, 'r') as sched_file:
|
||||||
for line in fp:
|
for line in sched_file:
|
||||||
if ' ' not in line:
|
if ' ' not in line:
|
||||||
continue
|
continue
|
||||||
dateStr = line.split(' ')[0]
|
date_str = line.split(' ')[0]
|
||||||
if 'T' not in dateStr:
|
if 'T' not in date_str:
|
||||||
continue
|
continue
|
||||||
post_id = line.split(' ', 1)[1].replace('\n', '').replace('\r', '')
|
post_id = line.split(' ', 1)[1].replace('\n', '').replace('\r', '')
|
||||||
post_filename = scheduleDir + post_id + '.json'
|
post_filename = schedule_dir + post_id + '.json'
|
||||||
if deleteSchedulePost:
|
if delete_schedule_post:
|
||||||
# delete extraneous scheduled posts
|
# delete extraneous scheduled posts
|
||||||
if os.path.isfile(post_filename):
|
if os.path.isfile(post_filename):
|
||||||
try:
|
try:
|
||||||
|
@ -54,35 +54,35 @@ def _update_post_schedule(base_dir: str, handle: str, httpd,
|
||||||
str(post_filename))
|
str(post_filename))
|
||||||
continue
|
continue
|
||||||
# create the new index file
|
# create the new index file
|
||||||
indexLines.append(line)
|
index_lines.append(line)
|
||||||
# convert string date to int
|
# convert string date to int
|
||||||
postTime = \
|
post_time = \
|
||||||
datetime.datetime.strptime(dateStr, "%Y-%m-%dT%H:%M:%S%z")
|
datetime.datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S%z")
|
||||||
postTime = postTime.replace(tzinfo=None)
|
post_time = post_time.replace(tzinfo=None)
|
||||||
postDaysSinceEpoch = \
|
post_days_since_epoch = \
|
||||||
(postTime - datetime.datetime(1970, 1, 1)).days
|
(post_time - datetime.datetime(1970, 1, 1)).days
|
||||||
if daysSinceEpoch < postDaysSinceEpoch:
|
if days_since_epoch < post_days_since_epoch:
|
||||||
continue
|
continue
|
||||||
if daysSinceEpoch == postDaysSinceEpoch:
|
if days_since_epoch == post_days_since_epoch:
|
||||||
if curr_time.time().hour < postTime.time().hour:
|
if curr_time.time().hour < post_time.time().hour:
|
||||||
continue
|
continue
|
||||||
if curr_time.time().minute < postTime.time().minute:
|
if curr_time.time().minute < post_time.time().minute:
|
||||||
continue
|
continue
|
||||||
if not os.path.isfile(post_filename):
|
if not os.path.isfile(post_filename):
|
||||||
print('WARN: schedule missing post_filename=' + post_filename)
|
print('WARN: schedule missing post_filename=' + post_filename)
|
||||||
indexLines.remove(line)
|
index_lines.remove(line)
|
||||||
continue
|
continue
|
||||||
# load post
|
# load post
|
||||||
post_json_object = load_json(post_filename)
|
post_json_object = load_json(post_filename)
|
||||||
if not post_json_object:
|
if not post_json_object:
|
||||||
print('WARN: schedule json not loaded')
|
print('WARN: schedule json not loaded')
|
||||||
indexLines.remove(line)
|
index_lines.remove(line)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# set the published time
|
# set the published time
|
||||||
# If this is not recent then http checks on the receiving side
|
# If this is not recent then http checks on the receiving side
|
||||||
# will reject it
|
# will reject it
|
||||||
statusNumber, published = get_status_number()
|
_, published = get_status_number()
|
||||||
if post_json_object.get('published'):
|
if post_json_object.get('published'):
|
||||||
post_json_object['published'] = published
|
post_json_object['published'] = published
|
||||||
if has_object_dict(post_json_object):
|
if has_object_dict(post_json_object):
|
||||||
|
@ -130,7 +130,7 @@ def _update_post_schedule(base_dir: str, handle: str, httpd,
|
||||||
httpd.cw_lists,
|
httpd.cw_lists,
|
||||||
httpd.lists_enabled,
|
httpd.lists_enabled,
|
||||||
httpd.content_license_url):
|
httpd.content_license_url):
|
||||||
indexLines.remove(line)
|
index_lines.remove(line)
|
||||||
try:
|
try:
|
||||||
os.remove(post_filename)
|
os.remove(post_filename)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
@ -139,43 +139,43 @@ def _update_post_schedule(base_dir: str, handle: str, httpd,
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# move to the outbox
|
# move to the outbox
|
||||||
outboxPostFilename = \
|
outbox_post_filename = \
|
||||||
post_filename.replace('/scheduled/', '/outbox/')
|
post_filename.replace('/scheduled/', '/outbox/')
|
||||||
os.rename(post_filename, outboxPostFilename)
|
os.rename(post_filename, outbox_post_filename)
|
||||||
|
|
||||||
print('Scheduled post sent ' + post_id)
|
print('Scheduled post sent ' + post_id)
|
||||||
|
|
||||||
indexLines.remove(line)
|
index_lines.remove(line)
|
||||||
if len(indexLines) > maxScheduledPosts:
|
if len(index_lines) > max_scheduled_posts:
|
||||||
deleteSchedulePost = True
|
delete_schedule_post = True
|
||||||
|
|
||||||
# write the new schedule index file
|
# write the new schedule index file
|
||||||
scheduleIndexFile = \
|
schedule_index_file = \
|
||||||
base_dir + '/accounts/' + handle + '/schedule.index'
|
base_dir + '/accounts/' + handle + '/schedule.index'
|
||||||
with open(scheduleIndexFile, 'w+') as scheduleFile:
|
with open(schedule_index_file, 'w+') as schedule_file:
|
||||||
for line in indexLines:
|
for line in index_lines:
|
||||||
scheduleFile.write(line)
|
schedule_file.write(line)
|
||||||
|
|
||||||
|
|
||||||
def run_post_schedule(base_dir: str, httpd, maxScheduledPosts: int):
|
def run_post_schedule(base_dir: str, httpd, max_scheduled_posts: int):
|
||||||
"""Dispatches scheduled posts
|
"""Dispatches scheduled posts
|
||||||
"""
|
"""
|
||||||
while True:
|
while True:
|
||||||
time.sleep(60)
|
time.sleep(60)
|
||||||
# for each account
|
# for each account
|
||||||
for subdir, dirs, files in os.walk(base_dir + '/accounts'):
|
for _, dirs, _ in os.walk(base_dir + '/accounts'):
|
||||||
for account in dirs:
|
for account in dirs:
|
||||||
if '@' not in account:
|
if '@' not in account:
|
||||||
continue
|
continue
|
||||||
if not is_account_dir(account):
|
if not is_account_dir(account):
|
||||||
continue
|
continue
|
||||||
# scheduled posts index for this account
|
# scheduled posts index for this account
|
||||||
scheduleIndexFilename = \
|
schedule_index_filename = \
|
||||||
base_dir + '/accounts/' + account + '/schedule.index'
|
base_dir + '/accounts/' + account + '/schedule.index'
|
||||||
if not os.path.isfile(scheduleIndexFilename):
|
if not os.path.isfile(schedule_index_filename):
|
||||||
continue
|
continue
|
||||||
_update_post_schedule(base_dir, account,
|
_update_post_schedule(base_dir, account,
|
||||||
httpd, maxScheduledPosts)
|
httpd, max_scheduled_posts)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
|
@ -183,7 +183,7 @@ def run_post_schedule_watchdog(project_version: str, httpd) -> None:
|
||||||
"""This tries to keep the scheduled post thread running even if it dies
|
"""This tries to keep the scheduled post thread running even if it dies
|
||||||
"""
|
"""
|
||||||
print('Starting scheduled post watchdog')
|
print('Starting scheduled post watchdog')
|
||||||
postScheduleOriginal = \
|
post_schedule_original = \
|
||||||
httpd.thrPostSchedule.clone(run_post_schedule)
|
httpd.thrPostSchedule.clone(run_post_schedule)
|
||||||
httpd.thrPostSchedule.start()
|
httpd.thrPostSchedule.start()
|
||||||
while True:
|
while True:
|
||||||
|
@ -192,7 +192,7 @@ def run_post_schedule_watchdog(project_version: str, httpd) -> None:
|
||||||
continue
|
continue
|
||||||
httpd.thrPostSchedule.kill()
|
httpd.thrPostSchedule.kill()
|
||||||
httpd.thrPostSchedule = \
|
httpd.thrPostSchedule = \
|
||||||
postScheduleOriginal.clone(run_post_schedule)
|
post_schedule_original.clone(run_post_schedule)
|
||||||
httpd.thrPostSchedule.start()
|
httpd.thrPostSchedule.start()
|
||||||
print('Restarting scheduled posts...')
|
print('Restarting scheduled posts...')
|
||||||
|
|
||||||
|
@ -201,23 +201,23 @@ def remove_scheduled_posts(base_dir: str, nickname: str, domain: str) -> None:
|
||||||
"""Removes any scheduled posts
|
"""Removes any scheduled posts
|
||||||
"""
|
"""
|
||||||
# remove the index
|
# remove the index
|
||||||
scheduleIndexFilename = \
|
schedule_index_filename = \
|
||||||
acct_dir(base_dir, nickname, domain) + '/schedule.index'
|
acct_dir(base_dir, nickname, domain) + '/schedule.index'
|
||||||
if os.path.isfile(scheduleIndexFilename):
|
if os.path.isfile(schedule_index_filename):
|
||||||
try:
|
try:
|
||||||
os.remove(scheduleIndexFilename)
|
os.remove(schedule_index_filename)
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: remove_scheduled_posts unable to delete ' +
|
print('EX: remove_scheduled_posts unable to delete ' +
|
||||||
scheduleIndexFilename)
|
schedule_index_filename)
|
||||||
# remove the scheduled posts
|
# remove the scheduled posts
|
||||||
scheduledDir = acct_dir(base_dir, nickname, domain) + '/scheduled'
|
scheduled_dir = acct_dir(base_dir, nickname, domain) + '/scheduled'
|
||||||
if not os.path.isdir(scheduledDir):
|
if not os.path.isdir(scheduled_dir):
|
||||||
return
|
return
|
||||||
for scheduledPostFilename in os.listdir(scheduledDir):
|
for scheduled_post_filename in os.listdir(scheduled_dir):
|
||||||
filePath = os.path.join(scheduledDir, scheduledPostFilename)
|
file_path = os.path.join(scheduled_dir, scheduled_post_filename)
|
||||||
if os.path.isfile(filePath):
|
if os.path.isfile(file_path):
|
||||||
try:
|
try:
|
||||||
os.remove(filePath)
|
os.remove(file_path)
|
||||||
except OSError:
|
except OSError:
|
||||||
print('EX: remove_scheduled_posts unable to delete ' +
|
print('EX: remove_scheduled_posts unable to delete ' +
|
||||||
filePath)
|
file_path)
|
||||||
|
|
Loading…
Reference in New Issue