sitemap-parser 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8eddc8204f36a00114c47cbee1a9e1bf884fda408e51383e3af615c7811b1a25
4
- data.tar.gz: 10fde984cc652d03edf863ee8c6cc03a531b6cadaa1e37de49b2d2095d3d4f46
3
+ metadata.gz: 4b2ca099a396692ae3eb5cfac8b670f422224646f6c87e612daa88ede8384de3
4
+ data.tar.gz: 3c5a041670278d3c2f41fc5fae6e1709eb98b340752b99be8e08f92d2662d01a
5
5
  SHA512:
6
- metadata.gz: ec5cad59821bdc90a202fca1ac7b514c34be55c4addd3948ba3b9fe86d68170dbb773836d8eb094f1b7625ee4112ea9903693edabb395b2951fa638a98d3083b
7
- data.tar.gz: d2ffb35d82995f2e031afb304350f88a726db081937e9b955b689f774bf3c7e5a4ae8da132c95d6ef2f6edad5326a95a079194c048f01ae4e91d7878d0c51066
6
+ metadata.gz: 43ba8d994436b2a0ad920994ff0ca7b4908ef1879915f0f4bb9d02a7cb96f404338e1311768476c5f7da362959631c0c98a58d409c75c5fb580acfed98cf6ee5
7
+ data.tar.gz: b21e34028ba28bfbae37250cbf8ea90fd675d5f1f21366e4e78eca89128eda62f7dfa06a0487c2fd0992ed68da884b5669459c9f0c73f3c8f227a80a01429737
data/CHANGELOG.md ADDED
@@ -0,0 +1,36 @@
1
+ # Changelog
2
+
3
+ ## 0.6.0
4
+
5
+ ### Breaking changes
6
+
7
+ - Ruby 3.0 or newer is required.
8
+ - `SitemapParser::DEFLATE_TYPE_REGEX` is gone. Gzip is detected from the file's contents instead.
9
+ - A remote sitemap index that lists a local path, or anything else that isn't an http(s) URL, now raises instead of reading it.
10
+ - A sitemap listed more than once in an index tree is parsed once, so its URLs no longer appear twice in `urls` or `to_a`.
11
+
12
+ ### New features
13
+
14
+ - Recurse through sitemap indexes nested more than one level deep. Before, `recurse: true` stopped after the first level (#25). Thanks to [@etagwerker](https://github.com/etagwerker).
15
+ - Pass Typhoeus options, such as `userpwd`, along to nested sitemaps when recursing (#28). Thanks to [@prasannaboga](https://github.com/prasannaboga).
16
+ - Send a `Sitemap-Parser` User-Agent by default, and include the URL in the error when a request fails (#29). Thanks to [@tamaloa](https://github.com/tamaloa).
17
+ - Set custom request headers with the `headers` option (#30). Thanks to [@sscherban](https://github.com/sscherban).
18
+ - Read local sitemaps with any file name, not just `sitemap.xml` (#30). Thanks to [@sscherban](https://github.com/sscherban).
19
+ - Read gzipped sitemaps whatever their `Content-Type`, and read local `.gz` files too. Gzip is now detected from the file's contents.
20
+
21
+ ### Bug fixes
22
+
23
+ - Keep custom headers when no User-Agent is given, instead of replacing them with the default one (#34). Thanks to [@aksshatt](https://github.com/aksshatt).
24
+ - Treat a User-Agent header given in any case, such as `user-agent`, as the caller's own, and don't add a second one.
25
+ - Return `[]` for an empty sitemap instead of raising (#30). Thanks to [@sscherban](https://github.com/sscherban).
26
+ - Strip whitespace around `<loc>` values.
27
+ - Stop recursing when a sitemap index lists itself or an ancestor, which used to loop until the stack overflowed. A sitemap listed more than once is fetched only once.
28
+ - Refuse to read local files listed in a remote sitemap index. Before, a remote index could make the parser read any file on disk.
29
+ - Raise a clear error when a sitemap isn't a URL or an existing file, or when a `<url>` has no `<loc>`, instead of a `NoMethodError`.
30
+ - Don't try to gunzip plain XML served as `application/octet-stream`.
31
+
32
+ ### Maintenance
33
+
34
+ - Move CI to GitHub Actions, and loosen the nokogiri constraint to `>= 1.6` (#26). Thanks to [@petergoldstein](https://github.com/petergoldstein).
35
+ - Test on Ruby 3.0, the oldest the gem allows, and every supported release, and upgrade RuboCop to 1.x.
36
+ - Publish releases to RubyGems from GitHub Actions through trusted publishing.
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 GitHub, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Sitemap Parser
2
+
3
+ *Ruby Gem to parse sitemaps.org compliant sitemaps*
4
+
5
+ [![CI](https://github.com/benbalter/sitemap-parser/actions/workflows/ci.yml/badge.svg)](https://github.com/benbalter/sitemap-parser/actions/workflows/ci.yml) [![Gem Version](https://badge.fury.io/rb/sitemap-parser.svg)](https://badge.fury.io/rb/sitemap-parser)
6
+
7
+ ## Usage
8
+
9
+ Create a new instance of the Parser with a URL or a path to a local file. Gzipped sitemaps (`sitemap.xml.gz`) work too.
10
+
11
+ ```ruby
12
+ sitemap = SitemapParser.new "https://ben.balter.com/sitemap.xml"
13
+ sitemap = SitemapParser.new "path/to/sitemap.xml"
14
+ ```
15
+
16
+ Extract the URLs of the sitemap
17
+
18
+ ```ruby
19
+ sitemap.urls # => Array of Nokogiri XML::Node objects
20
+ sitemap.to_a # => Array of url strings
21
+ ```
22
+
23
+ ## Options
24
+
25
+ ### Recurse nested sitemaps
26
+
27
+ By default, a sitemap index returns no URLs. Pass `recurse: true` to follow the sitemaps it lists, however deeply they're nested. Each sitemap is fetched once, even if it's listed more than once.
28
+
29
+ ```ruby
30
+ sitemap = SitemapParser.new('https://ben.balter.com/sitemap.xml', recurse: true)
31
+ ```
32
+
33
+ To only extract the URLs that match a pattern, pass a regex. It's matched against every URL, in nested sitemaps and pages alike.
34
+
35
+ ```ruby
36
+ sitemap = SitemapParser.new('https://ben.balter.com/sitemap.xml', recurse: true, url_regex: /sitemapregex/)
37
+ ```
38
+
39
+ ### Request headers
40
+
41
+ Requests send a `Sitemap-Parser` User-Agent unless you set your own. Any other headers you pass are kept.
42
+
43
+ ```ruby
44
+ sitemap = SitemapParser.new('https://ben.balter.com/sitemap.xml', headers: { 'User-Agent' => 'MyBot/1.0', 'Accept-Language' => 'en' })
45
+ ```
46
+
47
+ ### Typhoeus options
48
+
49
+ Any other option is passed to [Typhoeus](https://github.com/typhoeus/typhoeus), for every sitemap fetched.
50
+
51
+ ```ruby
52
+ sitemap = SitemapParser.new('https://ben.balter.com/sitemap.xml', userpwd: "username:password")
53
+ ```
54
+
55
+ ## Releasing
56
+
57
+ 1. Open a pull request that bumps `lib/sitemap-parser/version.rb` and adds a `## X.Y.Z` section to [`CHANGELOG.md`](CHANGELOG.md).
58
+ 2. Merge it once CI passes.
59
+ 3. Push a signed tag for the version, e.g. `git tag -s v0.6.0 -m v0.6.0 && git push origin v0.6.0`.
60
+
61
+ The [release workflow](.github/workflows/release.yml) then publishes the gem to RubyGems through [trusted publishing](https://guides.rubygems.org/trusted-publishing/) and creates the GitHub release from the changelog.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SitemapParser
4
+ VERSION = '0.6.0'
5
+ end
@@ -1,31 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'nokogiri'
4
+ require 'set'
4
5
  require 'typhoeus'
5
6
  require 'zlib'
6
7
  require_relative 'sitemap-parser/version'
7
8
 
9
+ # Parses a sitemaps.org sitemap or sitemap index, from a URL or a local file,
10
+ # into its <url> nodes (#urls) or their locations (#to_a).
8
11
  class SitemapParser
12
+ attr_accessor :url, :options
13
+
14
+ DEFAULT_OPTIONS = {
15
+ followlocation: true,
16
+ recurse: false,
17
+ url_regex: nil
18
+ }.freeze
19
+
20
+ # The first two bytes of every gzip stream (RFC 1952).
21
+ GZIP_MAGIC = [0x1f, 0x8b].freeze
22
+
9
23
  def initialize(url, opts = {})
10
24
  @url = url
11
- @options = { followlocation: true, recurse: false, url_regex: nil }.merge(opts)
25
+ @options = DEFAULT_OPTIONS.merge(opts)
12
26
  end
13
27
 
14
28
  def raw_sitemap
15
- @raw_sitemap ||= begin
16
- if /\Ahttp/i.match?(@url)
17
- request_options = @options.dup.tap { |opts| opts.delete(:recurse); opts.delete(:url_regex) }
18
- request = Typhoeus::Request.new(@url, request_options)
19
- request.on_complete do |response|
20
- raise "HTTP request to #{@url} failed" unless response.success?
21
-
22
- return inflate_body_if_needed(response)
23
- end
24
- request.run
25
- elsif File.exist?(@url) && @url =~ %r{[\\/]sitemap\.xml\Z}i
26
- File.open(@url, &:read)
27
- end
28
- end
29
+ @raw_sitemap ||= fetch_remote_sitemap || read_local_sitemap ||
30
+ raise("Could not read sitemap at #{url}: not an http(s) URL or an existing file")
29
31
  end
30
32
 
31
33
  def sitemap
@@ -33,45 +35,117 @@ class SitemapParser
33
35
  end
34
36
 
35
37
  def urls
36
- if sitemap.at('urlset')
37
- filter_sitemap_urls(sitemap.at('urlset').search('url'))
38
- elsif sitemap.at('sitemapindex')
39
- found_urls = []
40
- if @options[:recurse]
41
- urls = sitemap.at('sitemapindex').search('sitemap')
42
- filter_sitemap_urls(urls).each do |sitemap|
43
- child_sitemap_location = sitemap.at('loc').content
44
- found_urls << self.class.new(child_sitemap_location, recurse: false).urls
45
- end
46
- end
47
- found_urls.flatten
48
- else
49
- raise 'Malformed sitemap, no urlset'
50
- end
38
+ @urls ||= if urlset
39
+ filter_sitemap_urls(urlset.search('url'))
40
+ elsif sitemapindex
41
+ options[:recurse] ? parse_sitemap_index : []
42
+ elsif raw_sitemap.strip.empty?
43
+ []
44
+ else
45
+ raise 'Malformed sitemap, no urlset or sitemapindex'
46
+ end
51
47
  end
52
48
 
53
49
  def to_a
54
50
  urls.map { |url| url.at('loc').content }
55
- rescue NoMethodError
56
- raise 'Malformed sitemap, url without loc'
57
51
  end
58
52
 
53
+ protected
54
+
55
+ # Sitemap locations already fetched while recursing, shared by every parser
56
+ # in one tree so that an index which lists itself, an ancestor, or the same
57
+ # child twice is fetched only once instead of looping forever.
58
+ def visited
59
+ @visited ||= Set[url]
60
+ end
61
+
62
+ attr_writer :visited
63
+
59
64
  private
60
65
 
61
- def filter_sitemap_urls(urls)
62
- return urls if @options[:url_regex].nil?
66
+ def parse_sitemap_index
67
+ filter_sitemap_urls(sitemapindex.search('sitemap')).flat_map do |sitemap|
68
+ location = sitemap.at('loc').content
69
+ next [] unless visited.add?(location)
63
70
 
64
- urls.select { |url| url.at('loc').content.strip =~ @options[:url_regex] }
71
+ child_parser(location).urls.to_a
72
+ end
65
73
  end
66
74
 
67
- def inflate_body_if_needed(response)
68
- return response.body unless response.headers
75
+ def child_parser(location)
76
+ # A remote index must not be able to make us read files from local disk.
77
+ if remote_sitemap? && !remote_sitemap?(location)
78
+ raise "Refusing to read #{location}: sitemap index #{url} may only list http(s) URLs"
79
+ end
80
+
81
+ self.class.new(location, options).tap { |child| child.visited = visited }
82
+ end
83
+
84
+ def urlset
85
+ @urlset ||= sitemap.at('urlset')
86
+ end
69
87
 
70
- case response.headers['Content-Type']
71
- when %r{application/gzip}, %r{application/x-gzip}, %r{application/octet-stream}
72
- Zlib.gunzip(response.body)
73
- else
74
- response.body
88
+ def sitemapindex
89
+ @sitemapindex ||= sitemap.at('sitemapindex')
90
+ end
91
+
92
+ def strip_whitespace(urls)
93
+ urls.each do |url|
94
+ loc = url.at('loc') || raise("Malformed sitemap, #{url.name} without loc")
95
+ loc.content = loc.content.strip
75
96
  end
97
+
98
+ urls
99
+ end
100
+
101
+ def filter_sitemap_urls(urls)
102
+ urls = strip_whitespace(urls)
103
+ return urls if options[:url_regex].nil?
104
+
105
+ urls.select { |url| url.at('loc').content =~ options[:url_regex] }
106
+ end
107
+
108
+ # Inflates gzipped sitemaps (e.g. sitemap.xml.gz), detected by their magic
109
+ # bytes rather than the Content-Type, which servers often get wrong.
110
+ def inflate_if_gzipped(body)
111
+ return body unless body.byteslice(0, 2).bytes == GZIP_MAGIC
112
+
113
+ Zlib.gunzip(body)
114
+ end
115
+
116
+ def remote_sitemap?(location = url)
117
+ %r{\Ahttps?://}i.match?(location)
118
+ end
119
+
120
+ def local_sitemap?
121
+ File.exist?(url)
122
+ end
123
+
124
+ def fetch_remote_sitemap
125
+ return nil unless remote_sitemap?
126
+
127
+ request_options = options.except(:recurse, :url_regex)
128
+ request_options[:headers] = request_headers
129
+ request = Typhoeus::Request.new(url, request_options)
130
+
131
+ response = request.run
132
+ raise "HTTP request to #{url} failed with code #{response.code}." unless response.success?
133
+
134
+ inflate_if_gzipped(response.body)
135
+ end
136
+
137
+ # Returns the request headers, keeping any caller-supplied headers and adding
138
+ # a default User-Agent only when the caller didn't provide one, in any case.
139
+ def request_headers
140
+ headers = options[:headers] || {}
141
+ return headers if headers.keys.any? { |key| key.to_s.casecmp?('User-Agent') }
142
+
143
+ headers.merge('User-Agent' => 'Sitemap-Parser')
144
+ end
145
+
146
+ def read_local_sitemap
147
+ return nil unless local_sitemap?
148
+
149
+ inflate_if_gzipped(File.binread(url))
76
150
  end
77
151
  end
metadata CHANGED
@@ -1,27 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sitemap-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Balter
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2020-12-01 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: nokogiri
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - "~>"
16
+ - - ">="
18
17
  - !ruby/object:Gem::Version
19
18
  version: '1.6'
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
- - - "~>"
23
+ - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: '1.6'
27
26
  - !ruby/object:Gem::Dependency
@@ -44,62 +43,34 @@ dependencies:
44
43
  - - "<"
45
44
  - !ruby/object:Gem::Version
46
45
  version: '2.0'
47
- - !ruby/object:Gem::Dependency
48
- name: minitest
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: '4.7'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '4.7'
61
46
  - !ruby/object:Gem::Dependency
62
47
  name: rake
63
48
  requirement: !ruby/object:Gem::Requirement
64
49
  requirements:
65
50
  - - "~>"
66
51
  - !ruby/object:Gem::Version
67
- version: '10.4'
52
+ version: '13.0'
68
53
  type: :development
69
54
  prerelease: false
70
55
  version_requirements: !ruby/object:Gem::Requirement
71
56
  requirements:
72
57
  - - "~>"
73
58
  - !ruby/object:Gem::Version
74
- version: '10.4'
59
+ version: '13.0'
75
60
  - !ruby/object:Gem::Dependency
76
61
  name: rubocop
77
62
  requirement: !ruby/object:Gem::Requirement
78
63
  requirements:
79
64
  - - "~>"
80
65
  - !ruby/object:Gem::Version
81
- version: '0.80'
82
- type: :development
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - "~>"
87
- - !ruby/object:Gem::Version
88
- version: '0.80'
89
- - !ruby/object:Gem::Dependency
90
- name: rubocop-minitest
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - "~>"
94
- - !ruby/object:Gem::Version
95
- version: '0.1'
66
+ version: '1.0'
96
67
  type: :development
97
68
  prerelease: false
98
69
  version_requirements: !ruby/object:Gem::Requirement
99
70
  requirements:
100
71
  - - "~>"
101
72
  - !ruby/object:Gem::Version
102
- version: '0.1'
73
+ version: '1.0'
103
74
  - !ruby/object:Gem::Dependency
104
75
  name: rubocop-performance
105
76
  requirement: !ruby/object:Gem::Requirement
@@ -114,32 +85,18 @@ dependencies:
114
85
  - - "~>"
115
86
  - !ruby/object:Gem::Version
116
87
  version: '1.5'
117
- - !ruby/object:Gem::Dependency
118
- name: shoulda
119
- requirement: !ruby/object:Gem::Requirement
120
- requirements:
121
- - - "~>"
122
- - !ruby/object:Gem::Version
123
- version: '3.5'
124
- type: :development
125
- prerelease: false
126
- version_requirements: !ruby/object:Gem::Requirement
127
- requirements:
128
- - - "~>"
129
- - !ruby/object:Gem::Version
130
- version: '3.5'
131
88
  - !ruby/object:Gem::Dependency
132
89
  name: test-unit
133
90
  requirement: !ruby/object:Gem::Requirement
134
91
  requirements:
135
- - - "~>"
92
+ - - ">="
136
93
  - !ruby/object:Gem::Version
137
94
  version: '3.1'
138
95
  type: :development
139
96
  prerelease: false
140
97
  version_requirements: !ruby/object:Gem::Requirement
141
98
  requirements:
142
- - - "~>"
99
+ - - ">="
143
100
  - !ruby/object:Gem::Version
144
101
  version: '3.1'
145
102
  description: Ruby Gem to parse sitemaps.org compliant sitemaps.
@@ -148,12 +105,20 @@ executables: []
148
105
  extensions: []
149
106
  extra_rdoc_files: []
150
107
  files:
108
+ - CHANGELOG.md
109
+ - LICENSE.md
110
+ - README.md
151
111
  - lib/sitemap-parser.rb
112
+ - lib/sitemap-parser/version.rb
152
113
  homepage: https://github.com/benbalter/sitemap-parser
153
114
  licenses:
154
115
  - MIT
155
- metadata: {}
156
- post_install_message:
116
+ metadata:
117
+ homepage_uri: https://github.com/benbalter/sitemap-parser
118
+ source_code_uri: https://github.com/benbalter/sitemap-parser
119
+ bug_tracker_uri: https://github.com/benbalter/sitemap-parser/issues
120
+ changelog_uri: https://github.com/benbalter/sitemap-parser/blob/main/CHANGELOG.md
121
+ rubygems_mfa_required: 'true'
157
122
  rdoc_options: []
158
123
  require_paths:
159
124
  - lib
@@ -161,15 +126,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
161
126
  requirements:
162
127
  - - ">="
163
128
  - !ruby/object:Gem::Version
164
- version: '0'
129
+ version: '3.0'
165
130
  required_rubygems_version: !ruby/object:Gem::Requirement
166
131
  requirements:
167
132
  - - ">="
168
133
  - !ruby/object:Gem::Version
169
134
  version: '0'
170
135
  requirements: []
171
- rubygems_version: 3.1.4
172
- signing_key:
136
+ rubygems_version: 4.0.20
173
137
  specification_version: 4
174
138
  summary: Ruby Gem to parse sitemaps.org compliant sitemaps
175
139
  test_files: []