Skip to content

Commit

Permalink
Create get_posts_by_url.py
Browse files Browse the repository at this point in the history
  • Loading branch information
DGaffney authored Nov 8, 2024
1 parent 91e0335 commit af3197c
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions examples/get_posts_by_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from atproto import Client, IdResolver, models
from typing import Optional

def fetch_posts(client: Client, resolver: IdResolver, url: str) -> Optional[models.app.bsky.feed.get_posts.Response]:

Check failure on line 4 in examples/get_posts_by_url.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

examples/get_posts_by_url.py:1:1: I001 Import block is un-sorted or un-formatted
"""
Fetches a post using its Bluesky URL.
Args:
client (Client): Authenticated Atproto client.
resolver (IdResolver): Resolver instance for DID lookup.
url (str): URL of the Bluesky post.
Returns:
Optional[models.Record]: The hydrated post record if found, otherwise None.
"""
try:
# Extract the handle and post ID from the URL
parts = url.split("/")

Check failure on line 16 in examples/get_posts_by_url.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (Q000)

examples/get_posts_by_url.py:16:27: Q000 Double quotes found but single quotes preferred
handle = parts[4] # Username in the URL
post_id = parts[6] # Post ID in the URL
# Resolve the DID for the username
did = resolver.handle.resolve(handle)
if not did:
print(f"Could not resolve DID for handle '{handle}'.")
return None
# Construct the `at://` URI for the post
at_uri = f"at://{did}/app.bsky.feed.post/{post_id}"

Check failure on line 25 in examples/get_posts_by_url.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (Q000)

examples/get_posts_by_url.py:25:18: Q000 Double quotes found but single quotes preferred
# Fetch and return the post record
post_response = client.get_posts([at_uri])
return post_response

Check failure on line 28 in examples/get_posts_by_url.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RET504)

examples/get_posts_by_url.py:28:16: RET504 Unnecessary assignment to `post_response` before `return` statement
except Exception as e:

Check failure on line 29 in examples/get_posts_by_url.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (BLE001)

examples/get_posts_by_url.py:29:12: BLE001 Do not catch blind exception: `Exception`
print(f"Error fetching post for URL {url}: {e}")

Check failure on line 30 in examples/get_posts_by_url.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (Q000)

examples/get_posts_by_url.py:30:15: Q000 Double quotes found but single quotes preferred
return None

def main() -> None:
# Initialize client and authenticate
client = Client()
client.login('my-handle', 'my-password')

# Initialize IdResolver for DID resolution
resolver = IdResolver()

# Define the URL of the post to fetch
url = "https://bsky.app/profile/danabra.mov/post/3lagnt6bpkc2l"

Check failure on line 42 in examples/get_posts_by_url.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (Q000)

examples/get_posts_by_url.py:42:11: Q000 Double quotes found but single quotes preferred

# Fetch the post
post_record = fetch_posts(client, resolver, url)

# Display the post details
if post_record:
print(f"Post Content: {post_record.text}")

Check failure on line 49 in examples/get_posts_by_url.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (Q000)

examples/get_posts_by_url.py:49:15: Q000 Double quotes found but single quotes preferred
else:
print("Post could not be fetched.")

Check failure on line 51 in examples/get_posts_by_url.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (Q000)

examples/get_posts_by_url.py:51:15: Q000 Double quotes found but single quotes preferred

if __name__ == '__main__':
main()

0 comments on commit af3197c

Please sign in to comment.