-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluesky_bot.py
318 lines (260 loc) · 9.86 KB
/
bluesky_bot.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import configparser
import logging
import random
import asyncio
import re
from datetime import datetime
from typing import List, Dict
import click
from apscheduler.schedulers.background import BackgroundScheduler
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
from apscheduler.triggers.cron import CronTrigger
import httpx
from aiolimiter import AsyncLimiter
# Logging setup
logging.basicConfig(level=logging.INFO)
# Constants
BUFFER = []
EXISTING_POSTS_BUFFER = []
# FastAPI setup
app = FastAPI(
title="Bluesky PRIDE Bot",
description="Bluesky bot - publish datasets for PRIDE Archive",
version="0.0.1",
contact={
"name": "PRIDE Team",
"url": "https://www.ebi.ac.uk/pride/",
"email": "[email protected]",
},
license_info={
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
},
)
# Configuration placeholders
BLUESKY_HANDLE = None
BLUESKY_PASSWORD = None
# Load FLAN-T5 model
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
summarizer = pipeline("summarization", model=model, tokenizer=tokenizer)
# Rate limiters
five_minute_limiter = AsyncLimiter(30, 300)
daily_limiter = AsyncLimiter(300, 86400)
async def bluesky_login():
"""Log in to Bluesky and return the JWT token."""
async with five_minute_limiter, daily_limiter:
async with httpx.AsyncClient() as client:
response = await client.post(
"https://bsky.social/xrpc/com.atproto.server.createSession",
json={"identifier": BLUESKY_HANDLE, "password": BLUESKY_PASSWORD},
)
response.raise_for_status()
return response.json()["accessJwt"]
def build_bluesky_post(accession: str, tweet: str, url: str) -> str:
"""Build a Bluesky post."""
alert_emoji = "🚨"
return f"[{accession}]({url}) {alert_emoji}\n\n{tweet}\n\n{alert_emoji} New dataset alert! {alert_emoji}"
def create_tweet(title: str, description: str) -> str:
"""Create a tweet using LLM to summarize the dataset."""
prompt = f"Summarize this dataset for social media with 200 characters: Title: {title}; Description: {description}"
result = summarizer(prompt, max_length=60, min_length=10, do_sample=True)[0]["summary_text"]
return result[:239]
class MessageModel(BaseModel):
accession: str
title: str
description: str
url: str
@app.post("/publish")
async def post_to_bluesky(message: MessageModel):
"""Add a message to the posting buffer."""
tweet = create_tweet(message.title, message.description)
post_str = build_bluesky_post(message.accession, tweet, message.url)
BUFFER.append(post_str)
return {"status": "Added to buffer", "total_in_buffer": len(BUFFER)}
@app.get("/buffer_count")
async def get_buffer_count():
"""Get the number of posts in the buffer."""
return {"pending_posts": len(BUFFER)}
@app.get("/post_now")
async def post_now():
"""Manually post from the buffer."""
if BUFFER:
await post_from_buffer()
return {"status": "Posted"}
return {"status": "Buffer is empty"}
@app.get("/get_posts")
async def get_posts():
"""Fetch recent posts from Bluesky."""
if not EXISTING_POSTS_BUFFER:
await update_posts()
return EXISTING_POSTS_BUFFER
async def update_posts(limit: int = 5, jwt_token: str = None):
"""Update the existing posts buffer."""
try:
if not jwt_token:
jwt_token = await bluesky_login()
async with httpx.AsyncClient() as client:
headers = {"Authorization": f"Bearer {jwt_token}"}
response = await client.get(
"https://bsky.social/xrpc/com.atproto.repo.listRecords",
headers=headers,
params={"repo": BLUESKY_HANDLE, "collection": "app.bsky.feed.post", "limit": limit},
)
response.raise_for_status()
records = response.json().get("records", [])
global EXISTING_POSTS_BUFFER
EXISTING_POSTS_BUFFER = [
{
"id": record["uri"].split("/")[-1],
"content": record["value"]["text"],
"createdAt": record["value"]["createdAt"],
}
for record in records
]
except Exception as e:
logging.error(f"Error fetching posts: {e}")
return EXISTING_POSTS_BUFFER
def _parse_urls(post_text: str) -> List[Dict]:
"""
Parse plain URLs from the post text.
Args:
post_text (str): The text of the post.
Returns:
List[Dict]: A list of dictionaries with URL spans.
"""
spans = []
url_regex = rb"[$|\W](https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*[-a-zA-Z0-9@%_\+~#//=])?)"
text_bytes = post_text.encode("UTF-8")
for match in re.finditer(url_regex, text_bytes):
spans.append({
"start": match.start(1),
"end": match.end(1),
"url": match.group(1).decode("UTF-8"),
})
return spans
def _parse_rich_urls(post_content: str):
spans = []
while True:
span, post_content = _handle_first_rich_url(post_content)
if span:
spans.append(span)
else:
break
return spans, post_content
def _handle_first_rich_url(post_content: str):
regex = rb"\[(.*?)\]\(\s*(https?://[^\s)]+)\s*\)"
text_bytes = post_content.encode("UTF-8")
match = re.search(regex, text_bytes)
if match:
span = {
"start": match.start(1) - 1,
"end": match.end(1) - 1,
"url": match.group(2).decode("UTF-8"),
}
post_content = (
post_content[: match.start(1) - 1]
+ post_content[match.start(1): match.end(1)]
+ post_content[match.end():]
)
return span, post_content
return None, post_content
def parse_facets(post_content: str):
facets = []
# Parse rich text URLs
spans, post_content = _parse_rich_urls(post_content)
for rich_url in spans:
facets.append({
"index": {
"byteStart": rich_url["start"],
"byteEnd": rich_url["end"],
},
"features": [
{
"$type": "app.bsky.richtext.facet#link",
"uri": rich_url["url"],
}
],
})
return facets, post_content
async def post_from_buffer():
"""
Post a random message from the buffer to Bluesky.
Returns:
dict: The status of the posting operation.
"""
if not BUFFER:
logging.info("No posts in the buffer to post.")
return {"status": "Buffer is empty"}
post_content = random.choice(BUFFER)
try:
jwt_token = await bluesky_login() # Ensure this function is defined elsewhere
post_data = {
"collection": "app.bsky.feed.post",
"repo": BLUESKY_HANDLE,
"record": {
"type": "app.bsky.feed.post",
"text": post_content,
"createdAt": datetime.utcnow().isoformat() + "Z",
"langs": ["th", "en-US"],
},
}
# Parse and update facets in the post data
facets, new_content = parse_facets(post_content)
post_data["record"]["facets"] = facets
post_data["record"]["text"] = new_content
async with httpx.AsyncClient() as client:
headers = {"Authorization": f"Bearer {jwt_token}"}
response = await client.post(
"https://bsky.social/xrpc/com.atproto.repo.createRecord",
json=post_data,
headers=headers,
)
response.raise_for_status()
BUFFER.remove(post_content)
logging.info(f"Successfully posted: {post_content}")
# Use the existing connection to update the posts buffer
await update_posts(limit=5, jwt_token=jwt_token)
return {"status": "Posted successfully", "post": post_content}
except Exception as e:
logging.error(f"Failed to post to Bluesky: {e}")
return {"status": "Failed to post", "error": str(e)}
def post_from_buffer_job():
asyncio.run(post_from_buffer())
def update_posts_job():
asyncio.run(update_posts())
# Scheduler setup
scheduler = BackgroundScheduler()
scheduler.add_job(post_from_buffer_job, CronTrigger(hour=7, minute=0))
scheduler.add_job(post_from_buffer_job, CronTrigger(hour=11, minute=0))
scheduler.add_job(post_from_buffer_job, CronTrigger(hour=15, minute=0))
scheduler.add_job(post_from_buffer_job, CronTrigger(hour=19, minute=0))
scheduler.add_job(post_from_buffer_job, CronTrigger(hour=23, minute=0))
scheduler.add_job(update_posts_job, CronTrigger(hour=0, minute=0))
scheduler.start()
def get_config(file: str) -> configparser.ConfigParser:
"""Read the configuration file."""
config = configparser.ConfigParser()
config.read(file)
return config
@click.command()
@click.option("--config-file", "-a", type=click.Path(), default="config.ini")
@click.option("--config-profile", "-c", default="TEST", help="Select a config profile")
def main(config_file, config_profile):
"""Main function to start the application."""
global BLUESKY_HANDLE, BLUESKY_PASSWORD
config = get_config(config_file)
BLUESKY_HANDLE = config[config_profile]["BLUESKY_HANDLE"]
BLUESKY_PASSWORD = config[config_profile]["BLUESKY_PASSWORD"]
port = config[config_profile].getint("PORT", 8000)
logging.getLogger("uvicorn.access").addFilter(lambda record: "GET /health" not in record.getMessage())
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=port)
@app.get("/health")
def health_check():
"""Health check endpoint."""
return {"status": "alive"}
if __name__ == "__main__":
main()