From f52a3e7ace8789dc6f1a22110cd5c7e8b386e9d3 Mon Sep 17 00:00:00 2001 From: bashrc Date: Wed, 29 Apr 2026 12:54:57 +0100 Subject: [PATCH] Replace file operations with function --- schedule.py | 297 ++++++++++++++++++++++++++-------------------------- 1 file changed, 148 insertions(+), 149 deletions(-) diff --git a/schedule.py b/schedule.py index 16c1c3cb7..291bc2164 100644 --- a/schedule.py +++ b/schedule.py @@ -24,6 +24,8 @@ from outbox import post_message_to_outbox from session import create_session from threads import begin_thread from siteactive import save_unavailable_sites +from data import save_string +from data import load_list def _update_post_schedule(base_dir: str, handle: str, httpd, @@ -46,165 +48,162 @@ def _update_post_schedule(base_dir: str, handle: str, httpd, nickname = handle.split('@')[0] shared_items_federated_domains = httpd.shared_items_federated_domains shared_item_federation_tokens = httpd.shared_item_federation_tokens - try: - with open(schedule_index_filename, 'r', encoding='utf-8') as fp_sched: - for line in fp_sched: - if ' ' not in line: - continue - date_str = line.split(' ')[0] - if 'T' not in date_str: - continue - post_id1 = line.split(' ', 1)[1] - post_id = remove_eol(post_id1) - post_filename = schedule_dir + post_id + '.json' - if delete_schedule_post: - # delete extraneous scheduled posts - if os.path.isfile(post_filename): - try: - os.remove(post_filename) - except OSError: - print('EX: ' + - '_update_post_schedule unable to delete ' + - str(post_filename)) - continue - # create the new index file - index_lines.append(line) - # convert string date to int - post_time = \ - date_from_string_format(date_str, ["%Y-%m-%dT%H:%M:%S%z"]) - post_time = post_time.replace(tzinfo=None) - post_days_since_epoch = \ - (post_time - date_epoch()).days - if days_since_epoch < post_days_since_epoch: - continue - if days_since_epoch == post_days_since_epoch: - if curr_time.time().hour < post_time.time().hour: - continue - if curr_time.time().minute < post_time.time().minute: - continue - if not os.path.isfile(post_filename): - print('WARN: schedule missing post_filename=' + - post_filename) - index_lines.remove(line) - continue - # load post - post_json_object = load_json(post_filename) - if not post_json_object: - print('WARN: schedule json not loaded') - index_lines.remove(line) - continue - - # set the published time - # If this is not recent then http checks on the receiving side - # will reject it - _, published = get_status_number() - if post_json_object.get('published'): - post_json_object['published'] = published - if has_object_dict(post_json_object): - if post_json_object['object'].get('published'): - post_json_object['published'] = published - - print('Sending scheduled post ' + post_id) - - if nickname: - httpd.post_to_nickname = nickname - - # create session if needed - curr_session = httpd.session - curr_proxy_type = httpd.proxy_type - if not curr_session: - curr_session = create_session(httpd.proxy_type) - httpd.session = curr_session - if not curr_session: - continue - - if not post_message_to_outbox(curr_session, - httpd.translate, - post_json_object, nickname, - httpd, base_dir, - httpd.http_prefix, - httpd.domain, - httpd.domain_full, - httpd.onion_domain, - httpd.i2p_domain, - httpd.yggdrasil_domain, - httpd.port, - httpd.recent_posts_cache, - httpd.followers_threads, - httpd.federation_list, - httpd.send_threads, - httpd.post_log, - httpd.cached_webfingers, - httpd.person_cache, - httpd.allow_deletion, - curr_proxy_type, - httpd.project_version, - httpd.debug, - httpd.yt_replace_domain, - httpd.twitter_replacement_domain, - httpd.show_published_date_only, - httpd.allow_local_network_access, - httpd.city, - httpd.system_language, - shared_items_federated_domains, - shared_item_federation_tokens, - httpd.low_bandwidth, - httpd.signing_priv_key_pem, - httpd.peertube_instances, - httpd.theme_name, - httpd.max_like_count, - httpd.max_recent_posts, - httpd.cw_lists, - httpd.lists_enabled, - httpd.content_license_url, - httpd.dogwhistles, - httpd.min_images_for_accounts, - httpd.buy_sites, - httpd.sites_unavailable, - httpd.max_recent_books, - httpd.books_cache, - httpd.max_cached_readers, - httpd.auto_cw_cache, - httpd.block_federated, - httpd.mitm_servers, - httpd.instance_software): - index_lines.remove(line) + scheduled_list = load_list(schedule_index_filename, + 'EX: _update_post_schedule unable to read ' + + schedule_index_filename + ' [ex]') + if scheduled_list is not None: + for line in scheduled_list: + if ' ' not in line: + continue + date_str = line.split(' ')[0] + if 'T' not in date_str: + continue + post_id1 = line.split(' ', 1)[1] + post_id = remove_eol(post_id1) + post_filename = schedule_dir + post_id + '.json' + if delete_schedule_post: + # delete extraneous scheduled posts + if os.path.isfile(post_filename): try: os.remove(post_filename) except OSError: - print('EX: _update_post_schedule unable to delete ' + + print('EX: ' + + '_update_post_schedule unable to delete ' + str(post_filename)) + continue + # create the new index file + index_lines.append(line) + # convert string date to int + post_time = \ + date_from_string_format(date_str, ["%Y-%m-%dT%H:%M:%S%z"]) + post_time = post_time.replace(tzinfo=None) + post_days_since_epoch = \ + (post_time - date_epoch()).days + if days_since_epoch < post_days_since_epoch: + continue + if days_since_epoch == post_days_since_epoch: + if curr_time.time().hour < post_time.time().hour: continue - - # move to the outbox - outbox_post_filename = \ - post_filename.replace('/scheduled/', '/outbox/') - try: - os.rename(post_filename, outbox_post_filename) - except OSError: - print('EX: _update_post_schedule unable to rename ' + - post_filename + ' -> ' + outbox_post_filename) - - print('Scheduled post sent ' + post_id) - + if curr_time.time().minute < post_time.time().minute: + continue + if not os.path.isfile(post_filename): + print('WARN: schedule missing post_filename=' + + post_filename) index_lines.remove(line) - if len(index_lines) > max_scheduled_posts: - delete_schedule_post = True - except OSError as exc: - print('EX: _update_post_schedule unable to read ' + - schedule_index_filename + ' ' + str(exc)) + continue + # load post + post_json_object = load_json(post_filename) + if not post_json_object: + print('WARN: schedule json not loaded') + index_lines.remove(line) + continue + + # set the published time + # If this is not recent then http checks on the receiving side + # will reject it + _, published = get_status_number() + if post_json_object.get('published'): + post_json_object['published'] = published + if has_object_dict(post_json_object): + if post_json_object['object'].get('published'): + post_json_object['published'] = published + + print('Sending scheduled post ' + post_id) + + if nickname: + httpd.post_to_nickname = nickname + + # create session if needed + curr_session = httpd.session + curr_proxy_type = httpd.proxy_type + if not curr_session: + curr_session = create_session(httpd.proxy_type) + httpd.session = curr_session + if not curr_session: + continue + + if not post_message_to_outbox(curr_session, + httpd.translate, + post_json_object, nickname, + httpd, base_dir, + httpd.http_prefix, + httpd.domain, + httpd.domain_full, + httpd.onion_domain, + httpd.i2p_domain, + httpd.yggdrasil_domain, + httpd.port, + httpd.recent_posts_cache, + httpd.followers_threads, + httpd.federation_list, + httpd.send_threads, + httpd.post_log, + httpd.cached_webfingers, + httpd.person_cache, + httpd.allow_deletion, + curr_proxy_type, + httpd.project_version, + httpd.debug, + httpd.yt_replace_domain, + httpd.twitter_replacement_domain, + httpd.show_published_date_only, + httpd.allow_local_network_access, + httpd.city, + httpd.system_language, + shared_items_federated_domains, + shared_item_federation_tokens, + httpd.low_bandwidth, + httpd.signing_priv_key_pem, + httpd.peertube_instances, + httpd.theme_name, + httpd.max_like_count, + httpd.max_recent_posts, + httpd.cw_lists, + httpd.lists_enabled, + httpd.content_license_url, + httpd.dogwhistles, + httpd.min_images_for_accounts, + httpd.buy_sites, + httpd.sites_unavailable, + httpd.max_recent_books, + httpd.books_cache, + httpd.max_cached_readers, + httpd.auto_cw_cache, + httpd.block_federated, + httpd.mitm_servers, + httpd.instance_software): + index_lines.remove(line) + try: + os.remove(post_filename) + except OSError: + print('EX: _update_post_schedule unable to delete ' + + str(post_filename)) + continue + + # move to the outbox + outbox_post_filename = \ + post_filename.replace('/scheduled/', '/outbox/') + try: + os.rename(post_filename, outbox_post_filename) + except OSError: + print('EX: _update_post_schedule unable to rename ' + + post_filename + ' -> ' + outbox_post_filename) + + print('Scheduled post sent ' + post_id) + + index_lines.remove(line) + if len(index_lines) > max_scheduled_posts: + delete_schedule_post = True # write the new schedule index file schedule_index_file = \ acct_handle_dir(base_dir, handle) + '/schedule.index' - try: - with open(schedule_index_file, 'w+', - encoding='utf-8') as fp_schedule: - for line in index_lines: - fp_schedule.write(line) - except OSError: - print('EX: _update_post_schedule unable to write ' + - schedule_index_file) + text = '' + for line in index_lines: + text += line + save_string(text, schedule_index_file, + 'EX: _update_post_schedule unable to write ' + + schedule_index_file) def run_post_schedule(base_dir: str, httpd, max_scheduled_posts: int):