-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
271 lines (231 loc) · 8.51 KB
/
main.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
from fasthtml import FastHTML
from fasthtml.common import fast_app, Form, Head, Picture, SortableJS, Hidden, HTMLResponse, serve, database, Div, Card, MarkdownJS, A, Html, H3, Title, Body, Img, Titled, Article, Header, P, Footer, Main, H1, Style, picolink, H2, Ul, Li, Script
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import JSONResponse
from fastapi.middleware.trustedhost import TrustedHostMiddleware
from fastapi.middleware.httpsredirect import HTTPSRedirectMiddleware
from slack_bolt.adapter.fastapi.async_handler import AsyncSlackRequestHandler
from limits import parse_many
import pandas as pd
from datetime import datetime, timedelta
from summariser.newsletter_creator import update_items_from_articles, get_last_update_date, db, create_newsletter
from config import settings
from slack_handlers import app as slack_app
from utils import setup_rate_limiter, setup_logging
logger = setup_logging()
# Set up rate limiting
limiter = setup_rate_limiter()
rate_limits = parse_many(settings.RATE_LIMIT)
handler = AsyncSlackRequestHandler(slack_app)
items = db.t.items
comparisons = db.t.comparisons
last_update = db.t.last_update
newsletter_summaries = db.t.newsletter_summaries
pico_css = Style('''
:root {
--pico-font-size: 100%;
--pico-font-family: Pacifico;
--card-border-color: #d1d5db;
--card-background: #f9fafb;
}
.item-card {
margin-bottom: 1rem;
border: 1px solid var(--card-border-color);
border-radius: 0.5rem;
padding: 1rem;
background-color: var(--card-background);
cursor: move;
transition: all 0.3s ease;
}
.item-list {
list-style: none !important;
padding-left: 0 !important;
margin-left: 0 !important;
}
.item-list li {
list-style-type: none !important;
}
.long-item {
padding: 1.5rem;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.long-item h3 {
font-size: 1.5rem;
margin-bottom: 1rem;
}
.short-item {
padding: 1rem;
}
.short-item h3 {
font-size: 1.2rem;
margin-bottom: 0.5rem;
}
.link-item {
padding: 0.5rem;
border: none;
background: none;
}
.link-item h3 {
font-size: 1rem;
margin-bottom: 0;
}
.long-summary, .short-summary, .read-more {
display: none;
}
.long-item .long-summary,
.short-item .short-summary,
.long-item .read-more,
.short-item .read-more {
display: block;
}
h2 {
margin-top: 2rem;
margin-bottom: 1rem;
border-bottom: 2px solid var(--card-border-color);
padding-bottom: 0.5rem;
}
.newsletter-summary {
background-color: #f0f4f8;
border: 1px solid #d1d5db;
border-radius: 0.5rem;
padding: 1rem;
margin-bottom: 2rem;
}
ul, ol {
list-style-type: none;
}
.sortable {
list-style-type: none;
}
#header-image {
display: block;
margin-left: auto;
margin-right: auto;
max-width: 30%;
height: auto;
}
''')
class StoryCard:
def __init__(self, title, url, long_summary, short_summary, item_id):
self.title = title
self.url = url
self.long_summary = long_summary
self.short_summary = short_summary
self.item_id = item_id
def render(self, format_type):
base_class = "item-card"
if format_type == "long":
base_class += " long-item"
elif format_type == "short":
base_class += " short-item"
else: # link format
base_class += " link-item"
return Li(
Article(
H3(A(self.title, href=self.url)),
P(self.long_summary, cls="long-summary"),
P(self.short_summary, cls="short-summary"),
Footer(A("Read more", href=self.url, cls="secondary read-more")),
Hidden(id="id", value=self.item_id),
cls=base_class,
)
)
app, rt = fast_app(hdrs=(picolink, pico_css, SortableJS('.sortable'),))
LONG_ITEM_COUNT = 3
SHORT_ITEM_COUNT = 4
@app.get("/")
def home():
last_update = get_last_update_date()
current_date = datetime.now().date()
if not last_update or (current_date - last_update) >= timedelta(days=7):
if current_date.weekday() == 4: # 4 represents Friday (0 is Monday, 6 is Sunday)
print("Updating items from Omnivore...")
create_newsletter()
last_update = current_date
try:
df = pd.DataFrame(items())
df_sorted = df.sort_values('interest_score', ascending=False).reset_index(drop=True)
except KeyError:
logger.error("No items found in the database")
create_newsletter()
df_sorted = pd.DataFrame()
df = pd.DataFrame(items())
df_sorted = df.sort_values('interest_score', ascending=False).reset_index(drop=True)
item_cards = []
for i, row in df_sorted.iterrows():
card = StoryCard(row['title'], row['url'], row['long_summary'], row['short_summary'], row['id'])
format_type = "long" if i < LONG_ITEM_COUNT else "short" if i < LONG_ITEM_COUNT + SHORT_ITEM_COUNT else "link"
item_cards.append(card.render(format_type))
card_container = Form(*item_cards,
id='story-container', cls='sortable', hx_trigger="end", hx_post="/reorder", hx_swap="innerHTML", hx_target="#story-container")
# Get the latest newsletter summary
latest_summary = newsletter_summaries(order_by='-date', limit=1)
summary_content = latest_summary[0]['summary'] if latest_summary else "No newsletter summary available."
page = (Title('Bedtime Reading'),
Img(src=f"/header.png", id=f'header-image'),
Main(
Div(
P(f"Last updated on: {last_update.strftime('%Y-%m-%d') if last_update else 'Never'}", cls="last-updated"),
Div(summary_content, cls="newsletter-summary"),
card_container,
cls="container"
)
)
)
return page
@app.post("/update")
async def update():
current_date = datetime.now().date()
last_update.update({'date': current_date})
@app.post("/reorder")
async def reorder(request: Request):
form_data = await request.form()
ids = form_data.getlist("id")
new_order = ids
df = pd.DataFrame(items())
df_sorted = df.sort_values('interest_score', ascending=False).reset_index(drop=True)
# Get the original order of items
original_order = df_sorted['id'].tolist()
# Record comparisons
for new_index, item_id in enumerate(new_order):
item_id = int(item_id)
old_index = original_order.index(item_id)
if new_index < old_index:
# This item has moved up in the ranking
for losing_id in original_order[new_index:old_index]:
if losing_id != item_id:
comparisons.insert({
'winning_id': item_id,
'losing_id': losing_id
})
# Reorder items based on new order
reordered_items = []
for item_id in new_order:
item = df_sorted[df_sorted['id'] == int(item_id)].iloc[0]
reordered_items.append(item)
# Render the reordered items
item_cards = []
for i, row in enumerate(reordered_items):
card = StoryCard(row['title'], row['url'], row['long_summary'], row['short_summary'], row['id'])
format_type = "long" if i < LONG_ITEM_COUNT else "short" if i < LONG_ITEM_COUNT + SHORT_ITEM_COUNT else "link"
item_cards.append(card.render(format_type))
return Li(*item_cards)
@app.post("/slack/events")
async def slack_events(req: Request):
try:
# Check rate limits
for rate_limit in rate_limits:
if not limiter.hit(rate_limit, "global", req.client.host):
logger.warning("Rate limit exceeded")
raise HTTPException(status_code=429, detail="Too many requests")
body = await req.json()
logger.info(f"Received Slack event: {body}")
# Handle URL verification
if body.get("type") == "url_verification":
return {"challenge": body["challenge"]}
return await handler.handle(req)
except Exception as e:
logger.error(f"Error handling Slack event: {str(e)}", exc_info=True)
raise HTTPException(status_code=500, detail="An error occurred processing the Slack event")
6
serve()