implement title getter for bookmarks

This commit is contained in:
James Ravenscroft 2024-09-08 12:09:41 +01:00
parent 3354021cbd
commit 7376dc4dc9
1 changed files with 32 additions and 16 deletions

View File

@ -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,27 +66,45 @@ 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")
for property_name in properties_to_check:
property_data = data.get(property_name)
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
if type(reply_data) == str:
title = get_html_title(reply_data)
title = get_html_title(property_data)
if title is not None:
print(f"Found in-reply-to title: '{title}'")
data['in-reply-to'] = {"url": reply_data,
"title": str(title)}
print(f"Found {property_name} title: '{title}'")
data[property_name] = {"url": property_data, "title": str(title)}
updated = True
print(f"Updating in-reply-to data... {full_path}")
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)
# parse the response and extract the title
@cli.command()
@click.option("--folder", type=click.Path(dir_okay=True, file_okay=False), required=True)