refinements to search

This commit is contained in:
Kyle Drake 2024-12-13 13:14:35 -06:00
parent 44644cc7fa
commit 16fb3444fa
3 changed files with 82 additions and 44 deletions

View file

@ -119,16 +119,24 @@ def browse_sites_dataset
ds
end
get '/browse/search' do
if params[:q]
@title = 'Search Results'
def daily_search_max?
query_count = $redis_cache.get("search_query_count").to_i
if query_count >= $config['google_custom_search_query_limit']
halt 429, "Query limit reached. Please try again tomorrow."
$redis_cache.expire("search_query_count", 86400) if query_count == 0
return true if query_count >= $config['google_custom_search_query_limit']
false
end
get '/browse/search' do
@title = 'Site Search'
@daily_search_max_reached = daily_search_max?
if @daily_search_max_reached
params[:q] = nil
end
if !params[:q].blank?
$redis_cache.incr("search_query_count")
$redis_cache.expire("search_query_count", 86400) if query_count == 0
@start = params[:start].to_i
@start = 0 if @start < 0
@ -173,6 +181,7 @@ get '/browse/search' do
end
end
else
@items = nil
@total_results = 0
end

View file

@ -40,7 +40,6 @@
</fieldset>
</div>
<div class="col col-50 filter">
<form method="GET" action="browse">
<fieldset class="grouping">
<% unless is_education? %>
<label class="text-Label" for="tag"><span class="hide-on-mobile">Filter by </span>Tag:</label>
@ -145,15 +144,24 @@
<% unless is_education? %>
<%== erb :_browse_tags, layout: false %>
<div class="row content misc">
<h3>Text Search</h3>
<h3>Site Search</h3>
<% if !daily_search_max? %>
<form method="GET" action="/browse/search">
<fieldset>
<input name="q" type="text" placeholder="keywords" class="input-Area" autocapitalize="off" autocorrect="off" style="width: 50%">
<input class="btn btn-Action" type="submit" value="Search">
</fieldset>
</form>
<% else %>
<form id="searchForm" method="GET" action="https://duckduckgo.com" class="content" onsubmit="return addSiteToSearch()">
<input name="csrf_token" type="hidden" value="<%= csrf_token %>">
<fieldset>
<input id="searchQuery" name="q" type="text" placeholder="keywords" class="input-Area" autocapitalize="off" autocorrect="off" value="<%= flash[:username] %>" style="width: 50%">
<input class="btn btn-Action" type="submit" value="Search">
</fieldset>
</form>
<p>Search powered by <a href="https://duckduckgo.com/">Duck Duck Go</a></p>
<% end %>
</div>
<% end %>
</div>

View file

@ -65,22 +65,43 @@
color: #333;
line-height: 1.5;
}
</style>
<!--
<div class="header-Outro">
<div class="row content single-Col">
<h1>Neocities Search</h1>
</div>
</div>
<div class="content single-Col misc-page">
<h1>Search Results</h1>
-->
<% if @items.empty? %>
<div class="header-Outro with-columns browse-page">
<div class="row content">
<div class="col col-100">
<h1>Site Search</h1>
</div>
<form id="search_criteria" action="/browse/search" method="GET">
<div class="col col-50 filter">
<fieldset class="grouping">
<input name="q" type="text" class="input-Area" value="<%= params[:q] %>">
<input style="vertical-align: 5px; margin-left: 4px;" type="submit" class="btn-Action" value="Search">
</fieldset>
</div>
</div>
</form>
</div>
<div class="content single-Col misc-page">
<% if @daily_search_max_reached %>
Search temporarily unavailable, please try again tomorrow.
<% elsif @items == [] %>
No results.
<% else %>
<% elsif !@items.nil? %>
<div class="search-results">
<% @items.each do |item| %>
<div class="result-item">