Refactor Reportbooru API clients.

* Combine MissedSearchService, PostViewCountService, and
  PopularSearchService into single ReportbooruService class.
* Use Danbooru::Http for these services instead of HTTParty.
This commit is contained in:
evazion
2020-06-14 00:24:15 -05:00
parent 1846133cd6
commit a4df18e650
17 changed files with 102 additions and 143 deletions

View File

@@ -1,29 +0,0 @@
# queries reportbooru to find missed post searches
class MissedSearchService
def self.enabled?
Danbooru.config.reportbooru_server.present?
end
def initialize
if !MissedSearchService.enabled?
raise NotImplementedError.new("the Reportbooru service isn't configured. Missed searches are not available.")
end
end
def each_search(&block)
fetch_data.scan(/(.+?) (\d+)\.0\n/).each(&block)
end
def fetch_data
Cache.get("ms", 1.minute) do
url = URI.parse("#{Danbooru.config.reportbooru_server}/missed_searches")
response = HTTParty.get(url, Danbooru.config.httparty_options.reverse_merge(timeout: 6))
if response.success?
response = response.body
else
response = ""
end
response.force_encoding("utf-8")
end
end
end

View File

@@ -1,61 +0,0 @@
# queries reportbooru to find popular post searches
class PopularSearchService
attr_reader :date
def self.enabled?
Danbooru.config.reportbooru_server.present?
end
def initialize(date)
if !PopularSearchService.enabled?
raise NotImplementedError.new("the Reportbooru service isn't configured. Popular searches are not available.")
end
@date = date
end
def each_search(limit = 100, &block)
JSON.parse(fetch_data.to_s).slice(0, limit).each(&block)
end
def tags
JSON.parse(fetch_data.to_s).map {|x| x[0]}
end
def fetch_data
return [] unless self.class.enabled?
dates = date.strftime("%Y-%m-%d")
data = Cache.get("ps-day-#{dates}", 1.minute) do
url = "#{Danbooru.config.reportbooru_server}/post_searches/rank?date=#{dates}"
response = HTTParty.get(url, Danbooru.config.httparty_options.reverse_merge(timeout: 3))
if response.success?
response = response.body
else
response = "[]"
end
response
end.to_s.force_encoding("utf-8")
if data.blank? || data == "[]"
dates = date.yesterday.strftime("%Y-%m-%d")
data = Cache.get("ps-day-#{dates}", 1.minute) do
url = "#{Danbooru.config.reportbooru_server}/post_searches/rank?date=#{dates}"
response = HTTParty.get(url, Danbooru.config.httparty_options.reverse_merge(timeout: 3))
if response.success?
response = response.body
else
response = "[]"
end
response
end.to_s.force_encoding("utf-8")
end
data
rescue StandardError => e
DanbooruLogger.log(e)
return []
end
end

View File

@@ -169,8 +169,8 @@ module PostSets
end
def popular_tags
if PopularSearchService.enabled?
PopularSearchService.new(Date.today).tags
if reportbooru_service.enabled?
reportbooru_service.popular_searches(Date.today, limit: MAX_SIDEBAR_TAGS).map(&:first)
else
frequent_tags
end
@@ -199,6 +199,10 @@ module PostSets
def tag_list_html(**options)
tag_set_presenter.tag_list_html(name_only: query.is_metatag?(:search), **options)
end
def reportbooru_service
@reportbooru_service ||= ReportbooruService.new
end
end
end
end

View File

@@ -1,25 +0,0 @@
class PostViewCountService
attr_reader :http, :reportbooru_server
def initialize(http: Danbooru::Http.new, reportbooru_server: Danbooru.config.reportbooru_server)
@reportbooru_server = reportbooru_server
@http = http
end
def enabled?
reportbooru_server.present?
end
def fetch_rank(date = Date.today)
raise NotImplementedError, "Reportbooru not configured, post views not available." unless enabled?
response = http.get("#{reportbooru_server}/post_views/rank?date=#{date}")
return [] if response.status != 200
JSON.parse(response.to_s)
end
def popular_posts(date = Date.today)
ranking = fetch_rank(date)
ranking.slice(0, 50).map {|x| Post.find(x[0])}
end
end

View File

@@ -0,0 +1,49 @@
class ReportbooruService
attr_reader :http, :reportbooru_server
def initialize(http: Danbooru::Http.new, reportbooru_server: Danbooru.config.reportbooru_server)
@reportbooru_server = reportbooru_server
@http = http
end
def enabled?
reportbooru_server.present?
end
def missed_search_rankings(expires_in: 1.minutes)
raise NotImplementedError, "Reportbooru not configured, missed searches not available." unless enabled?
response = http.cache(expires_in).get("#{reportbooru_server}/missed_searches")
return [] if response.status != 200
body = response.to_s.force_encoding("utf-8")
body.lines.map(&:split).map { [_1, _2.to_i] }
end
def post_search_rankings(date = Date.today, expires_in: 1.minutes)
raise NotImplementedError, "Reportbooru not configured, popular searches not available." unless enabled?
response = http.cache(expires_in).get("#{reportbooru_server}/post_searches/rank?date=#{date}")
return [] if response.status != 200
JSON.parse(response.to_s.force_encoding("utf-8"))
end
def post_view_rankings(date = Date.today, expires_in: 1.minutes)
raise NotImplementedError, "Reportbooru not configured, post views not available." unless enabled?
response = http.get("#{reportbooru_server}/post_views/rank?date=#{date}")
return [] if response.status != 200
JSON.parse(response.to_s.force_encoding("utf-8"))
end
def popular_searches(date = Date.today, limit: 100)
ranking = post_search_rankings(date)
ranking = post_search_rankings(date.yesterday) if ranking.blank?
ranking.take(limit).map(&:first)
end
def popular_posts(date = Date.today, limit: 100)
ranking = post_view_rankings(date)
ranking.take(limit).map { |x| Post.find(x[0]) }
end
end