-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
439 lines (374 loc) · 15.1 KB
/
app.py
File metadata and controls
439 lines (374 loc) · 15.1 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
from flask import Flask, redirect, render_template, request, current_app, jsonify
from flask_talisman import Talisman
import random
import requests
from lib.arena import Arena, ArenaError
from lib.hn import Hack
from lib.search import Search
from lib.db import get_random_jumpable_site, mark_site_as_not_jumpable, init_db
import os
from dotenv import load_dotenv
from supabase import create_client, Client
from requests.exceptions import ConnectionError, Timeout, RequestException
load_dotenv()
app = Flask(__name__, static_url_path='/static')
# Initialize the database
DB_PATH = "lib/sites.db"
init_db(DB_PATH)
# SUPABASE_URL = os.environ.get("SUPABASE_URL", "")
# SUPABASE_KEY = os.environ.get("SUPABASE_KEY", "")
# supabase_client: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
# class RandomWikipediaPage:
# def __init__(self):
# self.CATEGORIES = [
# 'philosophy_of_mind',
# 'computer_programming',
# 'pseudomathematics',
# ]
# def get_url(self):
# base_url = 'https://en.wikipedia.org/wiki/Special:RandomInCategory/'
# category = random.choice(self.CATEGORIES)
# url = base_url + category
# return url
class RandomWikipediaPage:
def __init__(self):
self.CATEGORIES = [
'philosophy_of_mind',
'computer_programming',
'pseudomathematics',
'carl_jung',
'psychoanalysis',
]
def get_url(self):
# Nov 28 2024 override:
return "https://en.wikipedia.org/wiki/Special:Random"
# Pick a random category
category = random.choice(self.CATEGORIES)
# Use the Wikipedia API to get a list of pages in the chosen category
url = f'https://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:{category}&cmlimit=500&format=json'
response = requests.get(url)
# Check if the request was successful
if response.status_code == 200:
data = response.json()
pages = data.get('query', {}).get('categorymembers', [])
# Choose a random page from the category
if pages:
random_page = random.choice(pages)
page_title = random_page['title'].replace(' ', '_')
return f'https://en.wikipedia.org/wiki/{page_title}'
# Fallback in case of an error or no pages
return f'https://en.wikipedia.org/wiki/Category:{category}'
def check_embeddable(link):
if not link or not link.startswith('https://'):
return False, "Only https URLs can be embedded"
try:
response = requests.head(link, allow_redirects=True, timeout=5)
except (ConnectionError, Timeout, RequestException) as e:
return False, str(e)
x_frame_options = response.headers.get('X-Frame-Options', '').upper()
csp = response.headers.get('Content-Security-Policy', '')
headers_allow_embedding = (
x_frame_options not in ['DENY', 'SAMEORIGIN'] and
'frame-ancestors' not in csp and
'X-Frame-Options' not in csp
)
if not response.ok:
return False, f"Status {response.status_code}"
if not headers_allow_embedding:
return False, "Blocked by X-Frame-Options or Content-Security-Policy"
return True, None
def serialize_jump(candidate, source_kind=None, can_embed=True):
source = candidate.get('source') or {}
channel = candidate.get('channel') or {}
link = candidate.get('url') or source.get('url')
return {
"url": link,
"can_embed": can_embed,
"metadata": {
"source": source_kind or candidate.get('source_kind') or "local",
"title": candidate.get('title') or source.get('title'),
"block_id": candidate.get('id'),
"block_type": candidate.get('type'),
"channel_slug": channel.get('slug'),
"channel_title": channel.get('title'),
"curator": candidate.get('curator'),
"curator_slug": candidate.get('curator_slug'),
"connected_at": candidate.get('connected_at'),
"connection_count": candidate.get('connection_count'),
}
}
def live_arena_jump_payload(mode="random", channel_slug=None, block_id=None, attempts=4):
last_error = None
for _ in range(attempts):
try:
candidate = Arena().get_jump(
mode=mode,
channel_slug=channel_slug,
block_id=block_id,
)
link = candidate.get('url') or (candidate.get('source') or {}).get('url')
can_embed, reason = check_embeddable(link)
if can_embed:
return serialize_jump(candidate, can_embed=True)
last_error = reason
except (ArenaError, RequestException, ValueError) as e:
last_error = str(e)
if last_error:
print(f"Live Are.na jump failed: {last_error}")
return None
def local_db_jump_payload(attempts=3):
for _ in range(attempts):
link = get_random_jumpable_site(DB_PATH)
if not link:
return None
can_embed, reason = check_embeddable(link)
if can_embed:
return serialize_jump(
{
"url": link,
"title": link,
"source_kind": "local:arena-cache",
},
can_embed=True,
)
print(f"Local site {link} is not jumpable: {reason}")
mark_site_as_not_jumpable(link, DB_PATH)
return None
def build_jump_payload():
mode = request.args.get('mode', 'random')
channel_slug = request.args.get('channel') or request.args.get('channel_slug')
block_id = request.args.get('block_id')
try:
block_id = int(block_id) if block_id else None
except ValueError:
block_id = None
if mode not in {"random", "same_channel", "drift"}:
mode = "random"
# Normal jumps remain random; these modes only constrain the random pool.
payload = live_arena_jump_payload(
mode=mode,
channel_slug=channel_slug,
block_id=block_id,
)
if payload:
return payload
payload = local_db_jump_payload()
if payload:
return payload
try:
link = Hack().serve()
return serialize_jump(
{"url": link, "title": link, "source_kind": "hacker-news"},
can_embed=True,
)
except Exception as e:
print(f"Hacker News fallback failed. {str(e)}")
print("Falling back to Wikipedia")
return serialize_jump(
{
"url": "https://en.wikipedia.org/wiki/Special:Random",
"title": "Wikipedia Random",
"source_kind": "wikipedia",
},
can_embed=True,
)
# Serve index.html
@app.route('/')
def index():
return current_app.send_static_file('index.html')
@app.route('/jump')
def jump():
return jsonify(build_jump_payload())
@app.route('/api/jump/next')
def api_jump_next():
return jsonify(build_jump_payload())
@app.route('/old_jump')
def old_jump():
max_attempts = 3 # Number of attempts before Wikipedia fallback
for attempt in range(max_attempts):
# Try Arena first
try:
a = Arena()
a.get_channel_contents()
link = a.get_item_url()
if link and link.startswith('https'):
print(f"Arena link (attempt {attempt + 1}): {link}")
try:
# Check if the link is accessible and can be embedded
response = requests.head(link, allow_redirects=True, timeout=5)
# Check for X-Frame-Options and Content-Security-Policy headers
x_frame_options = response.headers.get('X-Frame-Options', '').upper()
csp = response.headers.get('Content-Security-Policy', '')
# Skip if site blocks framing
if (x_frame_options in ['DENY', 'SAMEORIGIN'] or
'frame-ancestors' in csp or
'X-Frame-Options' in csp):
print(f"Site blocks framing: {link}")
continue
if response.status_code == 200:
# Try to actually connect to verify it works
test_response = requests.get(link, timeout=5)
if test_response.status_code == 200:
# Additional check for common blocking phrases in content
content = test_response.text.lower()
blocking_phrases = [
'blocked by chromium',
'security error',
'cannot be displayed in a frame',
'x-frame-options',
'refused to connect'
]
if not any(phrase in content for phrase in blocking_phrases):
return jsonify({"url": link, "can_embed": True})
else:
print(f"Content contains blocking phrases: {link}")
continue
except (requests.exceptions.ConnectionError,
requests.exceptions.SSLError,
requests.exceptions.TooManyRedirects,
requests.exceptions.RequestException,
requests.exceptions.Timeout) as e:
print(f"Connection error for Arena link: {str(e)}")
continue
except Exception as e:
print(f"Arena error (attempt {attempt + 1}): {str(e)}")
# Try Marginalia
try:
link = Search().random()
if link and link.startswith('https'):
print(f"Marginalia link (attempt {attempt + 1}): {link}")
try:
# Check if the link is accessible and can be embedded
response = requests.head(link, allow_redirects=True, timeout=5)
# Check for X-Frame-Options and Content-Security-Policy headers
x_frame_options = response.headers.get('X-Frame-Options', '').upper()
csp = response.headers.get('Content-Security-Policy', '')
# Skip if site blocks framing
if (x_frame_options in ['DENY', 'SAMEORIGIN'] or
'frame-ancestors' in csp or
'X-Frame-Options' in csp):
print(f"Site blocks framing: {link}")
continue
if response.status_code == 200:
# Try to actually connect to verify it works
test_response = requests.get(link, timeout=5)
if test_response.status_code == 200:
# Additional check for common blocking phrases in content
content = test_response.text.lower()
blocking_phrases = [
'blocked by chromium',
'security error',
'cannot be displayed in a frame',
'x-frame-options',
'refused to connect'
]
if not any(phrase in content for phrase in blocking_phrases):
return jsonify({"url": link, "can_embed": True})
else:
print(f"Content contains blocking phrases: {link}")
continue
except (requests.exceptions.ConnectionError,
requests.exceptions.SSLError,
requests.exceptions.TooManyRedirects,
requests.exceptions.RequestException,
requests.exceptions.Timeout) as e:
print(f"Connection error for Marginalia link: {str(e)}")
continue
except Exception as e:
print(f"Marginalia error (attempt {attempt + 1}): {str(e)}")
print(f"Both sources failed on attempt {attempt + 1}, trying again...")
# Fall back to Wikipedia only after all attempts fail
print("All attempts failed, falling back to Wikipedia")
return jsonify({"url": "https://en.wikipedia.org/wiki/Special:Random", "can_embed": True})
@app.route('/fetch-content', methods=['POST'])
def fetch_content():
url = request.json['url']
try:
response = requests.get(url)
return jsonify({
'content': response.text,
'content_type': response.headers.get('Content-Type', '')
})
except Exception as e:
return jsonify({'error': str(e)}), 500
# random
@app.route('/marginalia_random')
def marginalia_random():
try:
link = Search().random()
return redirect(link)
except Exception as e:
return redirect('/hn')
@app.route('/arena')
def arena():
a = Arena()
a.get_channel_contents()
try:
link = a.get_item_url()
except Exception as e:
link = a.get_item_url()
try:
return redirect(link)
except Exception as e:
return redirect('https://moonjump.app/arena')
@app.route('/devtools')
def devtools():
a = Arena(channel='devtools')
a.get_channel_contents()
try:
link = a.get_item_url()
except Exception as e:
link = a.get_item_url()
try:
return redirect(link)
except Exception as e:
return redirect('https://moonjump.app/devtools')
@app.route('/django')
def django():
a = Arena(channel='django')
a.get_channel_contents()
try:
link = a.get_item_url()
except Exception as e:
link = a.get_item_url()
try:
return redirect(link)
except Exception as e:
return redirect('https://moonjump.app/django')
@app.route('/bookmarks')
def bookmarks():
a = Arena(channel='bookmarks')
a.get_channel_contents()
try:
link = a.get_item_url()
except Exception as e:
link = a.get_item_url()
try:
return redirect(link)
except Exception as e:
return redirect('https://moonjump.app/bookmarks')
@app.route('/hn')
def hn():
h = Hack()
link = h.serve()
try:
return redirect(link)
except Exception as e:
return redirect('https://moonjump.app/hn')
@app.route('/search')
def search():
# Get query parameter, default to empty string if not provided
query = request.args.get('query', '')
if not query:
return jsonify({"error": "No search query provided"}), 400
# Replace spaces with +
query = query.replace(' ', '+')
s = Search()
link = s.get_link(query)
try:
return jsonify({"url": link, "can_embed": True})
except Exception as e:
return jsonify({"error": str(e)}), 500
# Talisman(app, content_security_policy=None)
if __name__ == '__main__':
app.run(debug=True, port=5000)