From 96531797baae0b1b8e37549997765e63c2afb728 Mon Sep 17 00:00:00 2001 From: Jonathan Peacher Date: Sat, 1 Jul 2023 20:59:35 -0500 Subject: [PATCH] feat: added just-search --- projects/settings.py | 2 ++ projects/urls.py | 1 + requirements.txt | 3 ++- search/README.md | 4 ++++ search/__init__.py | 0 search/apps.py | 6 ++++++ search/templates/search/search.html | 15 +++++++++++++++ search/views.py | 22 ++++++++++++++++++++++ 8 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 search/README.md create mode 100644 search/__init__.py create mode 100644 search/apps.py create mode 100644 search/templates/search/search.html create mode 100644 search/views.py diff --git a/projects/settings.py b/projects/settings.py index f1842be..91d67b2 100644 --- a/projects/settings.py +++ b/projects/settings.py @@ -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 diff --git a/projects/urls.py b/projects/urls.py index 0e51c6a..49d5046 100644 --- a/projects/urls.py +++ b/projects/urls.py @@ -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), ] diff --git a/requirements.txt b/requirements.txt index c8c8ce5..a3a1287 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file +neapolitan==23.11 +beautifulsoup4==4.12.2 \ No newline at end of file diff --git a/search/README.md b/search/README.md new file mode 100644 index 0000000..ec173e0 --- /dev/null +++ b/search/README.md @@ -0,0 +1,4 @@ +# Just Search + +- Just search. +- Maybe more private? 🤔 diff --git a/search/__init__.py b/search/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/search/apps.py b/search/apps.py new file mode 100644 index 0000000..3afccb0 --- /dev/null +++ b/search/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class SearchConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'search' diff --git a/search/templates/search/search.html b/search/templates/search/search.html new file mode 100644 index 0000000..66f1719 --- /dev/null +++ b/search/templates/search/search.html @@ -0,0 +1,15 @@ +{% extends 'index.html'%} + +{% block index_main %} +

Just Search

+
+ +
+ + {% for item in result %} + +

{{ item.title }}

+
+

{{ item.link }}

+ {% endfor %} +{% endblock %} \ No newline at end of file diff --git a/search/views.py b/search/views.py new file mode 100644 index 0000000..ed62ee6 --- /dev/null +++ b/search/views.py @@ -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), +] \ No newline at end of file