Add Commonly Visited sites to SearXNG homepage
This revision is from 2024/07/25 21:53. You can Restore it.
/usr/local/searxng/searxng-src/searx/webapp.py
import os
def get_most_visited_sites(limit=9):
sites = []
file_path = os.path.join(os.path.dirname(__file__), 'most_visited_sites.txt')
try:
with open(file_path, 'r') as file:
for line in file:
parts = line.strip().split('|')
if len(parts) == 3:
sites.append({
"url": parts[0],
"title": parts[1],
"favicon": parts[2]
})
if len(sites) == limit:
break
except FileNotFoundError:
print(f"Warning: {file_path} not found. Using default sites.")
# Fallback to default sites if file is not found
sites = [
{"url": "https://imtcoin.com", "title": "Immortality", "favicon": "https://imtcoin.com/favicon.ico"},
{"url": "https://wikipedia.org", "title": "Wikipedia", "favicon": "https://wikipedia.org/favicon.ico"},
{"url": "https://github.com", "title": "GitHub", "favicon": "https://github.com/favicon.ico"},
]
return sites[:limit]
@app.route('/', methods=['GET', 'POST'])
def index():
"""Render index page."""
# redirect to search if there's a query in the request
if request.form.get('q'):
query = ('?' + request.query_string.decode()) if request.query_string else ''
return redirect(url_for('search') + query, 308)
# Get the most visited sites
most_visited_sites = get_most_visited_sites(limit=9)
return render(
'index.html',
selected_categories=get_selected_categories(request.preferences, request.form),
current_locale=request.preferences.get_value("locale"),
most_visited_sites=most_visited_sites
)
/usr/local/searxng/searxng-src/searx/templates/index.html
{% extends "simple/base.html" %}
{% from 'simple/icons.html' import icon_big %}
{% block content %}
<div class="index">
<div class="title"><h1>SearXNG</h1></div>
{% include 'simple/simple_search.html' %}
{% if most_visited_sites %}
<div class="top-sites">
<h2>Top Sites</h2>
<div class="site-grid">
{% for site in most_visited_sites %}
<a href="{{ site.url }}" class="site-tile">
<img src="{{ site.favicon }}" alt="{{ site.title }} favicon">
<span>{{ site.title }}</span>
</a>
{% endfor %}
</div>
</div>
{% endif %}
</div>
{% endblock %}