Skip to content

Commit

Permalink
v2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Feb 9, 2024
1 parent 396f7c2 commit 5edd835
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 114 deletions.
1 change: 1 addition & 0 deletions lib/miteru.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
# Concerns
require "miteru/concerns/database_connectable"
require "miteru/concerns/error_unwrappable"
require "miteru/concerns/url_truncatable"

# Core classes
require "miteru/service"
Expand Down
16 changes: 4 additions & 12 deletions lib/miteru/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,22 @@ def initialize(url)
# @param [Integer. nil] ex
#
def set(key, value, ex:)
value = redis.set("#{prefix}:#{key}", value, ex:)
Miteru.logger.info("Cache:#{key} is set.") if verbose?
value = redis.set("#{cache_prefix}:#{key}", value, ex:)
logger.info("Cache:#{key} is set.") if verbose?
value
end

#
# @param [String] key
#
def cached?(key)
value = redis.exists?("#{prefix}:#{key}")
Miteru.logger.info("Cache:#{key} found.") if verbose?
value = redis.exists?("#{cache_prefix}:#{key}")
logger.info("Cache:#{key} found.") if verbose?
value
end

private

def verbose?
Miteru.config.verbose
end

def prefix
Miteru.config.cache_prefix
end

#
# @return [Redis]
#
Expand Down
24 changes: 24 additions & 0 deletions lib/miteru/concerns/url_truncatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Miteru
module Concerns
module UrlTruncatable
extend ActiveSupport::Concern

def decoded_url
@decoded_url ||= URI.decode_www_form_component(url)
end

#
# @return [String]
#
def truncated_url
@truncated_url ||= decoded_url.truncate(64)
end

def defanged_truncated_url
@defanged_truncated_url ||= truncated_url.to_s.gsub(".", "[.]")
end
end
end
end
43 changes: 12 additions & 31 deletions lib/miteru/crawler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@ def call(website)
Try[OpenSSL::SSL::SSLError, ::HTTP::Error, Addressable::URI::InvalidURIError] do
info = "Website:#{website.info}."
info = info.colorize(:red) if website.kits?
Miteru.logger.info(info)
logger.info(info)

website.kits.each do |kit|
downloader = Downloader.new(kit)
result = downloader.result

unless result.success?
Miteru.logger.warn("Kit:#{kit.truncated_url} failed to download - #{result.failure}.")
logger.warn("Kit:#{kit.truncated_url} failed to download - #{result.failure}.")
next
end

destination = result.value!
Miteru.logger.info("Kit:#{kit.truncated_url} downloaded as #{destination}.")
logger.info("Kit:#{kit.truncated_url} downloaded as #{destination}.")
# Remove downloaded file if auto_download is not allowed
FileUtils.rm(destination, force: true) unless auto_download?
# Notify the website
notify website
# Notify the kit
notify(kit)
end

# Cache the website
Expand All @@ -37,39 +35,22 @@ def call(website)

private

def cache?
Miteru.cache?
end

def cache
Miteru.cache
end

def cache_ex
Miteru.config.cache_ex
end

def auto_download?
Miteru.config.auto_download
end

#
# @param [Miteru::Website] website
# @param [Miteru::Kit] kit
#
def notify(website)
def notify(kit)
notifiers.each do |notifier|
result = notifier.result(website)
result = notifier.result(kit)
if result.success?
Miteru.logger.info("Notifier:#{notifier.name} succeeded.")
logger.info("Notifier:#{notifier.name} succeeded.")
else
Miteru.logger.warn("Notifier:#{notifier.name} failed - #{result.failure}.")
logger.warn("Notifier:#{notifier.name} failed - #{result.failure}.")
end
end
end

#
# @return [Array<Miteru::Notifiers::Base>]
#
private

def notifiers
@notifiers ||= Miteru.notifiers.map(&:new)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/miteru/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def call

# Record a kit in DB
Record.create_by_kit_and_hash(kit, sha256: sha256(destination))
Miteru.logger.info "Download #{kit.url} as #{destination}"
logger.info "Download #{kit.url} as #{destination}"

destination
end
Expand Down
13 changes: 2 additions & 11 deletions lib/miteru/kit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module Miteru
class Kit < Service
include Concerns::UrlTruncatable

# @return [String]
attr_reader :url

Expand Down Expand Up @@ -85,17 +87,6 @@ def hostname
@hostname ||= URI(url).hostname
end

def decoded_url
@decoded_url ||= URI.decode_www_form_component(url)
end

#
# @return [String]
#
def truncated_url
url.truncate(64)
end

private

def filename_to_download
Expand Down
2 changes: 1 addition & 1 deletion lib/miteru/notifiers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Miteru
module Notifiers
class Base < Service
def call(website)
def call(kit)
raise NotImplementedError
end

Expand Down
8 changes: 4 additions & 4 deletions lib/miteru/notifiers/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ class Slack < Base
#
# Notifiy to Slack
#
# @param [Miteru::Website] website
# @param [Miteru::Kit] kit
#
def call(website)
def call(kit)
return unless callable?

attachment = SlackAttachment.new(website.url)
notifier.post(text: website.info, attachments: attachment.to_a) if website.kits?
attachment = SlackAttachment.new(kit.url)
notifier.post(text: kit.defanged_truncated_url, attachments: attachment.to_a)
end

def callable?
Expand Down
6 changes: 3 additions & 3 deletions lib/miteru/notifiers/urlscan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ module Miteru
module Notifiers
class UrlScan < Base
#
# @param [Miteru::Website] website
# @param [Miteru::Kit] kit
#
def call(website)
def call(kit)
return unless callable?

website.kits.each { |kit| submit(kit.url, source: website.source) }
submit(kit.decoded_url, source: kit.source)
end

def callable?
Expand Down
74 changes: 35 additions & 39 deletions lib/miteru/orchestrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@
module Miteru
class Orchestrator < Service
def call
Miteru.logger.info("#{non_cached_websites.length} websites loaded in total.") if verbose?
logger.info("#{non_cached_websites.length} websites loaded in total.") if verbose?

if Miteru.sidekiq?
non_cached_websites.each do |website|
Jobs::CrawleJob.perform_async(website.url, website.source)
Miteru.logger.info("Website:#{website.truncated_url} crawler job queued.") if verbose?
end
if sidekiq?
sidekiq_call
else
Miteru.logger.info("Use #{threads} thread(s).") if verbose?
Parallel.each(non_cached_websites, in_threads: threads) do |website|
Miteru.logger.info("Website:#{website.truncated_url} crawling started.") if verbose?
parallel_call
end
end

result = Crawler.result(website)
if result.success?
Miteru.logger.info("Crawler:#{website.truncated_url} succeeded.")
else
Miteru.logger.info("Crawler:#{website.truncated_url} failed - #{result.failure}.")
end
def sidekiq_call
non_cached_websites.each do |website|
Jobs::CrawleJob.perform_async(website.url, website.source)
logger.info("Website:#{website.truncated_url} crawler job queued.") if verbose?
end
end

def parallel_call
logger.info("Use #{threads} thread(s).") if verbose?
Parallel.each(non_cached_websites, in_threads: threads) do |website|
logger.info("Website:#{website.truncated_url} crawling started.") if verbose?
result = Crawler.result(website)
if result.success?
logger.info("Crawler:#{website.truncated_url} succeeded.")
else
logger.info("Crawler:#{website.truncated_url} failed - #{result.failure}.")
end
end
end
Expand All @@ -34,44 +41,33 @@ def websites
result = feed.result
if result.success?
websites = result.value!
Miteru.logger.info("Feed:#{feed.source} has #{websites.length} websites.") if verbose?
logger.info("Feed:#{feed.source} has #{websites.length} websites.") if verbose?
out << websites
else
Miteru.logger.warn("Feed:#{feed.source} failed - #{result.failure}")
logger.warn("Feed:#{feed.source} failed - #{result.failure}")
end
end
end.flatten.uniq(&:url)
end

#
# @return [Array<Miteru::Website>]
#
def non_cached_websites
return websites unless cache?

websites.reject { |website| cache.cached?(website.url) }
@non_cached_websites ||= [].tap do |out|
out << if cache?
websites.reject { |website| cache.cached?(website.url) }
else
websites
end
end.flatten.uniq(&:url)
end

#
# @return [Array<Miteru::Feeds::Base>]
#
def feeds
Miteru.feeds.map(&:new)
end

private

def cache?
Miteru.cache?
end

def cache
Miteru.cache
end

def threads
Miteru.config.threads
end

def verbose?
Miteru.config.verbose
@feeds ||= Miteru.feeds.map(&:new)
end
end
end
44 changes: 44 additions & 0 deletions lib/miteru/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,49 @@ def result(...)
new.result(...)
end
end

private

def auto_download?
Miteru.config.auto_download
end

#
# @return [SemanticLogger]
#
def logger
Miteru.logger
end

def cache?
Miteru.cache?
end

def sidekiq?
Miteru.sidekiq?
end

#
# @return [Miteru::Cache]
#
def cache
Miteru.cache
end

def threads
Miteru.config.threads
end

def verbose?
Miteru.config.verbose
end

def cache_prefix
Miteru.config.cache_prefix
end

def cache_ex
Miteru.config.cache_ex
end
end
end
2 changes: 1 addition & 1 deletion lib/miteru/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Miteru
VERSION = "2.2.0"
VERSION = "2.2.1"
end
Loading

0 comments on commit 5edd835

Please sign in to comment.