-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
272 lines (228 loc) · 8.43 KB
/
app.py
File metadata and controls
272 lines (228 loc) · 8.43 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
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
from typing import Union
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from datetime import datetime
from atproto import Client, client_utils
from pydantic import BaseModel
import subprocess
import requests
import os
app = FastAPI()
# Mount the `static` directory to serve static files
app.mount("/static", StaticFiles(directory="static"), name="static")
class SocialPost(BaseModel):
username: str
password: str
text: str
link: str | None = None
baseURL: Union[str, None] = None # Mastodon root URL (optional)
class Config:
allow_population_by_field_name = True
alias_generator = lambda s: s.upper()
populate_by_name = True
@app.get("/", response_class=HTMLResponse)
def read_root():
# Existing code remains unchanged
try:
uptime_output = subprocess.run(
["uptime"],
capture_output=True,
text=True,
shell=True
)
uptimeoutput = "N/A"
if uptime_output.returncode == 0:
uptimeoutput = uptime_output.stdout
# Get current time
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Read last 50 lines of log file
log_output = subprocess.run(
["tail", "-n", "50", "/var/log/pybsposter.log"],
capture_output=True,
text=True
)
if log_output.returncode == 0:
logs = log_output.stdout.split('\n')
logs = [log.strip() for log in logs if log]
else:
logs = ["No log entries found"]
except subprocess.CalledProcessError as e:
print(f"Error: {e}")
uptime_seconds, current_time, logs = "N/A", "N/A", ["Error reading metrics or logs"]
# Get app version from environment variable
app_version = os.environ.get("VERSION", "unknown")
return f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PyBsPoster</title>
<link rel="icon" href="/static/favicon.ico" type="image/x-icon">
<style>
body {{
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}}
.container {{
text-align: center;
background: #ffffff;
padding: 2em;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}}
.logo {{
max-width: 200px;
}}
.title {{
color: #333;
font-size: 1.2em;
margin: 20px 0;
}}
h1 {{
color: #333;
font-size: 2em;
}}
p {{
color: #555;
font-size: 1.2em;
}}
.version {{
margin-top: 1em;
font-weight: bold;
color: #007BFF;
}}
</style>
</head>
<body>
<div class="container">
<img src="/static/logo3.png" alt="Logo" class="logo">
<h1 class="title">Welcome to PyBSPoster</h1>
<p>The application is running smoothly.</p>
<p>Documentation for this instance can be found:</p>
<div class="title"><a href="/docs">Swagger (OpenAPI)</a><br/><a href="/redoc">Redocs</a><br/></div>
<div class="version">Version: {app_version}</div><br/><br/>
<div>Uptime: {uptimeoutput}</div><br/><br/>
<div>Last Updated: {current_time}</div>
</div>
</body>
</html>
"""
@app.post("/preview")
async def preview_social(post: SocialPost):
"""
Generate a text preview of the user's post with trimming logic applied.
Args:
post (SocialPost): The post data including username, text, and optional link.
Returns:
dict: A dictionary containing the trimmed preview text and link.
"""
# Extract text and link from the user's input
text = post.text
link = post.link if post.link else ""
# Calculate the total length of text and link
total_length = len(text) + len(link)
# Trim the text if combined length exceeds 300 characters
if total_length > 300:
preview_text = text[:(300 - len(link) - 4)] + "... " # Trim and add ellipsis
else:
preview_text = text
# Return the preview response
return {
"preview_text": preview_text,
"link": link
}
@app.post("/post")
async def post_social(post: SocialPost):
"""
Post to BlueSky.
Args:
post (SocialPost): Contains the text, optional link
username and password for your BlueSky account.
NOTE: baseURL is not used for BlueSky, but is included for consistency.
Returns:
Response from Bluesky or status message.
"""
# Debug
# print(f"Received username: {post.username}", flush=True)
# print(f"Received password: {post.password}", flush=True)
# print(f"Received text: {post.text}", flush=True)
# print(f"Received link: {post.link}", flush=True)
# Calculate the total length of text and link
text = post.text
link = post.link if post.link else ""
username = post.username
password = post.password
total_length = len(text) + len(link)
if total_length > 300:
text = text[:(300 - len(link) - 4)] + "... " # Trim and add ellipsis
try:
client = Client()
except Exception as e:
print(f"Failed to connect to server: {e}")
profile = client.login(username, password)
builder = client_utils.TextBuilder().text(text).link(link,link)
text_string = str(builder) # Convert TextBuilder to a string
try:
post = client.send_post(builder)
response = {
"YOU ARE:": profile.display_name,
"TEXT": text_string,
"LINK": link
}
return response
except Exception as e:
print(f"Failed to post: {e}")
return {'error': 'Failed to send post'}, 500
@app.post("/post/mastodon")
async def post_to_mastodon(post: SocialPost):
"""
Post to Mastodon.
Args:
post (SocialPost): Contains the text, optional link, and the Mastodon instance URL (baseURL).
the password is the API key for Mastodon.
NOTE: The username is not used for Mastodon, but is included for consistency.
Returns:
Response from Mastodon or status message.
"""
# Ensure the baseURL is provided for Mastodon posts
if not post.baseURL:
raise HTTPException(status_code=400, detail="baseURL is required for Mastodon posting.")
# Construct the API URL from the baseURL provided
mastodon_api_url = f"{post.baseURL.rstrip('/')}/api/v1/statuses"
# Construct the payload for Mastodon API
text = post.text[:499] + "..." if len(post.text) > 499 else post.text
payload = {
"status": text + (" " + post.link if post.link else "")
}
# The API Key will be used as the password for authentication
if not post.password:
raise HTTPException(status_code=400, detail="Password (API key) is required for Mastodon posting.")
mastodon_api_key = post.password
if not mastodon_api_key:
raise HTTPException(status_code=500, detail="Missing Mastodon API key")
# Send POST request to Mastodon API
try:
response = requests.post(
mastodon_api_url,
json=payload,
headers={
"Authorization": f"Bearer {mastodon_api_key}",
"Content-Type": "application/json"
}
)
# Check if Mastodon accepted the post
if response.status_code == 200:
return {"message": "Post successfully shared on Mastodon"}
else:
response.raise_for_status()
except Exception as e:
print(f"Failed to post to Mastodon: {e}")
return {'error': 'Failed to send post'}, 500