-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.py
More file actions
243 lines (212 loc) · 8 KB
/
app.py
File metadata and controls
243 lines (212 loc) · 8 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
import hashlib
import os
import secrets
import threading
import time
import requests
from dotenv import load_dotenv
from feedgen.feed import FeedGenerator
from flask import Flask, request, make_response, render_template, flash
from flask_caching import Cache
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from urllib.parse import unquote
app = Flask(__name__)
app.secret_key = secrets.token_hex() # No need to persist this key between resets
load_dotenv()
DIFFBOT_TOKEN = os.getenv("DIFFBOT_TOKEN", None)
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379")
CACHE_TTL = int(os.getenv("CACHE_TTL", 900)) # 15 minutes default
# Configure Flask-Caching with Redis backend, fallback to simple cache for local dev
try:
cache = Cache(app, config={
'CACHE_TYPE': 'redis',
'CACHE_REDIS_URL': REDIS_URL,
'CACHE_DEFAULT_TIMEOUT': CACHE_TTL
})
# Test Redis connection
cache.get('_test')
CACHE_AVAILABLE = True
except Exception:
# Fallback to simple in-memory cache if Redis unavailable
cache = Cache(app, config={
'CACHE_TYPE': 'simple',
'CACHE_DEFAULT_TIMEOUT': CACHE_TTL
})
CACHE_AVAILABLE = False
# Rate limiting - use Redis if available for cross-worker support
storage_uri = REDIS_URL if CACHE_AVAILABLE else 'memory://'
limiter = Limiter(
get_remote_address,
app=app,
storage_uri=storage_uri
)
# Distributed locking to prevent thundering herd on cache misses
_local_locks = {}
_local_locks_guard = threading.Lock()
class DistributedLock:
"""Redis-based distributed lock with fallback to threading lock."""
def __init__(self, key, timeout=30):
self.key = f"lock:{key}"
self.timeout = timeout
self.acquired = False
def __enter__(self):
if CACHE_AVAILABLE:
# Use Redis SETNX for distributed locking
redis_client = cache.cache._read_client
while not redis_client.set(self.key, "1", nx=True, ex=self.timeout):
time.sleep(0.05)
self.acquired = True
else:
# Fallback to threading lock for local dev
with _local_locks_guard:
if self.key not in _local_locks:
_local_locks[self.key] = threading.Lock()
self._lock = _local_locks[self.key]
self._lock.acquire()
self.acquired = True
return self
def __exit__(self, *args):
if self.acquired:
if CACHE_AVAILABLE:
redis_client = cache.cache._read_client
redis_client.delete(self.key)
else:
self._lock.release()
# =============================================================================
# Helper Functions
# =============================================================================
def normalize_url(url):
"""Normalize URL for cache key and rate limiting."""
if not url:
return ''
return unquote(url).lower().strip()
def get_url_for_rate_limit():
"""Get normalized URL from request for rate limiting."""
return normalize_url(request.args.get('url', ''))
def add_cache_headers(response, content):
"""Add HTTP cache headers for RSS readers."""
response.headers['Cache-Control'] = f'public, max-age={CACHE_TTL}'
etag = hashlib.md5(content).hexdigest()
response.headers['ETag'] = f'"{etag}"'
return response
def diffbot_extract(url):
"""Extract list data from Diffbot API."""
try:
payload = {
'token': DIFFBOT_TOKEN,
'url': unquote(url),
'paging': "false"
}
response = requests.get("https://api.diffbot.com/v3/list", params=payload)
data = response.json()
if data.get("error"):
raise Exception(data.get("error", "Page Error"))
obj = data.get("objects", [])[0]
return {
"items": obj.get("items", []),
"title": obj.get("title", ""),
"pageUrl": obj.get("pageUrl", ""),
"icon": obj.get("icon", ""),
"rss_url": obj.get("rss_url")
}
except IndexError:
raise Exception("No content found on page")
except Exception as e:
raise Exception(str(e))
def build_feed(data):
"""Build FeedGenerator from extracted data."""
feed_items = data.get("items", [])
feed_title = data.get("title", "")
feed_url = data.get("pageUrl", "")
feed_icon = data.get("icon", "")
actual_rss_url = data.get("rss_url")
fg = FeedGenerator()
fg.load_extension('media')
fg.title(feed_title or feed_url)
fg.id(feed_url)
fg.description(feed_url)
fg.icon(feed_icon)
fg.link(href=f"https://rss.diffbot.com/rss?url={feed_url}", rel='self')
fg.link(href=feed_url, rel='alternate')
fg.managingEditor("jerome@diffbot.com (Jerome Choo)")
fg.docs("https://rss.diffbot.com")
for article in reversed(feed_items):
if article.get("title") and article.get("link"):
fe = fg.add_entry()
fe.title(article.get("title", ""))
fe.id(article.get("link", ""))
fe.link(href=article.get("link", ""))
if image := article.get("image"):
fe.enclosure(url=image, length=0, type='image/jpeg')
fe.media.thumbnail(url=image)
fe.description(article.get("summary", ""))
if author := article.get("byline") or article.get("author"):
fe.author(name=author)
if published_date := article.get("date"):
fe.pubDate(published_date)
return fg, actual_rss_url
def generate_feed(url):
"""Generate RSS feed with caching and locking to reduce unnecessary Diffbot API calls."""
if not url:
raise Exception("No URL Provided")
cache_key = f"feed:{normalize_url(url)}"
cached_data = cache.get(cache_key)
if cached_data:
return build_feed(cached_data)
# Use distributed lock to prevent thundering herd on cache miss
with DistributedLock(cache_key):
# Double-check cache after acquiring lock (another request may have populated it)
cached_data = cache.get(cache_key)
if cached_data:
return build_feed(cached_data)
feed_data = diffbot_extract(url)
cache.set(cache_key, feed_data, timeout=CACHE_TTL)
return build_feed(feed_data)
# =============================================================================
# Routes
# =============================================================================
@app.route('/')
def index():
return render_template('home.html')
@app.route('/feeds')
def feeds():
feed_detail = {}
actual_rss_url = ""
try:
# Attempt to Generate an RSS Feed
fg, actual_rss_url = generate_feed(request.args.get('url', None))
# Feed Details
feed_detail = {
"title": fg.title(),
"description": fg.description(),
"link": fg.link(),
"icon": fg.icon()
}
except Exception as e:
flash(str(e), 'Error')
return render_template('home.html', page_url=request.args.get('url', ''), feed_detail=feed_detail, actual_rss_url=actual_rss_url)
@app.route('/rss')
@limiter.limit("60/minute", error_message='Rate limit exceeded')
def rss():
try:
fg, rss_url = generate_feed(request.args.get('url', None))
content = fg.rss_str()
response = make_response(content)
response.headers.set('Content-Type', 'application/rss+xml')
add_cache_headers(response, content)
return response
except Exception as e:
return make_response(str(e), 400)
@app.route('/atom')
@limiter.limit("60/minute", error_message='Rate limit exceeded')
def atom():
try:
fg, rss_url = generate_feed(request.args.get('url', None))
content = fg.atom_str()
response = make_response(content)
response.headers.set('Content-Type', 'application/atom+xml')
add_cache_headers(response, content)
return response
except Exception as e:
return make_response(str(e), 400)