Skip to content

Instantly share code, notes, and snippets.

@huytd
Created October 21, 2024 06:04
Show Gist options
  • Save huytd/b6a1faebf38183db76e04d15b5d6bb15 to your computer and use it in GitHub Desktop.
Save huytd/b6a1faebf38183db76e04d15b5d6bb15 to your computer and use it in GitHub Desktop.
Use Gemini to generate bookmark, use in Raycast
#!/usr/bin/env python3
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Add a new bookmark in Obsidian
# @raycast.mode silent
# Optional parameters:
# @raycast.icon 🔖
# @raycast.argument1 { "type": "text", "placeholder": "URL" }
# Documentation:
# @raycast.author Huy Tran
API_KEY = "<gemini api key>"
BOOKMARK_FILE = "~/notes/Bookmarks.md"
import sys
import re
from openai import OpenAI
import google.generativeai as genai
import requests
import datetime
def get_ordinal_suffix(day):
if 10 <= day % 100 <= 20:
suffix = 'th'
else:
suffix = {1: 'st', 2: 'nd', 3: 'rd'}.get(day % 10, 'th')
return suffix
def custom_strftime(format_string, date_obj):
day = date_obj.day
suffix = get_ordinal_suffix(day)
return date_obj.strftime(format_string).replace('{S}', str(day) + suffix)
today = datetime.date.today()
formatted_date = custom_strftime("%A, %B {S} %Y", today)
def get_text_from_url(url):
try:
response = requests.get(url)
response.raise_for_status() # Raise an exception for bad status codes
return response.text
except requests.RequestException as e:
return f"An error occurred: {e}"
def generate_bookmark_content(url, with_summary):
text = get_text_from_url(url)
if not with_summary:
match = re.search('<title>(.*?)</title>', text)
title = match.group(1) if match else 'No title'
return f"- {title} ({url})"
prompt = f"""
From this text, create a quick note entry to use as a bookmark in the following format, do not say anything else, and only use the provided content to generate:
- <1 line description> (<url>)
- <a brief summary with no more than 3 bullet points>"
Here's the text content:
---
URL: {url}
TEXT CONTENT:
{text}
---
"""
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content(prompt)
output = response.text
return output
##################################################
# PROCESS URL
##################################################
url = sys.argv[1]
with_summary = True
if url.startswith("!"):
with_summary = False
url = url.replace("!", "")
output = generate_bookmark_content(url, with_summary)
##################################################
# WRITE FILE
##################################################
has_today_section = False
with open(BOOKMARK_FILE, "r") as file:
lines = file.readlines()
for line in lines:
if line.startswith("# ") and formatted_date in line:
has_today_section = True
break
content_to_write = ""
if not has_today_section:
content_to_write = f"# {formatted_date}"
content_to_write += f"\n{output}"
with open(BOOKMARK_FILE, "a") as f:
f.write(content_to_write)
print("✅ Bookmarked!")
@huytd
Copy link
Author

huytd commented Oct 21, 2024

Installation Guide

Create a new Raycast command with this script. Don't forget to replace the BOOKMARK_FILE value with your bookmark file, and API_KEY as your Google Gemini API Key.

Follow this guide to setup the python package and API key https://ai.google.dev/gemini-api/docs/quickstart?lang=python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment