Module: FetchUtil::Regulatory::Robots

Included in:
FetchUtil::Regulatory
Defined in:
lib/fetch_util/regulatory/robots.rb

Constant Summary collapse

ROBOT_DIRECTIVES =
%w[
  all
  follow
  index
  indexifembedded
  max-image-preview
  max-snippet
  max-video-preview
  noai
  noarchive
  nocache
  nofollow
  noimageai
  noimageindex
  noindex
  none
  nosnippet
  notranslate
  unavailable_after
].freeze

Instance Method Summary collapse

Instance Method Details

#extract_robots_source_signals(body) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fetch_util/regulatory/robots.rb', line 39

def extract_robots_source_signals(body)
  signals = {
    "robotstxt" => [],
    "contentsignal" => [],
    "contentusagerobots" => []
  }
  user_agents = []
  in_rules = false

  body.to_s.gsub("\r\n", "\n").gsub("\r", "\n").each_line do |line|
    content = line.sub(/\s*#.*\z/, "").strip
    next if content.empty?

    field, value = content.split(":", 2)
    next unless value

    field = field.strip.downcase
    value = value.strip

    case field
    when "user-agent"
      user_agents = [] if in_rules
      in_rules = false
      user_agents << value unless value.empty?
    when "allow", "disallow"
      next if user_agents.empty?

      in_rules = true
      user_agents.each do |user_agent|
        signals["robotstxt"] << robot_signal(field, user_agent, value)
      end
    when "content-signal"
      next if user_agents.empty?

      in_rules = true
      user_agents.each do |user_agent|
        signals["contentsignal"].concat(extract_content_signal_signals(value, user_agent: user_agent))
      end
    when "content-usage"
      next if user_agents.empty?

      in_rules = true
      user_agents.each do |user_agent|
        signals["contentusagerobots"].concat(extract_content_usage_robot_signals(value, user_agent: user_agent))
      end
    end
  end

  signals
end

#robots_record(requested_uri) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fetch_util/regulatory/robots.rb', line 26

def robots_record(requested_uri)
  fetch_record("robotstxt:#{origin_key(requested_uri)}", robots_uri(requested_uri), fallback: empty_robots_record) do |body|
    payload = extract_robots_source_signals(body)
    {
      "signals" => {
        "robotstxt" => sort_robot_signals(payload["robotstxt"]),
        "contentsignal" => sort_usage_preference_signals(payload["contentsignal"]),
        "contentusagerobots" => sort_usage_preference_signals(payload["contentusagerobots"])
      }
    }
  end
end