-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_bluesky_posts.py
More file actions
150 lines (112 loc) · 4.01 KB
/
fetch_bluesky_posts.py
File metadata and controls
150 lines (112 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
"""
Fetch posts from a Bluesky account and convert to training format.
Usage:
python fetch_bluesky_posts.py @bobbby.online -o bluesky_posts.jsonl
"""
import argparse
import json
import os
import random
from pathlib import Path
from atproto import Client
from dotenv import load_dotenv
load_dotenv()
# Prompts matching the training data format
GENERATION_PROMPTS = [
"Write a tweet in your authentic voice.",
"Share your thoughts.",
"What's on your mind?",
"Post something.",
"Tweet something interesting.",
"Share an observation.",
"What would you say about this?",
"Give me your take.",
"Write something in your style.",
"How would you respond to this?",
]
def fetch_all_posts(handle: str, limit: int = None) -> list[dict]:
"""Fetch all posts from a Bluesky account."""
client = Client()
# Login with credentials from .env
bluesky_handle = os.getenv("BLUESKY_HANDLE")
app_password = os.getenv("BLUESKY_APP_PASSWORD")
if bluesky_handle and app_password:
print(f"Logging in as: {bluesky_handle}")
client.login(bluesky_handle, app_password)
# Resolve handle to DID if needed
if handle.startswith("@"):
handle = handle[1:]
print(f"Fetching posts from: {handle}")
all_posts = []
cursor = None
while True:
response = client.get_author_feed(
actor=handle,
filter="posts_no_replies", # Only original posts, not replies
limit=100,
cursor=cursor,
)
for feed_view in response.feed:
post = feed_view.post
record = post.record
# Skip reposts
if hasattr(feed_view, 'reason') and feed_view.reason:
continue
# Get the text content
text = getattr(record, 'text', None)
if not text:
continue
# Skip very short posts
if len(text.strip()) < 10:
continue
# Skip posts that are mostly URLs or mentions
words = text.split()
url_mention_count = sum(1 for w in words if w.startswith(('http', '@', 'https')))
if url_mention_count > len(words) / 2:
continue
all_posts.append({
"text": text.strip(),
"created_at": record.created_at,
"uri": post.uri,
})
print(f" Fetched {len(all_posts)} posts so far...")
if not response.cursor:
break
cursor = response.cursor
if limit and len(all_posts) >= limit:
all_posts = all_posts[:limit]
break
print(f"Total posts fetched: {len(all_posts)}")
return all_posts
def convert_to_training_format(posts: list[dict]) -> list[dict]:
"""Convert posts to the training JSONL format."""
training_data = []
for post in posts:
prompt = random.choice(GENERATION_PROMPTS)
training_example = {
"messages": [
{"role": "user", "content": prompt},
{"role": "assistant", "content": post["text"]}
]
}
training_data.append(training_example)
return training_data
def main():
parser = argparse.ArgumentParser(description="Fetch Bluesky posts for training")
parser.add_argument("handle", help="Bluesky handle (e.g., @bobbby.online)")
parser.add_argument("-o", "--output", default="bluesky_posts.jsonl", help="Output file")
parser.add_argument("-l", "--limit", type=int, help="Max posts to fetch")
args = parser.parse_args()
# Fetch posts
posts = fetch_all_posts(args.handle, args.limit)
# Convert to training format
training_data = convert_to_training_format(posts)
# Write to JSONL
output_path = Path(args.output)
with open(output_path, "w") as f:
for item in training_data:
f.write(json.dumps(item) + "\n")
print(f"\nWritten {len(training_data)} training examples to {output_path}")
if __name__ == "__main__":
main()