Last active
April 10, 2025 06:06
-
-
Save jlevy/ee975e59c8864902b288e2a44dd29f98 to your computer and use it in GitHub Desktop.
Example of uv script inline dependencies: https://docs.astral.sh/uv/guides/scripts/#declaring-script-dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S uv run --script | |
# /// script | |
# requires-python = "==3.13" | |
# dependencies = [ | |
# "requests>=2.32.3", | |
# "rich>=14.0.0", | |
# "markdownify>=1.1.0", | |
# "readabilipy>=0.3.0", | |
# ] | |
# /// | |
# An example of how to use inline script inline dependencies in uv. | |
# Just run | |
# uv run nytimes_in_md.py | |
# or even chmod +x it and just run | |
# ./nytimes_in_md.py | |
# This installs all deps (including Python if necessary!) then runs the script. | |
# See: https://docs.astral.sh/uv/guides/scripts/#declaring-script-dependencies | |
import requests | |
import re | |
from markdownify import markdownify | |
from readabilipy import simple_json_from_html_string | |
from rich import print | |
from rich.markdown import Markdown | |
# Fetch the New York Times homepage. | |
url = "https://www.nytimes.com/" | |
resp = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}) | |
html_content = resp.text | |
# Extract and clean up a little. | |
article_json = simple_json_from_html_string(html_content) | |
md: str = markdownify(article_json["content"]) | |
start_str = "Today’s Paper" | |
if start_str in md: | |
md = md.split(start_str)[1] | |
md = re.sub(r"\d+ min read\s*", "", md) | |
# Display in color in the terminal with rich. | |
print(Markdown(md)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment