Compare commits
7 Commits
13df4a7831
...
ff1297e507
| Author | SHA1 | Date | |
|---|---|---|---|
| ff1297e507 | |||
| 1d019f880b | |||
| 23b56b26b1 | |||
| b439199836 | |||
| 5736cde21a | |||
| ed8ad1b6f6 | |||
| 75779722c1 |
@@ -16,6 +16,7 @@ import traceback
|
||||
import time
|
||||
import datetime
|
||||
import humanize
|
||||
import urllib.request
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
|
||||
import settings
|
||||
@@ -28,6 +29,25 @@ from flask import abort, Flask, request, render_template, stream_with_context, R
|
||||
from werkzeug.exceptions import NotFound
|
||||
from flask_cors import CORS
|
||||
|
||||
smallweb_set = set()
|
||||
def load_smallweb_list():
|
||||
EXCLUDED = [
|
||||
'github.com',
|
||||
]
|
||||
|
||||
global smallweb_set
|
||||
try:
|
||||
url = 'https://raw.githubusercontent.com/kagisearch/smallweb/refs/heads/main/smallweb.txt'
|
||||
with urllib.request.urlopen(url, timeout=10) as response:
|
||||
urls = response.read().decode('utf-8').splitlines()
|
||||
hosts = {urlparse(u).hostname for u in urls if u and urlparse(u).hostname}
|
||||
smallweb_set = {h.replace('www.', '') for h in hosts if h not in EXCLUDED}
|
||||
logging.info('Loaded {} smallweb domains.'.format(len(smallweb_set)))
|
||||
except Exception as e:
|
||||
logging.error('Failed to load smallweb list: {}'.format(e))
|
||||
|
||||
load_smallweb_list()
|
||||
|
||||
database.init()
|
||||
search.init()
|
||||
|
||||
@@ -56,7 +76,39 @@ cors = CORS(flask_app)
|
||||
def api():
|
||||
skip = request.args.get('skip', 0)
|
||||
limit = request.args.get('limit', settings.FEED_LENGTH)
|
||||
stories = database.get_stories(limit, skip)
|
||||
|
||||
if request.args.get('smallweb') == 'true' and smallweb_set:
|
||||
limit = int(limit)
|
||||
skip = int(skip)
|
||||
filtered_stories = []
|
||||
current_skip = skip
|
||||
|
||||
while len(filtered_stories) < limit:
|
||||
stories_batch = database.get_stories(limit, current_skip)
|
||||
if not stories_batch:
|
||||
break
|
||||
|
||||
for story_str in stories_batch:
|
||||
story = json.loads(story_str)
|
||||
story_url = story.get('url') or story.get('link') or ''
|
||||
if not story_url:
|
||||
continue
|
||||
hostname = urlparse(story_url).hostname
|
||||
if hostname:
|
||||
hostname = hostname.replace('www.', '')
|
||||
if hostname in smallweb_set:
|
||||
filtered_stories.append(story_str)
|
||||
if len(filtered_stories) == limit:
|
||||
break
|
||||
|
||||
if len(filtered_stories) == limit:
|
||||
break
|
||||
|
||||
current_skip += limit
|
||||
stories = filtered_stories
|
||||
else:
|
||||
stories = database.get_stories(limit, skip)
|
||||
|
||||
# hacky nested json
|
||||
res = Response('{"stories":[' + ','.join(stories) + ']}')
|
||||
res.headers['content-type'] = 'application/json'
|
||||
|
||||
@@ -8,9 +8,19 @@ function Feed({ updateCache }) {
|
||||
const [stories, setStories] = useState(() => JSON.parse(localStorage.getItem('stories')) || false);
|
||||
const [error, setError] = useState('');
|
||||
const [loadingStatus, setLoadingStatus] = useState(null);
|
||||
const [filterSmallweb, setFilterSmallweb] = useState(() => localStorage.getItem('filterSmallweb') === 'true');
|
||||
|
||||
const handleFilterChange = e => {
|
||||
const isChecked = e.target.checked;
|
||||
setFilterSmallweb(isChecked);
|
||||
localStorage.setItem('filterSmallweb', isChecked);
|
||||
setStories(false);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetch('/api')
|
||||
const controller = new AbortController();
|
||||
|
||||
fetch(filterSmallweb ? '/api?smallweb=true' : '/api', { signal: controller.signal })
|
||||
.then(res => {
|
||||
if (!res.ok) {
|
||||
throw new Error(`Server responded with ${res.status} ${res.statusText}`);
|
||||
@@ -37,10 +47,13 @@ function Feed({ updateCache }) {
|
||||
let preloadedCount = 0;
|
||||
|
||||
for (const [index, newStory] of newApiStories.entries()) {
|
||||
if (controller.signal.aborted) {
|
||||
break;
|
||||
}
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10-second timeout
|
||||
const storyRes = await fetch('/api/' + newStory.id, { signal: controller.signal });
|
||||
const storyFetchController = new AbortController();
|
||||
const timeoutId = setTimeout(() => storyFetchController.abort(), 10000); // 10-second timeout
|
||||
const storyRes = await fetch('/api/' + newStory.id, { signal: storyFetchController.signal });
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!storyRes.ok) {
|
||||
@@ -89,11 +102,17 @@ function Feed({ updateCache }) {
|
||||
setLoadingStatus(null);
|
||||
},
|
||||
(error) => {
|
||||
if (error.name === 'AbortError') {
|
||||
console.log('Feed fetch aborted.');
|
||||
return;
|
||||
}
|
||||
const errorMessage = `Failed to fetch the main story list from the API. Your connection may be down or the server might be experiencing issues. ${error.toString()}.`;
|
||||
setError(errorMessage);
|
||||
}
|
||||
);
|
||||
}, [updateCache]);
|
||||
|
||||
return () => controller.abort();
|
||||
}, [updateCache, filterSmallweb]);
|
||||
|
||||
return (
|
||||
<div className='container'>
|
||||
@@ -102,6 +121,11 @@ function Feed({ updateCache }) {
|
||||
<meta name="robots" content="index" />
|
||||
</Helmet>
|
||||
|
||||
<div style={{marginBottom: '1rem'}}>
|
||||
<input type="checkbox" id="filter-smallweb" className="checkbox" checked={filterSmallweb} onChange={handleFilterChange} />
|
||||
<label htmlFor="filter-smallweb">Only Smallweb</label>
|
||||
</div>
|
||||
|
||||
{error &&
|
||||
<details style={{marginBottom: '1rem'}}>
|
||||
<summary>Connection error? Click to expand.</summary>
|
||||
|
||||
@@ -66,3 +66,7 @@
|
||||
.black .comment.lined {
|
||||
border-left: 1px solid #444444;
|
||||
}
|
||||
|
||||
.black .checkbox:checked + label::after {
|
||||
border-color: #ddd;
|
||||
}
|
||||
|
||||
@@ -62,3 +62,7 @@
|
||||
.dark .comment.lined {
|
||||
border-left: 1px solid #444444;
|
||||
}
|
||||
|
||||
.dark .checkbox:checked + label::after {
|
||||
border-color: #ddd;
|
||||
}
|
||||
|
||||
@@ -310,3 +310,44 @@ button.comment {
|
||||
cursor: pointer;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.checkbox {
|
||||
-webkit-appearance: none;
|
||||
appearance: none;
|
||||
position: absolute;
|
||||
opacity: 0;
|
||||
cursor: pointer;
|
||||
height: 0;
|
||||
width: 0;
|
||||
}
|
||||
|
||||
.checkbox + label {
|
||||
position: relative;
|
||||
cursor: pointer;
|
||||
padding-left: 1.75rem;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.checkbox + label::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0.1em;
|
||||
width: 1rem;
|
||||
height: 1rem;
|
||||
border: 1px solid #828282;
|
||||
background-color: transparent;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
.checkbox:checked + label::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
left: 0.35rem;
|
||||
top: 0.2em;
|
||||
width: 0.3rem;
|
||||
height: 0.6rem;
|
||||
border: solid #000;
|
||||
border-width: 0 2px 2px 0;
|
||||
transform: rotate(45deg);
|
||||
}
|
||||
|
||||
@@ -80,3 +80,11 @@
|
||||
.red .dot {
|
||||
background-color: #440000;
|
||||
}
|
||||
|
||||
.red .checkbox + label::before {
|
||||
border: 1px solid #690000;
|
||||
}
|
||||
|
||||
.red .checkbox:checked + label::after {
|
||||
border-color: #aa0000;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user