From 1f0b1601ce02ec5519e6b8a9f7d171f557cb3343 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 22 Aug 2022 21:14:37 +0100 Subject: [PATCH] Fix hashtag indexing bug --- inbox.py | 12 ++++-------- posts.py | 27 ++++++++++++++++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/inbox.py b/inbox.py index b6bc0b1af..5b5505a91 100644 --- a/inbox.py +++ b/inbox.py @@ -355,6 +355,8 @@ def store_hash_tags(base_dir: str, nickname: str, domain: str, print('Creating tagmaps directory') os.mkdir(tag_maps_dir) + post_url = remove_id_ending(post_json_object['id']) + post_url = post_url.replace('/', '#') hashtags_ctr = 0 for tag in post_json_object['object']['tag']: if not tag.get('type'): @@ -371,17 +373,12 @@ def store_hash_tags(base_dir: str, nickname: str, domain: str, tags_filename = tags_dir + '/' + tag_name + '.txt' days_diff = datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1) days_since_epoch = days_diff.days - post_url = remove_id_ending(post_json_object['id']) - post_url = post_url.replace('/', '#') tag_line = \ str(days_since_epoch) + ' ' + nickname + ' ' + post_url + '\n' if map_links and published: add_tag_map_links(tag_maps_dir, tag_name, map_links, published, post_url) - post_url = remove_id_ending(post_json_object['id']) - post_url = post_url.replace('/', '#') hashtag_added = False - print('**** tag_line3 ' + tag_line.replace('\n', '')) if not os.path.isfile(tags_filename): try: with open(tags_filename, 'w+', encoding='utf-8') as tags_file: @@ -397,12 +394,11 @@ def store_hash_tags(base_dir: str, nickname: str, domain: str, except OSError: pass if post_url not in content: - print('**** tag_line4 ' + content.replace('\n', '')) content = tag_line + content try: with open(tags_filename, 'w+', - encoding='utf-8') as tags_file: - tags_file.write(content) + encoding='utf-8') as tags_file2: + tags_file2.write(content) hashtag_added = True except OSError as ex: print('EX: Failed to write entry to tags file ' + diff --git a/posts.py b/posts.py index da4b1b274..b3db92270 100644 --- a/posts.py +++ b/posts.py @@ -942,7 +942,8 @@ def save_post_to_box(base_dir: str, http_prefix: str, post_id: str, return filename -def _update_hashtags_index(base_dir: str, tag: {}, new_post_id: str) -> None: +def _update_hashtags_index(base_dir: str, tag: {}, new_post_id: str, + nickname: str) -> None: """Writes the post url for hashtags to a file This allows posts for a hashtag to be quickly looked up """ @@ -955,25 +956,37 @@ def _update_hashtags_index(base_dir: str, tag: {}, new_post_id: str) -> None: os.mkdir(tags_dir) tag_name = tag['name'] tags_filename = tags_dir + '/' + tag_name[1:] + '.txt' - tagline = new_post_id + '\n' + + new_post_id = new_post_id.replace('/', '#') if not os.path.isfile(tags_filename): + days_diff = datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1) + days_since_epoch = days_diff.days + tag_line = \ + str(days_since_epoch) + ' ' + nickname + ' ' + \ + new_post_id + '\n' # create a new tags index file try: with open(tags_filename, 'w+', encoding='utf-8') as tags_file: - tags_file.write(tagline) + tags_file.write(tag_line) except OSError: print('EX: _update_hashtags_index unable to write tags file ' + tags_filename) else: # prepend to tags index file - if not text_in_file(tagline, tags_filename): + if not text_in_file(new_post_id, tags_filename): + days_diff = \ + datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1) + days_since_epoch = days_diff.days + tag_line = \ + str(days_since_epoch) + ' ' + nickname + ' ' + \ + new_post_id + '\n' try: with open(tags_filename, 'r+', encoding='utf-8') as tags_file: content = tags_file.read() - if tagline not in content: + if tag_line not in content: tags_file.seek(0, 0) - tags_file.write(tagline + content) + tags_file.write(tag_line + content) except OSError as ex: print('EX: Failed to write entry to tags file ' + tags_filename + ' ' + str(ex)) @@ -1530,7 +1543,7 @@ def _create_post_base(base_dir: str, if not post_tag_exists(tag['type'], tag['name'], tags): tags.append(tag) if is_public: - _update_hashtags_index(base_dir, tag, new_post_id) + _update_hashtags_index(base_dir, tag, new_post_id, nickname) # print('Content tags: ' + str(tags)) sensitive, summary = \