mirror of https://gitlab.com/bashrc2/epicyon
Replace file operations with functions
parent
733d4adee2
commit
664f13d3e2
64
petnames.py
64
petnames.py
|
|
@ -9,6 +9,9 @@ __module_group__ = "Core"
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from utils import acct_dir
|
from utils import acct_dir
|
||||||
|
from data import load_string
|
||||||
|
from data import save_string
|
||||||
|
from data import append_string
|
||||||
|
|
||||||
|
|
||||||
def set_pet_name(base_dir: str, nickname: str, domain: str,
|
def set_pet_name(base_dir: str, nickname: str, domain: str,
|
||||||
|
|
@ -28,13 +31,11 @@ def set_pet_name(base_dir: str, nickname: str, domain: str,
|
||||||
|
|
||||||
# does this entry already exist?
|
# does this entry already exist?
|
||||||
if os.path.isfile(petnames_filename):
|
if os.path.isfile(petnames_filename):
|
||||||
petnames_str = ''
|
petnames_str: str = \
|
||||||
try:
|
load_string(petnames_filename,
|
||||||
with open(petnames_filename, 'r',
|
'EX: set_pet_name unable to read ' + petnames_filename)
|
||||||
encoding='utf-8') as fp_petnames:
|
if petnames_str is None:
|
||||||
petnames_str = fp_petnames.read()
|
petnames_str = ''
|
||||||
except OSError:
|
|
||||||
print('EX: set_pet_name unable to read ' + petnames_filename)
|
|
||||||
if entry in petnames_str:
|
if entry in petnames_str:
|
||||||
return True
|
return True
|
||||||
if ' ' + handle + '\n' in petnames_str:
|
if ' ' + handle + '\n' in petnames_str:
|
||||||
|
|
@ -46,30 +47,22 @@ def set_pet_name(base_dir: str, nickname: str, domain: str,
|
||||||
else:
|
else:
|
||||||
new_petnames_str += entry
|
new_petnames_str += entry
|
||||||
# save the updated petnames file
|
# save the updated petnames file
|
||||||
try:
|
if not save_string(new_petnames_str, petnames_filename,
|
||||||
with open(petnames_filename, 'w+',
|
'EX: set_pet_name unable to save ' +
|
||||||
encoding='utf-8') as fp_petnames:
|
petnames_filename):
|
||||||
fp_petnames.write(new_petnames_str)
|
|
||||||
except OSError:
|
|
||||||
print('EX: set_pet_name unable to save ' + petnames_filename)
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
# entry does not exist in the petnames file
|
# entry does not exist in the petnames file
|
||||||
try:
|
if not append_string(entry, petnames_filename,
|
||||||
with open(petnames_filename, 'a+',
|
'EX: set_pet_name unable to append ' +
|
||||||
encoding='utf-8') as fp_petnames:
|
petnames_filename):
|
||||||
fp_petnames.write(entry)
|
|
||||||
except OSError:
|
|
||||||
print('EX: set_pet_name unable to append ' + petnames_filename)
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# first entry
|
# first entry
|
||||||
try:
|
if not save_string(entry, petnames_filename,
|
||||||
with open(petnames_filename, 'w+', encoding='utf-8') as fp_petnames:
|
'EX: set_pet_name unable to write ' +
|
||||||
fp_petnames.write(entry)
|
petnames_filename):
|
||||||
except OSError:
|
|
||||||
print('EX: set_pet_name unable to write ' + petnames_filename)
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
@ -86,12 +79,11 @@ def get_pet_name(base_dir: str, nickname: str, domain: str,
|
||||||
|
|
||||||
if not os.path.isfile(petnames_filename):
|
if not os.path.isfile(petnames_filename):
|
||||||
return ''
|
return ''
|
||||||
petnames_str = ''
|
petnames_str: str = \
|
||||||
try:
|
load_string(petnames_filename,
|
||||||
with open(petnames_filename, 'r', encoding='utf-8') as fp_petnames:
|
'EX: get_pet_name unable to read ' + petnames_filename)
|
||||||
petnames_str = fp_petnames.read()
|
if petnames_str is None:
|
||||||
except OSError:
|
petnames_str = ''
|
||||||
print('EX: get_pet_name unable to read ' + petnames_filename)
|
|
||||||
if ' ' + handle + '\n' in petnames_str:
|
if ' ' + handle + '\n' in petnames_str:
|
||||||
petnames_list = petnames_str.split('\n')
|
petnames_list = petnames_str.split('\n')
|
||||||
for pet in petnames_list:
|
for pet in petnames_list:
|
||||||
|
|
@ -117,12 +109,12 @@ def _get_pet_name_handle(base_dir: str, nickname: str, domain: str,
|
||||||
|
|
||||||
if not os.path.isfile(petnames_filename):
|
if not os.path.isfile(petnames_filename):
|
||||||
return ''
|
return ''
|
||||||
petnames_str = ''
|
petnames_str: str = \
|
||||||
try:
|
load_string(petnames_filename,
|
||||||
with open(petnames_filename, 'r', encoding='utf-8') as fp_petnames:
|
'EX: _get_pet_name_handle unable to read ' +
|
||||||
petnames_str = fp_petnames.read()
|
petnames_filename)
|
||||||
except OSError:
|
if petnames_str is None:
|
||||||
print('EX: _get_pet_name_handle unable to read ' + petnames_filename)
|
petnames_str = ''
|
||||||
if petname + ' ' in petnames_str:
|
if petname + ' ' in petnames_str:
|
||||||
petnames_list = petnames_str.split('\n')
|
petnames_list = petnames_str.split('\n')
|
||||||
for pet in petnames_list:
|
for pet in petnames_list:
|
||||||
|
|
|
||||||
19
poison.py
19
poison.py
|
|
@ -11,6 +11,7 @@ __module_group__ = "Core"
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
from random import randint
|
from random import randint
|
||||||
|
from data import load_string
|
||||||
|
|
||||||
common_nouns = (
|
common_nouns = (
|
||||||
"time",
|
"time",
|
||||||
|
|
@ -1979,11 +1980,10 @@ def load_dictionary(base_dir: str) -> []:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
words: list[str] = []
|
words: list[str] = []
|
||||||
try:
|
words_str = load_string(filename,
|
||||||
with open(filename, 'r', encoding='utf-8') as fp_dict:
|
'EX: unable to load dictionary ' + filename)
|
||||||
words = fp_dict.read().split('\n')
|
if words_str:
|
||||||
except OSError:
|
words = words_str.split('\n')
|
||||||
print('EX: unable to load dictionary ' + filename)
|
|
||||||
return words
|
return words
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1998,11 +1998,10 @@ def load_2grams(base_dir: str) -> {}:
|
||||||
|
|
||||||
twograms = {}
|
twograms = {}
|
||||||
lines: list[str] = []
|
lines: list[str] = []
|
||||||
try:
|
lines_str = load_string(filename,
|
||||||
with open(filename, 'r', encoding='utf-8') as fp_dict:
|
'EX: unable to load 2-grams ' + filename)
|
||||||
lines = fp_dict.read().split('\n')
|
if lines_str:
|
||||||
except OSError:
|
lines = lines_str.split('\n')
|
||||||
print('EX: unable to load 2-grams ' + filename)
|
|
||||||
for line_str in lines:
|
for line_str in lines:
|
||||||
words = line_str.split('\t')
|
words = line_str.split('\t')
|
||||||
if len(words) != 3:
|
if len(words) != 3:
|
||||||
|
|
|
||||||
329
posts.py
329
posts.py
|
|
@ -145,6 +145,9 @@ from conversation import conversation_tag_to_convthread_id
|
||||||
from conversation import post_id_to_convthread_id
|
from conversation import post_id_to_convthread_id
|
||||||
from quote import quote_toots_allowed
|
from quote import quote_toots_allowed
|
||||||
from data import load_list
|
from data import load_list
|
||||||
|
from data import load_string
|
||||||
|
from data import save_string
|
||||||
|
from data import append_string
|
||||||
|
|
||||||
|
|
||||||
def convert_post_content_to_html(message_json: {}) -> None:
|
def convert_post_content_to_html(message_json: {}) -> None:
|
||||||
|
|
@ -1000,12 +1003,9 @@ def _save_last_published(base_dir: str, nickname: str, domain: str,
|
||||||
|
|
||||||
published_filename = \
|
published_filename = \
|
||||||
acct_dir(base_dir, nickname, domain) + '/.last_published'
|
acct_dir(base_dir, nickname, domain) + '/.last_published'
|
||||||
try:
|
save_string(published, published_filename,
|
||||||
with open(published_filename, 'w+', encoding='utf-8') as fp_last:
|
'EX: unable to save last published time ' +
|
||||||
fp_last.write(published)
|
published_filename)
|
||||||
except OSError:
|
|
||||||
print('EX: unable to save last published time ' +
|
|
||||||
published_filename)
|
|
||||||
|
|
||||||
|
|
||||||
def save_post_to_box(base_dir: str, http_prefix: str, post_id: str,
|
def save_post_to_box(base_dir: str, http_prefix: str, post_id: str,
|
||||||
|
|
@ -1090,12 +1090,9 @@ def _update_hashtags_index(base_dir: str, tag: {}, new_post_id: str,
|
||||||
str(days_since_epoch) + ' ' + nickname + ' ' + \
|
str(days_since_epoch) + ' ' + nickname + ' ' + \
|
||||||
new_post_id + '\n'
|
new_post_id + '\n'
|
||||||
# create a new tags index file
|
# create a new tags index file
|
||||||
try:
|
save_string(tag_line, tags_filename,
|
||||||
with open(tags_filename, 'w+', encoding='utf-8') as fp_tags:
|
'EX: _update_hashtags_index unable to write tags file ' +
|
||||||
fp_tags.write(tag_line)
|
tags_filename)
|
||||||
except OSError:
|
|
||||||
print('EX: _update_hashtags_index unable to write tags file ' +
|
|
||||||
tags_filename)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if text_in_file(new_post_id, tags_filename):
|
if text_in_file(new_post_id, tags_filename):
|
||||||
|
|
@ -1142,13 +1139,9 @@ def _add_schedule_post(base_dir: str, nickname: str, domain: str,
|
||||||
schedule_index_filename + ' ' + str(ex))
|
schedule_index_filename + ' ' + str(ex))
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
save_string(index_str + '\n', schedule_index_filename,
|
||||||
with open(schedule_index_filename, 'w+',
|
'EX: Failed to write entry to scheduled posts index2 ' +
|
||||||
encoding='utf-8') as fp_schedule:
|
schedule_index_filename + ' [ex]')
|
||||||
fp_schedule.write(index_str + '\n')
|
|
||||||
except OSError as ex:
|
|
||||||
print('EX: Failed to write entry to scheduled posts index2 ' +
|
|
||||||
schedule_index_filename + ' ' + str(ex))
|
|
||||||
|
|
||||||
|
|
||||||
def _create_post_cw_from_reply(base_dir: str, nickname: str, domain: str,
|
def _create_post_cw_from_reply(base_dir: str, nickname: str, domain: str,
|
||||||
|
|
@ -1789,12 +1782,9 @@ def _create_post_mod_report(base_dir: str,
|
||||||
new_post['moderationStatus'] = 'pending'
|
new_post['moderationStatus'] = 'pending'
|
||||||
# save to index file
|
# save to index file
|
||||||
moderation_index_file = data_dir(base_dir) + '/moderation.txt'
|
moderation_index_file = data_dir(base_dir) + '/moderation.txt'
|
||||||
try:
|
append_string(new_post_id + '\n', moderation_index_file,
|
||||||
with open(moderation_index_file, 'a+', encoding='utf-8') as fp_mod:
|
'EX: unable to write moderation index file ' +
|
||||||
fp_mod.write(new_post_id + '\n')
|
moderation_index_file)
|
||||||
except OSError:
|
|
||||||
print('EX: unable to write moderation index file ' +
|
|
||||||
moderation_index_file)
|
|
||||||
|
|
||||||
|
|
||||||
def get_actor_from_in_reply_to(in_reply_to: str) -> str:
|
def get_actor_from_in_reply_to(in_reply_to: str) -> str:
|
||||||
|
|
@ -2176,11 +2166,8 @@ def pin_post2(base_dir: str, nickname: str, domain: str,
|
||||||
"""
|
"""
|
||||||
account_dir = acct_dir(base_dir, nickname, domain)
|
account_dir = acct_dir(base_dir, nickname, domain)
|
||||||
pinned_filename = account_dir + '/pinToProfile.txt'
|
pinned_filename = account_dir + '/pinToProfile.txt'
|
||||||
try:
|
save_string(pinned_content, pinned_filename,
|
||||||
with open(pinned_filename, 'w+', encoding='utf-8') as fp_pin:
|
'EX: unable to write ' + pinned_filename)
|
||||||
fp_pin.write(pinned_content)
|
|
||||||
except OSError:
|
|
||||||
print('EX: unable to write ' + pinned_filename)
|
|
||||||
|
|
||||||
|
|
||||||
def undo_pinned_post(base_dir: str, nickname: str, domain: str) -> None:
|
def undo_pinned_post(base_dir: str, nickname: str, domain: str) -> None:
|
||||||
|
|
@ -2206,13 +2193,10 @@ def get_pinned_post_as_json(base_dir: str, http_prefix: str,
|
||||||
pinned_post_json = {}
|
pinned_post_json = {}
|
||||||
actor = local_actor_url(http_prefix, nickname, domain_full)
|
actor = local_actor_url(http_prefix, nickname, domain_full)
|
||||||
if os.path.isfile(pinned_filename):
|
if os.path.isfile(pinned_filename):
|
||||||
pinned_content = None
|
pinned_content = \
|
||||||
try:
|
load_string(pinned_filename,
|
||||||
with open(pinned_filename, 'r', encoding='utf-8') as fp_pin:
|
'EX: get_pinned_post_as_json unable to read ' +
|
||||||
pinned_content = fp_pin.read()
|
pinned_filename)
|
||||||
except OSError:
|
|
||||||
print('EX: get_pinned_post_as_json unable to read ' +
|
|
||||||
pinned_filename)
|
|
||||||
if pinned_content:
|
if pinned_content:
|
||||||
pinned_post_json = {
|
pinned_post_json = {
|
||||||
'atomUri': actor + '/pinned',
|
'atomUri': actor + '/pinned',
|
||||||
|
|
@ -3003,11 +2987,9 @@ def create_report_post(base_dir: str,
|
||||||
new_report_file = acct_handle_dir(base_dir, handle) + '/.newReport'
|
new_report_file = acct_handle_dir(base_dir, handle) + '/.newReport'
|
||||||
if os.path.isfile(new_report_file):
|
if os.path.isfile(new_report_file):
|
||||||
continue
|
continue
|
||||||
try:
|
save_string(to_url + '/moderation', new_report_file,
|
||||||
with open(new_report_file, 'w+', encoding='utf-8') as fp_report:
|
'EX: create_report_post unable to write ' +
|
||||||
fp_report.write(to_url + '/moderation')
|
new_report_file)
|
||||||
except OSError:
|
|
||||||
print('EX: create_report_post unable to write ' + new_report_file)
|
|
||||||
|
|
||||||
return post_json_object
|
return post_json_object
|
||||||
|
|
||||||
|
|
@ -3024,23 +3006,15 @@ def _add_send_block(base_dir: str, nickname: str, domain: str,
|
||||||
inbox_url = inbox_url.replace('/inbox\n', '\n')
|
inbox_url = inbox_url.replace('/inbox\n', '\n')
|
||||||
|
|
||||||
if not os.path.isfile(send_block_filename):
|
if not os.path.isfile(send_block_filename):
|
||||||
try:
|
save_string(inbox_url, send_block_filename,
|
||||||
with open(send_block_filename, 'w+',
|
'EX: _add_send_block unable to create ' +
|
||||||
encoding='utf-8') as fp_blocks:
|
send_block_filename)
|
||||||
fp_blocks.write(inbox_url)
|
|
||||||
except OSError:
|
|
||||||
print('EX: _add_send_block unable to create ' +
|
|
||||||
send_block_filename)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if not text_in_file(inbox_url, send_block_filename, False):
|
if not text_in_file(inbox_url, send_block_filename, False):
|
||||||
try:
|
append_string(inbox_url, send_block_filename,
|
||||||
with open(send_block_filename, 'a+',
|
'EX: _add_send_block unable to write ' +
|
||||||
encoding='utf-8') as fp_blocks:
|
send_block_filename)
|
||||||
fp_blocks.write(inbox_url)
|
|
||||||
except OSError:
|
|
||||||
print('EX: _add_send_block unable to write ' +
|
|
||||||
send_block_filename)
|
|
||||||
|
|
||||||
|
|
||||||
def _remove_send_block(base_dir: str, nickname: str, domain: str,
|
def _remove_send_block(base_dir: str, nickname: str, domain: str,
|
||||||
|
|
@ -3060,22 +3034,16 @@ def _remove_send_block(base_dir: str, nickname: str, domain: str,
|
||||||
if not text_in_file(inbox_url, send_block_filename, False):
|
if not text_in_file(inbox_url, send_block_filename, False):
|
||||||
return
|
return
|
||||||
|
|
||||||
send_blocks_str = ''
|
send_blocks_str = \
|
||||||
try:
|
load_string(send_block_filename,
|
||||||
with open(send_block_filename, 'r',
|
'EX: _remove_send_block unable to read ' +
|
||||||
encoding='utf-8') as fp_blocks:
|
send_block_filename)
|
||||||
send_blocks_str = fp_blocks.read()
|
if send_blocks_str is None:
|
||||||
except OSError:
|
send_blocks_str = ''
|
||||||
print('EX: _remove_send_block unable to read ' +
|
|
||||||
send_block_filename)
|
|
||||||
send_blocks_str = send_blocks_str.replace(inbox_url, '')
|
send_blocks_str = send_blocks_str.replace(inbox_url, '')
|
||||||
try:
|
save_string(send_blocks_str, send_block_filename,
|
||||||
with open(send_block_filename, 'w+',
|
'EX: _remove_send_block unable to write ' +
|
||||||
encoding='utf-8') as fp_blocks:
|
send_block_filename)
|
||||||
fp_blocks.write(send_blocks_str)
|
|
||||||
except OSError:
|
|
||||||
print('EX: _remove_send_block unable to write ' +
|
|
||||||
send_block_filename)
|
|
||||||
|
|
||||||
|
|
||||||
def thread_send_post(session, post_json_str: str, federation_list: [],
|
def thread_send_post(session, post_json_str: str, federation_list: [],
|
||||||
|
|
@ -3154,21 +3122,13 @@ def thread_send_post(session, post_json_str: str, federation_list: [],
|
||||||
# save the log file
|
# save the log file
|
||||||
post_log_filename = base_dir + '/post.log'
|
post_log_filename = base_dir + '/post.log'
|
||||||
if os.path.isfile(post_log_filename):
|
if os.path.isfile(post_log_filename):
|
||||||
try:
|
append_string(log_str + '\n', post_log_filename,
|
||||||
with open(post_log_filename, 'a+',
|
'EX: thread_send_post unable to append ' +
|
||||||
encoding='utf-8') as fp_log:
|
post_log_filename)
|
||||||
fp_log.write(log_str + '\n')
|
|
||||||
except OSError:
|
|
||||||
print('EX: thread_send_post unable to append ' +
|
|
||||||
post_log_filename)
|
|
||||||
else:
|
else:
|
||||||
try:
|
save_string(log_str + '\n', post_log_filename,
|
||||||
with open(post_log_filename, 'w+',
|
'EX: thread_send_post unable to write ' +
|
||||||
encoding='utf-8') as fp_log:
|
post_log_filename)
|
||||||
fp_log.write(log_str + '\n')
|
|
||||||
except OSError:
|
|
||||||
print('EX: thread_send_post unable to write ' +
|
|
||||||
post_log_filename)
|
|
||||||
|
|
||||||
if post_result:
|
if post_result:
|
||||||
_remove_send_block(base_dir, nickname, domain, inbox_url)
|
_remove_send_block(base_dir, nickname, domain, inbox_url)
|
||||||
|
|
@ -4638,12 +4598,9 @@ def _add_post_to_timeline(file_path: str, boxname: str,
|
||||||
posts_in_box: [], box_actor: str) -> bool:
|
posts_in_box: [], box_actor: str) -> bool:
|
||||||
""" Reads a post from file and decides whether it is valid
|
""" Reads a post from file and decides whether it is valid
|
||||||
"""
|
"""
|
||||||
post_str = ''
|
post_str = load_string(file_path,
|
||||||
try:
|
'EX: _add_post_to_timeline unable to read ' +
|
||||||
with open(file_path, 'r', encoding='utf-8') as fp_post:
|
file_path)
|
||||||
post_str = fp_post.read()
|
|
||||||
except OSError:
|
|
||||||
print('EX: _add_post_to_timeline unable to read ' + file_path)
|
|
||||||
|
|
||||||
if not post_str:
|
if not post_str:
|
||||||
return False
|
return False
|
||||||
|
|
@ -4721,17 +4678,13 @@ def _locate_news_arrival(base_dir: str, domain: str,
|
||||||
account_dir = data_dir(base_dir) + '/news@' + domain + '/'
|
account_dir = data_dir(base_dir) + '/news@' + domain + '/'
|
||||||
post_filename = account_dir + 'outbox/' + post_url
|
post_filename = account_dir + 'outbox/' + post_url
|
||||||
if os.path.isfile(post_filename):
|
if os.path.isfile(post_filename):
|
||||||
try:
|
arrival = load_string(post_filename,
|
||||||
with open(post_filename, 'r', encoding='utf-8') as fp_arrival:
|
'EX: _locate_news_arrival unable to read ' +
|
||||||
arrival = fp_arrival.read()
|
post_filename)
|
||||||
if arrival:
|
if arrival:
|
||||||
arrival_date = \
|
arrival_date = \
|
||||||
date_from_string_format(arrival,
|
date_from_string_format(arrival, ["%Y-%m-%dT%H:%M:%S%z"])
|
||||||
["%Y-%m-%dT%H:%M:%S%z"])
|
return arrival_date
|
||||||
return arrival_date
|
|
||||||
except OSError:
|
|
||||||
print('EX: _locate_news_arrival unable to read ' + post_filename)
|
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -5462,13 +5415,12 @@ def _expire_posts_for_person(http_prefix: str, nickname: str, domain: str,
|
||||||
full_filename = os.path.join(box_dir, post_filename)
|
full_filename = os.path.join(box_dir, post_filename)
|
||||||
if not os.path.isfile(full_filename):
|
if not os.path.isfile(full_filename):
|
||||||
continue
|
continue
|
||||||
content = ''
|
content = \
|
||||||
try:
|
load_string(full_filename,
|
||||||
with open(full_filename, 'r', encoding='utf-8') as fp_content:
|
'EX: expire_posts_for_person unable to open content ' +
|
||||||
content = fp_content.read()
|
full_filename)
|
||||||
except OSError:
|
if content is None:
|
||||||
print('EX: expire_posts_for_person unable to open content ' +
|
content = ''
|
||||||
full_filename)
|
|
||||||
|
|
||||||
# Get time of publication
|
# Get time of publication
|
||||||
published_str = ''
|
published_str = ''
|
||||||
|
|
@ -5540,12 +5492,9 @@ def set_post_expiry_keep_dms(base_dir: str, nickname: str, domain: str,
|
||||||
print('EX: unable to write set_post_expiry_keep_dms False ' +
|
print('EX: unable to write set_post_expiry_keep_dms False ' +
|
||||||
expire_dms_filename)
|
expire_dms_filename)
|
||||||
return
|
return
|
||||||
try:
|
save_string('\n', expire_dms_filename,
|
||||||
with open(expire_dms_filename, 'w+', encoding='utf-8') as fp_expire:
|
'EX: unable to write set_post_expiry_keep_dms True ' +
|
||||||
fp_expire.write('\n')
|
expire_dms_filename)
|
||||||
except OSError:
|
|
||||||
print('EX: unable to write set_post_expiry_keep_dms True ' +
|
|
||||||
expire_dms_filename)
|
|
||||||
|
|
||||||
|
|
||||||
def expire_posts(base_dir: str, http_prefix: str,
|
def expire_posts(base_dir: str, http_prefix: str,
|
||||||
|
|
@ -5565,14 +5514,11 @@ def expire_posts(base_dir: str, http_prefix: str,
|
||||||
if not os.path.isfile(expire_posts_filename):
|
if not os.path.isfile(expire_posts_filename):
|
||||||
continue
|
continue
|
||||||
keep_dms = get_post_expiry_keep_dms(base_dir, nickname, domain)
|
keep_dms = get_post_expiry_keep_dms(base_dir, nickname, domain)
|
||||||
expire_days_str = None
|
expire_days_str = \
|
||||||
try:
|
load_string(expire_posts_filename,
|
||||||
with open(expire_posts_filename, 'r',
|
'EX: expire_posts failed to read days file ' +
|
||||||
encoding='utf-8') as fp_expire:
|
expire_posts_filename)
|
||||||
expire_days_str = fp_expire.read()
|
if expire_days_str is None:
|
||||||
except OSError:
|
|
||||||
print('EX: expire_posts failed to read days file ' +
|
|
||||||
expire_posts_filename)
|
|
||||||
continue
|
continue
|
||||||
if not expire_days_str:
|
if not expire_days_str:
|
||||||
continue
|
continue
|
||||||
|
|
@ -5599,13 +5545,9 @@ def get_post_expiry_days(base_dir: str, nickname: str, domain: str) -> int:
|
||||||
acct_handle_dir(base_dir, handle) + '/.expire_posts_days'
|
acct_handle_dir(base_dir, handle) + '/.expire_posts_days'
|
||||||
if not os.path.isfile(expire_posts_filename):
|
if not os.path.isfile(expire_posts_filename):
|
||||||
return 0
|
return 0
|
||||||
days_str = None
|
days_str = load_string(expire_posts_filename,
|
||||||
try:
|
'EX: unable to write post expire days ' +
|
||||||
with open(expire_posts_filename, 'r', encoding='utf-8') as fp_expire:
|
expire_posts_filename)
|
||||||
days_str = fp_expire.read()
|
|
||||||
except OSError:
|
|
||||||
print('EX: unable to write post expire days ' +
|
|
||||||
expire_posts_filename)
|
|
||||||
if not days_str:
|
if not days_str:
|
||||||
return 0
|
return 0
|
||||||
if not days_str.isdigit():
|
if not days_str.isdigit():
|
||||||
|
|
@ -5620,12 +5562,10 @@ def set_post_expiry_days(base_dir: str, nickname: str, domain: str,
|
||||||
handle = nickname + '@' + domain
|
handle = nickname + '@' + domain
|
||||||
expire_posts_filename = \
|
expire_posts_filename = \
|
||||||
acct_handle_dir(base_dir, handle) + '/.expire_posts_days'
|
acct_handle_dir(base_dir, handle) + '/.expire_posts_days'
|
||||||
try:
|
text = str(max_age_days)
|
||||||
with open(expire_posts_filename, 'w+', encoding='utf-8') as fp_expire:
|
save_string(text, expire_posts_filename,
|
||||||
fp_expire.write(str(max_age_days))
|
'EX: unable to write post expire days ' +
|
||||||
except OSError:
|
expire_posts_filename)
|
||||||
print('EX: unable to write post expire days ' +
|
|
||||||
expire_posts_filename)
|
|
||||||
|
|
||||||
|
|
||||||
def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
|
def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
|
||||||
|
|
@ -5671,13 +5611,9 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
|
||||||
index_filename + ' ' + str(ex))
|
index_filename + ' ' + str(ex))
|
||||||
# save the new index file
|
# save the new index file
|
||||||
if new_index:
|
if new_index:
|
||||||
try:
|
save_string(new_index, index_filename,
|
||||||
with open(index_filename, 'w+',
|
'EX: archive_posts_for_person unable to write ' +
|
||||||
encoding='utf-8') as fp_index:
|
index_filename)
|
||||||
fp_index.write(new_index)
|
|
||||||
except OSError:
|
|
||||||
print('EX: archive_posts_for_person unable to write ' +
|
|
||||||
index_filename)
|
|
||||||
|
|
||||||
# remove any edits or replies
|
# remove any edits or replies
|
||||||
for ext_name in ('edits', 'replies'):
|
for ext_name in ('edits', 'replies'):
|
||||||
|
|
@ -5707,13 +5643,11 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
|
||||||
continue
|
continue
|
||||||
edit_files_ctr += 1
|
edit_files_ctr += 1
|
||||||
if os.path.isfile(full_filename):
|
if os.path.isfile(full_filename):
|
||||||
content = ''
|
content = load_string(full_filename,
|
||||||
try:
|
'EX: unable to open content 2 ' +
|
||||||
with open(full_filename, 'r',
|
full_filename)
|
||||||
encoding='utf-8') as fp_content:
|
if content is None:
|
||||||
content = fp_content.read()
|
content = ''
|
||||||
except OSError:
|
|
||||||
print('EX: unable to open content 2 ' + full_filename)
|
|
||||||
if '"published":' in content:
|
if '"published":' in content:
|
||||||
published_str = content.split('"published":')[1]
|
published_str = content.split('"published":')[1]
|
||||||
if '"' in published_str:
|
if '"' in published_str:
|
||||||
|
|
@ -5779,12 +5713,11 @@ def archive_posts_for_person(http_prefix: str, nickname: str, domain: str,
|
||||||
# Get the published time
|
# Get the published time
|
||||||
full_filename = os.path.join(box_dir, post_filename)
|
full_filename = os.path.join(box_dir, post_filename)
|
||||||
if os.path.isfile(full_filename):
|
if os.path.isfile(full_filename):
|
||||||
content = ''
|
content = load_string(full_filename,
|
||||||
try:
|
'EX: unable to open content 1 ' +
|
||||||
with open(full_filename, 'r', encoding='utf-8') as fp_content:
|
full_filename)
|
||||||
content = fp_content.read()
|
if content is None:
|
||||||
except OSError:
|
content = ''
|
||||||
print('EX: unable to open content 1 ' + full_filename)
|
|
||||||
if '"published":' in content:
|
if '"published":' in content:
|
||||||
published_str = content.split('"published":')[1]
|
published_str = content.split('"published":')[1]
|
||||||
if '"' in published_str:
|
if '"' in published_str:
|
||||||
|
|
@ -6132,13 +6065,12 @@ def get_public_post_domains_blocked(session, base_dir: str,
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# read the blocked domains as a single string
|
# read the blocked domains as a single string
|
||||||
blocked_str = ''
|
blocked_str = \
|
||||||
try:
|
load_string(blocking_filename,
|
||||||
with open(blocking_filename, 'r', encoding='utf-8') as fp_block:
|
'EX: get_public_post_domains_blocked unable to read ' +
|
||||||
blocked_str = fp_block.read()
|
blocking_filename)
|
||||||
except OSError:
|
if blocked_str is None:
|
||||||
print('EX: get_public_post_domains_blocked unable to read ' +
|
blocked_str = ''
|
||||||
blocking_filename)
|
|
||||||
|
|
||||||
blocked_domains: list[str] = []
|
blocked_domains: list[str] = []
|
||||||
for domain_name in post_domains:
|
for domain_name in post_domains:
|
||||||
|
|
@ -6190,13 +6122,12 @@ def check_domains(session, base_dir: str,
|
||||||
update_follower_warnings = False
|
update_follower_warnings = False
|
||||||
follower_warning_str = ''
|
follower_warning_str = ''
|
||||||
if os.path.isfile(follower_warning_filename):
|
if os.path.isfile(follower_warning_filename):
|
||||||
try:
|
follower_warning_str = \
|
||||||
with open(follower_warning_filename, 'r',
|
load_string(follower_warning_filename,
|
||||||
encoding='utf-8') as fp_warn:
|
'EX: check_domains unable to read ' +
|
||||||
follower_warning_str = fp_warn.read()
|
follower_warning_filename)
|
||||||
except OSError:
|
if follower_warning_str is None:
|
||||||
print('EX: check_domains unable to read ' +
|
follower_warning_str = ''
|
||||||
follower_warning_filename)
|
|
||||||
|
|
||||||
if single_check:
|
if single_check:
|
||||||
# checks a single random non-mutual
|
# checks a single random non-mutual
|
||||||
|
|
@ -6247,13 +6178,9 @@ def check_domains(session, base_dir: str,
|
||||||
update_follower_warnings = True
|
update_follower_warnings = True
|
||||||
|
|
||||||
if update_follower_warnings and follower_warning_str:
|
if update_follower_warnings and follower_warning_str:
|
||||||
try:
|
save_string(follower_warning_str, follower_warning_filename,
|
||||||
with open(follower_warning_filename, 'w+',
|
'EX: check_domains unable to write ' +
|
||||||
encoding='utf-8') as fp_warn:
|
follower_warning_filename)
|
||||||
fp_warn.write(follower_warning_str)
|
|
||||||
except OSError:
|
|
||||||
print('EX: check_domains unable to write ' +
|
|
||||||
follower_warning_filename)
|
|
||||||
if not single_check:
|
if not single_check:
|
||||||
print(follower_warning_str)
|
print(follower_warning_str)
|
||||||
|
|
||||||
|
|
@ -6339,13 +6266,9 @@ def _reject_announce(announce_filename: str,
|
||||||
if os.path.isfile(announce_filename + '.reject'):
|
if os.path.isfile(announce_filename + '.reject'):
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
save_string('\n', announce_filename + '.reject',
|
||||||
with open(announce_filename + '.reject', 'w+',
|
'EX: _reject_announce unable to write ' +
|
||||||
encoding='utf-8') as fp_reject_announce:
|
announce_filename + '.reject')
|
||||||
fp_reject_announce.write('\n')
|
|
||||||
except OSError:
|
|
||||||
print('EX: _reject_announce unable to write ' +
|
|
||||||
announce_filename + '.reject')
|
|
||||||
|
|
||||||
|
|
||||||
def download_announce(session, base_dir: str, http_prefix: str,
|
def download_announce(session, base_dir: str, http_prefix: str,
|
||||||
|
|
@ -6972,13 +6895,11 @@ def edited_post_filename(base_dir: str, nickname: str, domain: str,
|
||||||
if not os.path.isfile(actor_filename):
|
if not os.path.isfile(actor_filename):
|
||||||
return '', None
|
return '', None
|
||||||
post_id = remove_id_ending(post_json_object['object']['id'])
|
post_id = remove_id_ending(post_json_object['object']['id'])
|
||||||
lastpost_id = None
|
lastpost_id = \
|
||||||
try:
|
load_string(actor_filename,
|
||||||
with open(actor_filename, 'r',
|
'EX: edited_post_filename unable to read ' +
|
||||||
encoding='utf-8') as fp_actor:
|
actor_filename)
|
||||||
lastpost_id = fp_actor.read()
|
if lastpost_id is None:
|
||||||
except OSError:
|
|
||||||
print('EX: edited_post_filename unable to read ' + actor_filename)
|
|
||||||
return '', None
|
return '', None
|
||||||
if not lastpost_id:
|
if not lastpost_id:
|
||||||
return '', None
|
return '', None
|
||||||
|
|
@ -7099,15 +7020,13 @@ def get_max_profile_posts(base_dir: str, nickname: str, domain: str,
|
||||||
max_profile_posts = 4
|
max_profile_posts = 4
|
||||||
if not os.path.isfile(max_posts_filename):
|
if not os.path.isfile(max_posts_filename):
|
||||||
return max_profile_posts
|
return max_profile_posts
|
||||||
try:
|
max_posts_str = \
|
||||||
with open(max_posts_filename, 'r', encoding='utf-8') as fp_posts:
|
load_string(max_posts_filename,
|
||||||
max_posts_str = fp_posts.read()
|
'EX: unable to read maximum profile posts ' +
|
||||||
if max_posts_str:
|
max_posts_filename)
|
||||||
if max_posts_str.isdigit():
|
if max_posts_str:
|
||||||
max_profile_posts = int(max_posts_str)
|
if max_posts_str.isdigit():
|
||||||
except OSError:
|
max_profile_posts = int(max_posts_str)
|
||||||
print('EX: unable to read maximum profile posts ' +
|
|
||||||
max_posts_filename)
|
|
||||||
if max_profile_posts < 1:
|
if max_profile_posts < 1:
|
||||||
max_profile_posts = 1
|
max_profile_posts = 1
|
||||||
if max_profile_posts > max_recent_posts:
|
if max_profile_posts > max_recent_posts:
|
||||||
|
|
@ -7122,13 +7041,9 @@ def set_max_profile_posts(base_dir: str, nickname: str, domain: str,
|
||||||
max_posts_filename = \
|
max_posts_filename = \
|
||||||
acct_dir(base_dir, nickname, domain) + '/max_profile_posts.txt'
|
acct_dir(base_dir, nickname, domain) + '/max_profile_posts.txt'
|
||||||
max_recent_posts_str = str(max_recent_posts)
|
max_recent_posts_str = str(max_recent_posts)
|
||||||
try:
|
if not save_string(max_recent_posts_str, max_posts_filename,
|
||||||
with open(max_posts_filename, 'w+',
|
'EX: unable to save maximum profile posts ' +
|
||||||
encoding='utf-8') as fp_posts:
|
max_posts_filename):
|
||||||
fp_posts.write(max_recent_posts_str)
|
|
||||||
except OSError:
|
|
||||||
print('EX: unable to save maximum profile posts ' +
|
|
||||||
max_posts_filename)
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue