diff --git a/app/browse.rb b/app/browse.rb index b7d95c95..12dd53d2 100644 --- a/app/browse.rb +++ b/app/browse.rb @@ -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 \ No newline at end of file diff --git a/config.yml.template b/config.yml.template index 260c0397..71e211d9 100644 --- a/config.yml.template +++ b/config.yml.template @@ -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 \ No newline at end of file + minfraud_license_key: DERPDERPDERP + google_custom_search_key: herpderp + google_custom_search_cx: herpderp + google_custom_search_query_limit: 69 \ No newline at end of file diff --git a/views/search.erb b/views/search.erb new file mode 100644 index 00000000..f5603380 --- /dev/null +++ b/views/search.erb @@ -0,0 +1,118 @@ + + +
+
+

Neocities Search

+
+
+ +
+

Search Results

+ + <% if @items.empty? %> + No results. + <% else %> +
+ <% @items.each do |item| %> +
+
+ <%= item['title'] %> +
+
+

+ <%= item['title'] %> +

+ + + +

+ <%== item['htmlSnippet'] %> +

+
+
+ <% end %> +
+ +
+

+ <% if @start > 0 %> +    + <% end %> + <% if @total_results > @start+10 %> + + <% end %> +

+
+ <% end %> +