quick n dirty neocities search results w/screenshots

This commit is contained in:
Kyle Drake 2024-12-12 14:05:38 -06:00
parent 3767725759
commit aae94f2078
3 changed files with 180 additions and 1 deletions

View file

@ -118,3 +118,58 @@ def browse_sites_dataset
ds
end
get '/browse/search' do
if params[:q]
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."
end
$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
@resp = JSON.parse HTTP.get('https://www.googleapis.com/customsearch/v1', params: {
key: $config['google_custom_search_key'],
cx: $config['google_custom_search_cx'],
safe: 'active',
start: @start,
q: Rack::Utils.escape(params[:q])
})
@items = []
if @total_results != 0 && @resp['error'].nil? && @resp['searchInformation']['totalResults'] != "0"
@total_results = @resp['searchInformation']['totalResults'].to_i
@resp['items'].each do |item|
link = Addressable::URI.parse(item['link'])
next if link.host == 'neocities.org'
username = link.host.split('.').first
site = Site[username: username]
next if site.nil? || site.is_deleted || site.is_nsfw
if link.path[-1] == '/'
link.path << 'index.html'
else
['.html', '.htm'].each do |ext|
if site.screenshot_exists?(link.path + ext, '540x405')
link.path += ext
break
end
end
end
item['screenshot_url'] = site.screenshot_url(link.path, '540x405')
@items << item
end
end
else
@total_results = 0
end
erb :'search'
end

View file

@ -28,6 +28,9 @@ development:
screenshot_urls:
- http://screenshots:derp@127.0.0.1:12345
stop_forum_spam_api_key: testkey
google_custom_search_key: herpderp
google_custom_search_cx: herpderp
google_custom_search_query_limit: 69
test:
database: 'postgres://localhost/neocities_test'
database_pool: 1
@ -61,4 +64,7 @@ test:
twilio_auth_token: derpderpderp
twilio_service_sid: VADERPDERPDERP
minfraud_account_id: 696969420
minfraud_license_key: DERPDERPDERP
minfraud_license_key: DERPDERPDERP
google_custom_search_key: herpderp
google_custom_search_cx: herpderp
google_custom_search_query_limit: 69

118
views/search.erb Normal file
View file

@ -0,0 +1,118 @@
<style>
.search-results {
background: #ffffff;
}
.result-item {
display: flex;
border-bottom: 1px solid #ddd;
padding: 10px 0;
}
.result-item:last-child {
border-bottom: none;
}
.screenshot {
width: 400px;
flex-shrink: 0;
overflow: hidden;
border: 1px solid #ddd;
margin-right: 15px;
}
.screenshot img {
width: 100%;
height: 100%;
object-fit: cover;
}
.result-details {
flex-grow: 1;
}
.result-title {
font-size: 1.2em;
margin: 0;
}
.result-title a {
color: #e93250;
text-decoration: none;
}
.result-title a:hover {
text-decoration: underline;
}
.result-url {
font-size: 0.9em;
color: #777;
margin-bottom: 5px;
}
.result-url a {
color: #e93250;
text-decoration: none;
}
.result-url a:hover {
text-decoration: underline;
}
.result-snippet {
font-size: 0.9em;
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? %>
No results.
<% else %>
<div class="search-results">
<% @items.each do |item| %>
<div class="result-item">
<div class="screenshot">
<a href="<%= item['link'] %>"><img src="<%= item['screenshot_url'] %>" alt="<%= item['title'] %>"></a>
</div>
<div class="result-details">
<h3 class="result-title">
<a href="<%= item['link'] %>"><%= item['title'] %></a>
</h3>
<div class="result-url">
<a href="<%= item['link'] %>"><%= item['link'] %></a>
</div>
<p class="result-snippet">
<%== item['htmlSnippet'] %>
</p>
</div>
</div>
<% end %>
</div>
<div class="txt-Center">
<h2>
<% if @start > 0 %>
<a href="?q=<%= Rack::Utils.escape params[:q] %>&start=<%= [@start-10, 0].max %>"><i class="fa fa-arrow-left arrow">&nbsp;&nbsp;</i></a>
<% end %>
<% if @total_results > @start+10 %>
<a href="?q=<%= Rack::Utils.escape params[:q] %>&start=<%= @start+10 %>"><i class="fa fa-arrow-right arrow"></i></a>
<% end %>
</h2>
</div>
<% end %>
</div>