Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add engine filter option and date range for google #175

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def discover():

@app.route('/save-settings', methods=['POST'])
def save_settings():
cookies = ['safe', 'javascript', 'domain', 'theme', 'lang', 'ux_lang', 'new_tab', 'method', 'ac']
cookies = ['safe', 'javascript', 'domain', 'theme', 'lang', 'ux_lang', 'new_tab', 'method', 'ac', "engine"]

response = make_response(redirect(request.referrer))
for cookie in cookies:
Expand All @@ -107,8 +107,13 @@ def save_settings():
max_age=COOKIE_AGE, httponly=False,
secure=app.config.get("HTTPS")
)

response.headers["Location"] = request.form.get('past')

# Disable https when running in a debug mode to avoid ssl errors
if __name__ == "__main__":
response.headers["Location"] = response.headers["Location"].replace("https", "http")

return response


Expand Down
3 changes: 3 additions & 0 deletions _config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
REPO = 'https://github.com/Extravi/araa-search'
DONATE = 'https://github.com/sponsors/Extravi'

DEFAULT_ENGINE = "google"


# Default theme
DEFAULT_THEME = 'dark_default'

Expand Down
3 changes: 3 additions & 0 deletions src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def bytes_to_string(size):
index += 1
return f"{size:.2f} {units[index]}"


class Settings():
def __init__(self):
self.domain = request.cookies.get("domain", DEFAULT_GOOGLE_DOMAIN)
Expand All @@ -157,6 +158,8 @@ def __init__(self):
self.theme = request.cookies.get("theme", DEFAULT_THEME)
self.method = request.cookies.get("method", DEFAULT_METHOD)
self.ac = request.cookies.get("ac", DEFAULT_AUTOCOMPLETE)
self.engine = request.cookies.get("engine", DEFAULT_ENGINE)


# Returns a tuple of two ellements.
# The first is the wikipedia proxy's URL (used to load an wiki page's image after page load),
Expand Down
16 changes: 13 additions & 3 deletions src/textResults.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,18 @@ def textResults(query: str) -> Response:
ratelimited = True # Used to determine if complete engine failure is due to a bug or due to
# the server getting completely ratelimited from every supported engine.

engine_list = []
for engine in ENGINES:
if engine.NAME == settings.engine:
# Put prefered engine at the top of the list
engine_list = [engine] + engine_list
else:
engine_list.append(engine)


try:
for ENGINE in ENGINES:
if (t := ratelimited_timestamps.get(ENGINE.__name__)) != None and t + ENGINE_RATELIMIT_COOLDOWN_MINUTES * 60 >= time.time():
for ENGINE in engine_list:
if (t := ratelimited_timestamps.get(ENGINE.__name__)) is not None and t + ENGINE_RATELIMIT_COOLDOWN_MINUTES * 60 >= time.time():
# Current engine is ratelimited. Skip it.
continue
results = ENGINE.search(query, p, search_type, settings)
Expand Down Expand Up @@ -138,5 +147,6 @@ def textResults(query: str) -> Response:
type=search_type, repo_url=REPO, donate_url=DONATE, commit=helpers.latest_commit(),
exported_math_expression=exported_math_expression, API_ENABLED=API_ENABLED,
TORRENTSEARCH_ENABLED=TORRENTSEARCH_ENABLED, lang_data=lang_data,
settings=settings, wiki=results.wiki, araa_name=ARAA_NAME
settings=settings, wiki=results.wiki, araa_name=ARAA_NAME,
before=args.get("before", ""), after=args.get("after", "")
)
11 changes: 11 additions & 0 deletions src/text_engines/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from src.text_engines.objects.fullEngineResults import FullEngineResults
from src.text_engines.objects.wikiSnippet import WikiSnippet
from src.text_engines.objects.textResult import TextResult
from flask import request


NAME = "google"


def __local_href__(url):
Expand All @@ -17,6 +21,13 @@ def search(query: str, page: int, search_type: str, user_settings: helpers.Setti
if search_type == "reddit":
query += " site:reddit.com"

after_date = request.args.get("after", "")
before_date = request.args.get("before", "")
if after_date != "":
query += f" after:{after_date}"
if before_date != "":
query += f" before:{before_date}"

# Random characters are to trick google into thinking it's a mobile phone
# loading more results
# -> https://github.com/searxng/searxng/issues/159
Expand Down
2 changes: 2 additions & 0 deletions src/text_engines/qwant.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from urllib.parse import urlparse, urlencode, unquote
from src import helpers

NAME = "qwant"


def sanitize_wiki(desc):
desc = re.sub(r"\[\d{1,}\]", "", desc)
Expand Down
10 changes: 10 additions & 0 deletions static/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ document.addEventListener("DOMContentLoaded", function () {
});
}

const engineSelect = document.querySelector("#engine_select")
if (engineSelect) {
engineSelect.addEventListener("change", function () {
const selectedOption = engineSelect.options[engineSelect.selectedIndex];
const selectedValue = selectedOption.value;
setCookie("engine", selectedValue);
window.location.reload();
});
}

const themeDivs = document.querySelectorAll(".themes-settings-menu div");

themeDivs.forEach(function (div) {
Expand Down
40 changes: 38 additions & 2 deletions static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ hr {
#lang,
#safe,
#open-new-tab,
#engine,
#domain,
#javascript-setting,
#ux_lang {
Expand Down Expand Up @@ -680,18 +681,23 @@ hr {
}

.results_settings {
margin-top: 110px;
}

.results_settings,
#date_range_form {
color: var(--fg);
left: 175px;
position: relative;
font-size: 15px;
margin-bottom: 10px;
max-width: 580px;
margin-top: 110px;
display: flex;
}


.results-settings,
.results-save,
.date-select,
.torrent-settings,
.torrent-sort-save,
.torrent-cat {
Expand All @@ -707,6 +713,25 @@ hr {
transition: all .3s ease;
}

#date_range_form .results-settings {
text-align: left;
}

.date_range {
display: inherit;
}

.date_range p {
width: 80px;
margin: 4px;
}

.date-select {
height: 20px;
display: inline;
}


.torrent-cat:hover,
.torrent-settings:hover,
.torrent-sort-save:hover {
Expand Down Expand Up @@ -1657,6 +1682,17 @@ p {
margin-top: 140px;
max-width: 355px;
}

#date_range_form {
left: 20px;
max-width: 335px;
}
.date_range p {
display: none;
}
.date-select {
margin-right: 0px;
}

.video_title {
font-size: 14px !important;
Expand Down
2 changes: 1 addition & 1 deletion static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
// Removes the 'Apply Settings' button for Javascript users,
// since changing any of the elements causes the settings to apply
// automatically.
let resultsSave = document.querySelector(".results-save");
const resultsSave = document.querySelector(".results-save");
if (resultsSave != null) {
resultsSave.style.display = "none";
}
Expand Down
18 changes: 17 additions & 1 deletion templates/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
{% block body %}
<form class="results_settings" action="/save-settings" method="post">
<input type="hidden" name="past" value="{{ current_url }}"></input>
<select class="results-settings" name="engine" id="engine_select">
<option value="google" {% if settings.engine == 'google' %}selected{% endif %}>Google</option>
<option value="qwant" {% if settings.engine == 'qwant' %}selected{% endif %}>Qwant</option>
</select>
<select class="results-settings" name="safe" id="safeSearchSelect">
<option value="active" {% if settings.safe == 'active' %}selected{% endif %}>{{ lang_data.settings.safe_search }} {{ lang_data.settings.on }}</option>
<option value="" {% if settings.safe == '' %}selected{% endif %}>{{ lang_data.settings.safe_search }} {{ lang_data.settings.off }}</option>
Expand Down Expand Up @@ -58,7 +62,19 @@
</select>
<button class="results-save" type="submit">Apply settings</button>
</form>
<p class="engine">Engine: {{ engine }}</p>

{% if engine != "qwant" %}
<form action="/search" id="date_range_form">
<input type="hidden" name="q" value="{{ q }}">
<input type="hidden" name="t" value="{{ type }}">
<div class="date_range">
<p>Date range</p>
<input class="date-select" type="date" name="after" value="{{ after }}">
<input class="date-select" type="date" name="before" value="{{ before }}">
</div>
<input type="submit" class="results-settings" value="Submit">
</form>
{% endif %}
<p class="fetched">{{ lang_data.results.results }} {{ fetched }} {{ lang_data.results.seconds }}</p>
{% if check %}
<div class="check"><p>Did you mean: <a href="/search?q={{ check }}&t={{ type }}"><h3>{{ check }}</h3></a></p></div>
Expand Down
7 changes: 7 additions & 0 deletions templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@
<option value="active" {% if settings.new_tab == 'active' %}selected{% endif %}>New Tab On</option>
</select>
</div>
<div class="settings-row">
<p>Prefered search engine</p>
<select id="engine" name="engine">
<option value="google" {% if settings.engine == 'google' %}selected{% endif %}>Google</option>
<option value="qwant" {% if settings.engine == 'qwant' %}selected{% endif %}>Qwant</option>
</select>
</div>
<div class="settings-row">
<p>Method</p>
<select id="open-new-tab" name="method">
Expand Down