fixes #2454: Add a new popular posts page for view counts

This commit is contained in:
r888888888
2015-07-24 15:55:24 -07:00
parent cb57b08d54
commit cc6da3ff89
8 changed files with 145 additions and 3 deletions

View File

@@ -299,7 +299,13 @@
Danbooru.Post.initialize_title_for = function(post) {
var $post = $(post);
var $img = $post.find("img");
$img.attr("title", $post.attr("data-tags") + " user:" + $post.attr("data-uploader") + " rating:" + $post.data("rating") + " score:" + $post.data("score"));
var score = null;
if ($post.data("views")) {
score = " views:" + $post.data("views");
} else {
score = " score:" + $post.data("score");
}
$img.attr("title", $post.attr("data-tags") + " user:" + $post.attr("data-uploader") + " rating:" + $post.data("rating") + score);
}
Danbooru.Post.initialize_post_image_resize_links = function() {

View File

@@ -8,6 +8,12 @@ module Explore
respond_with(@posts)
end
def popular_view
@post_set = PostSets::PopularView.new(params[:date], params[:scale])
@posts = @post_set.posts
respond_with(@posts)
end
def intro
@presenter = IntroPresenter.new
render :layout => "blank"

View File

@@ -0,0 +1,73 @@
module PostSets
class PopularView < PostSets::Base
attr_reader :date, :scale
def initialize(date, scale)
@date = date.blank? ? Time.zone.now : Time.zone.parse(date)
@scale = scale || "Day"
end
def posts
@posts ||= begin
array = fetch_data.scan(/\S+/).in_groups_of(2)
post_ids = array.map(&:first)
posts = ::Post.where(id: post_ids).order(arbitrary_sql_order_clause(post_ids, "posts"))
posts.each.with_index do |post, i|
post.view_count = array[i][1].to_i
end
posts
end
end
def min_date
case scale
when "week"
date.beginning_of_week
when "month"
date.beginning_of_month
else
date
end
end
def max_date
case scale
when "week"
date.end_of_week
when "month"
date.end_of_month
else
date
end
end
def source
"popular_by_#{scale.downcase}"
end
def fetch_data
dates = date.strftime('%Y-%m-%d')
Cache.get("pv-#{scale}-#{dates}", 1.minute) do
url = URI.parse("#{Danbooru.config.report_server}/hits/#{source}?date=#{dates}")
response = []
Net::HTTP.start(url.host, url.port, :use_ssl => url.is_a?(URI::HTTPS)) do |http|
http.read_timeout = 1
http.request_get(url.request_uri) do |res|
if res.is_a?(Net::HTTPSuccess)
response = res.body
end
end
end
response
end
end
def presenter
::PostSetPresenters::Popular.new(self)
end
end
end

View File

@@ -7,7 +7,7 @@ class Post < ActiveRecord::Base
:has_embedded_notes => 0x0001
}
attr_accessor :old_tag_string, :old_parent_id, :old_source, :old_rating, :has_constraints, :disable_versioning
attr_accessor :old_tag_string, :old_parent_id, :old_source, :old_rating, :has_constraints, :disable_versioning, :view_count
after_destroy :delete_files
after_destroy :delete_remote_files
after_save :create_version

View File

@@ -79,6 +79,7 @@ class PostPresenter < Presenter
data-parent-id="#{post.parent_id}"
data-has-children="#{post.has_children?}"
data-score="#{post.score}"
data-views="#{post.view_count}"
data-fav-count="#{post.fav_count}"
data-pixiv-id="#{post.pixiv_id}"
data-md5="#{post.md5}"

View File

@@ -0,0 +1,38 @@
module PostSetPresenters
class PopularView < Popular
def initialize(post_set)
@post_set = post_set
end
def nav_links_for_scale(template, scale)
html = []
html << '<span class="period">'
html << template.link_to(
"&laquo;prev".html_safe,
template.popular_explore_post_views_path(
:date => prev_date_for_scale(scale),
:scale => scale.downcase
),
:rel => (link_rel_for_scale?(template, scale.downcase) ? "prev" : nil)
)
html << template.link_to(
scale,
template.popular_explore_post_views_path(
:date => date,
:scale => scale.downcase
),
:class => "desc"
)
html << template.link_to(
"next&raquo;".html_safe,
template.popular_explore_post_views_path(
:date => next_date_for_scale(scale),
:scale => scale.downcase
),
:rel => (link_rel_for_scale?(template, scale.downcase) ? "next" : nil)
)
html << '</span>'
html.join("\n").html_safe
end
end
end

View File

@@ -0,0 +1,17 @@
<div id="c-explore-posts">
<div id="a-index">
<h1>Popular (by views): <%= @post_set.presenter.range_text %></h1>
<%= @post_set.presenter.nav_links(self) %>
<%= render "posts/partials/common/inline_blacklist" %>
<%= @post_set.presenter.post_previews_html(self) %>
</div>
</div>
<%= render "posts/partials/common/secondary_links" %>
<% content_for(:page_title) do %>
Popular (by views) - <%= Danbooru.config.app_name %>
<% end %>

View File

@@ -42,9 +42,10 @@ Rails.application.routes.draw do
end
end
namespace :explore do
resources :posts, :only => [:popular, :hot] do
resources :posts do
collection do
get :popular
get :popular_view
get :hot
get :intro
end