diff --git a/bstools/bstools.py b/bstools/bstools.py index e2d7545..ff8844b 100644 --- a/bstools/bstools.py +++ b/bstools/bstools.py @@ -53,14 +53,12 @@ def cli(): dotenv.load_dotenv() pass - @cli.command() @click.option("--folder", type=click.Path(dir_okay=True, file_okay=False), required=True) def fetch_link_titles(folder): """Fetch titles for reply and bookmark links""" for root, _, files in os.walk(folder): - for file in files: if file.endswith(".md"): full_path = os.path.join(root, file) @@ -68,26 +66,44 @@ def fetch_link_titles(folder): print(f"Analysing... {full_path}") - reply_data = data.get('in-reply-to') + properties_to_check = ['in-reply-to', 'bookmark-of'] + updated = False - if 'twitter.com' in reply_data: - print("Not grabbing title for tweet") - continue + for property_name in properties_to_check: - if type(reply_data) == str: - title = get_html_title(reply_data) + property_data = data.get(property_name) - if title is not None: - print(f"Found in-reply-to title: '{title}'") - data['in-reply-to'] = {"url": reply_data, - "title": str(title)} + if property_data: + if isinstance(property_data, str): + if 'twitter.com' in property_data: + print(f"Not grabbing title for tweet in {property_name}") + continue - print(f"Updating in-reply-to data... {full_path}") + title = get_html_title(property_data) - with open(full_path, 'wb') as f: - frontmatter.dump(data, f) + if title is not None: + print(f"Found {property_name} title: '{title}'") + data[property_name] = {"url": property_data, "title": str(title)} + updated = True - # parse the response and extract the title + elif isinstance(property_data, dict) and 'url' in property_data: + + if 'twitter.com' in property_data['url']: + print(f"Not grabbing title for tweet in {property_name}") + continue + + if 'title' not in property_data: + title = get_html_title(property_data['url']) + if title is not None: + print(f"Found {property_name} title: '{title}'") + property_data['title'] = str(title) + data[property_name] = property_data + updated = True + + if updated: + print(f"Updating data... {full_path}") + with open(full_path, 'wb') as f: + frontmatter.dump(data, f) @cli.command()