Only show next page when there are more results

This commit is contained in:
Omar Roth 2018-08-25 17:18:43 -05:00
parent 78fcf579a7
commit 77b12b6249
4 changed files with 7 additions and 5 deletions

View file

@ -425,7 +425,7 @@ get "/search" do |env|
page ||= 1 page ||= 1
search_params = build_search_params(sort_by: "relevance", content_type: "video") search_params = build_search_params(sort_by: "relevance", content_type: "video")
videos = search(query, page, search_params) count, videos = search(query, page, search_params).as(Tuple)
templated "search" templated "search"
end end
@ -2365,7 +2365,7 @@ get "/api/v1/search" do |env|
response = JSON.build do |json| response = JSON.build do |json|
json.array do json.array do
search_results = search(query, page, search_params) count, search_results = search(query, page, search_params).as(Tuple)
search_results.each do |video| search_results.each do |video|
json.object do json.object do
json.field "title", video.title json.field "title", video.title

View file

@ -2,13 +2,13 @@ def crawl_videos(db)
ids = Deque(String).new ids = Deque(String).new
random = Random.new random = Random.new
search(random.base64(3)).each do |video| search(random.base64(3)).as(Tuple)[1].each do |video|
ids << video.id ids << video.id
end end
loop do loop do
if ids.empty? if ids.empty?
search(random.base64(3)).each do |video| search(random.base64(3)).as(Tuple)[1].each do |video|
ids << video.id ids << video.id
end end
end end

View file

@ -23,7 +23,7 @@ def search(query, page = 1, search_params = build_search_params(content_type: "v
nodeset = html.xpath_nodes(%q(//ol[@class="item-section"]/li)) nodeset = html.xpath_nodes(%q(//ol[@class="item-section"]/li))
videos = extract_videos(nodeset) videos = extract_videos(nodeset)
return videos return {nodeset.size, videos}
end end
def build_search_params(sort_by = "relevance", date : String = "", content_type : String = "", duration : String = "", features : Array(String) = [] of String) def build_search_params(sort_by = "relevance", date : String = "", content_type : String = "", duration : String = "", features : Array(String) = [] of String)

View file

@ -18,6 +18,8 @@
</div> </div>
<div class="pure-u-1 pure-u-md-3-5"></div> <div class="pure-u-1 pure-u-md-3-5"></div>
<div style="text-align:right;" class="pure-u-1 pure-u-md-1-5"> <div style="text-align:right;" class="pure-u-1 pure-u-md-1-5">
<% if count == 20 %>
<a href="/search?q=<%= query %>&page=<%= page + 1 %>">Next page</a> <a href="/search?q=<%= query %>&page=<%= page + 1 %>">Next page</a>
<% end %>
</div> </div>
</div> </div>