feat: added just-search

main
Jonathan Peacher 2023-07-01 20:59:35 -05:00
rodzic 0dc360b009
commit 96531797ba
Nie znaleziono w bazie danych klucza dla tego podpisu
8 zmienionych plików z 52 dodań i 1 usunięć

Wyświetl plik

@ -52,6 +52,7 @@ INSTALLED_APPS = [
'recipes',
'neapolitan',
'user_solo',
'search',
]
MIDDLEWARE = [
@ -139,6 +140,7 @@ ACCOUNT_FORMS = {
ACCOUNT_AUTHENTICATION_METHOD = "username"
ACCOUNT_SIGNUP_REDIRECT_URL = "/"
LOGIN_REDIRECT_URL = "/"
# Internationalization

Wyświetl plik

@ -23,5 +23,6 @@ urlpatterns = [
path('', TemplateView.as_view(template_name="projects.html")),
path('accounts/', include('allauth.urls')),
path('recipes/', include('recipes.views')),
path('search/', include('search.views')),
path(settings.ADMIN_URL, admin.site.urls),
]

Wyświetl plik

@ -4,4 +4,5 @@ dj-database-url==2.0.0
psycopg2-binary==2.9.6
gunicorn==20.1.0
django-filter==23.2
neapolitan==23.11
neapolitan==23.11
beautifulsoup4==4.12.2

4
search/README.md 100644
Wyświetl plik

@ -0,0 +1,4 @@
# Just Search
- Just search.
- Maybe more private? 🤔

Wyświetl plik

6
search/apps.py 100644
Wyświetl plik

@ -0,0 +1,6 @@
from django.apps import AppConfig
class SearchConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'search'

Wyświetl plik

@ -0,0 +1,15 @@
{% extends 'index.html'%}
{% block index_main %}
<h2>Just Search</h2>
<form>
<input type="text" name="q" value="{{ query }}" style="width: 100%;" {% if not query %}autofocus{% endif %}>
</form>
{% for item in result %}
<a href="{{ item.link }}">
<h3 style="margin-bottom: 0;">{{ item.title }}</h3>
</a>
<p style="margin-top: 0;">{{ item.link }}</p>
{% endfor %}
{% endblock %}

22
search/views.py 100644
Wyświetl plik

@ -0,0 +1,22 @@
import requests
from bs4 import BeautifulSoup
from django.shortcuts import render
from django.urls import path
def search(request):
query = request.GET.get('q', '')
html = requests.get(f'https://www.google.com/search?q={query}').text
soup = BeautifulSoup(html, 'html.parser')
data = soup.select('a:has(h3)')
result = []
for item in data:
if not item.get('href').startswith('/search'):
result.append({
'title': item.h3.div.contents[0],
'link': item.get('href').replace('/url?q=', '').split('&', 1)[0]
})
return render(request, 'search/search.html', {'result': result, 'query': query})
urlpatterns = [
path('', search),
]