Also remove replies

main
bashrc 2026-01-14 20:52:12 +00:00
parent b2bd7950c9
commit fb495b0b52
1 changed files with 83 additions and 77 deletions

160
posts.py
View File

@ -5682,89 +5682,95 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
print('EX: archive_posts_for_person unable to write ' + print('EX: archive_posts_for_person unable to write ' +
index_filename) index_filename)
# remove any edits # remove any edits or replies
posts_in_box = os.scandir(box_dir) for ext_name in ('edits', 'replies'):
edits_in_box_dict = {} ext = '.' + ext_name
edits_ctr = 0 posts_in_box = os.scandir(box_dir)
edits_removed_ctr = 0 edits_in_box_dict = {}
edit_files_ctr = 0 edits_ctr = 0
for post_filename in posts_in_box: edits_removed_ctr = 0
post_filename = post_filename.name edit_files_ctr = 0
if not post_filename.endswith('.edits'): for post_filename in posts_in_box:
continue post_filename = post_filename.name
original_post_filename = post_filename.replace('.edits', '.json') if not post_filename.endswith(ext):
full_original_filename = os.path.join(box_dir, original_post_filename)
full_filename = os.path.join(box_dir, post_filename)
if not os.path.isfile(full_original_filename):
# if the original file doesn't exist (was remotely deleted by its
# author) then remove the corresponding edits
try:
os.remove(full_filename)
edits_removed_ctr += 1
except OSError:
print('EX: unable to remove edits file ' + full_filename)
continue continue
edit_files_ctr += 1 original_post_filename = post_filename.replace(ext, '.json')
if os.path.isfile(full_filename): full_original_filename = \
content = '' os.path.join(box_dir, original_post_filename)
try: full_filename = os.path.join(box_dir, post_filename)
with open(full_filename, 'r', encoding='utf-8') as fp_content: if not os.path.isfile(full_original_filename):
content = fp_content.read() # if the original file doesn't exist (was remotely deleted by
except OSError: # its author) then remove the corresponding edits
print('EX: unable to open content 2 ' + full_filename)
if '"published":' in content:
published_str = content.split('"published":')[1]
if '"' in published_str:
published_str = published_str.split('"')[1]
# does this look like a date?
if 'T' in published_str and \
'-' in published_str and \
':' in published_str:
edits_in_box_dict[published_str] = post_filename
edits_ctr += 1
if edits_removed_ctr > 0:
print(str(edits_removed_ctr) + ' edits removed from ' + boxname +
' for ' + nickname + '@' + domain)
no_of_edits = edits_ctr
if no_of_edits <= max_posts_in_box:
print('Checked ' + str(no_of_edits) + ' ' + boxname +
' edits in ' + str(edit_files_ctr) +
' files for ' + nickname + '@' + domain)
else:
# sort the list in ascending order of date
edits_in_box_sorted = \
OrderedDict(sorted(edits_in_box_dict.items(), reverse=False))
remove_edits_ctr = 0
for published_str, edit_filename in edits_in_box_sorted.items():
file_path = os.path.join(box_dir, edit_filename)
if not os.path.isfile(file_path):
continue
if archive_dir:
archive_path = os.path.join(archive_dir, edit_filename)
try: try:
os.rename(file_path, archive_path) os.remove(full_filename)
remove_edits_ctr += 1 edits_removed_ctr += 1
except OSError: except OSError:
print('EX: ' + print('EX: unable to remove ' + ext_name + ' file ' +
'archive_posts_for_person unable to archive edit ' + full_filename)
file_path + ' -> ' + archive_path) continue
else: edit_files_ctr += 1
if os.path.isfile(full_filename):
content = ''
try: try:
os.remove(edit_filename) with open(full_filename, 'r',
remove_edits_ctr += 1 encoding='utf-8') as fp_content:
content = fp_content.read()
except OSError: except OSError:
print('EX: ' + print('EX: unable to open content 2 ' + full_filename)
'archive_posts_for_person unable to delete edit ' + if '"published":' in content:
edit_filename) published_str = content.split('"published":')[1]
if archive_dir: if '"' in published_str:
print('Archived ' + str(remove_edits_ctr) + ' ' + boxname + published_str = published_str.split('"')[1]
' edits for ' + nickname + '@' + domain) # does this look like a date?
if 'T' in published_str and \
'-' in published_str and \
':' in published_str:
edits_in_box_dict[published_str] = post_filename
edits_ctr += 1
if edits_removed_ctr > 0:
print(str(edits_removed_ctr) + ' ' + ext_name + ' removed from ' +
boxname + ' for ' + nickname + '@' + domain)
no_of_edits = edits_ctr
if no_of_edits <= max_posts_in_box:
print('Checked ' + str(no_of_edits) + ' ' + boxname +
' ' + ext_name + ' in ' + str(edit_files_ctr) +
' files for ' + nickname + '@' + domain)
else: else:
print('Removed ' + str(remove_edits_ctr) + ' ' + boxname + # sort the list in ascending order of date
' edits for ' + nickname + '@' + domain) edits_in_box_sorted = \
OrderedDict(sorted(edits_in_box_dict.items(), reverse=False))
remove_edits_ctr = 0
for published_str, edit_filename in edits_in_box_sorted.items():
file_path = os.path.join(box_dir, edit_filename)
if not os.path.isfile(file_path):
continue
if archive_dir:
archive_path = os.path.join(archive_dir, edit_filename)
try:
os.rename(file_path, archive_path)
remove_edits_ctr += 1
except OSError:
print('EX: ' +
'archive_posts_for_person unable to archive ' +
ext_name + ' ' + file_path + ' -> ' +
archive_path)
else:
try:
os.remove(edit_filename)
remove_edits_ctr += 1
except OSError:
print('EX: ' +
'archive_posts_for_person unable to delete ' +
ext_name + ' ' + edit_filename)
if archive_dir:
print('Archived ' + str(remove_edits_ctr) + ' ' + boxname +
' ' + ext_name + ' for ' + nickname + '@' + domain)
else:
print('Removed ' + str(remove_edits_ctr) + ' ' + boxname +
' ' + ext_name + ' for ' + nickname + '@' + domain)
posts_in_box_dict = {} posts_in_box_dict = {}
posts_ctr = 0 posts_ctr = 0