-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemaps-test.py
executable file
·465 lines (368 loc) · 13 KB
/
sitemaps-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env python3
"""
Test a website's sitemap.xml implementation.
Download a website's sitemap.xml and all of the index files that it references,
and report on how many links were found, and what sort of attributes they
contained.
For example::
$ sitemaps-test.py lost.co.nz
Downloaded 327B from: https://lost.co.nz/sitemap.xml
Sitemap index found containing 3 URLs
Downloaded 1.6kB from: https://lost.co.nz/sitemap-links.xml
Found 12 URLs, 12 lastmod, 12 changefreq, and 12 priority fields
Downloaded 2.3kB from: https://lost.co.nz/sitemap-articles.xml
Found 17 URLs, 17 lastmod, 17 changefreq, and 17 priority fields
Downloaded 1.2kB from: https://lost.co.nz/sitemap-projects.xml
Found 9 URLs, 9 lastmod, 9 changefreq, and 9 priority fields
Total of 39 URLs found in 4 files
Requires:
Python 3.9+ and the 3rd-party `requests` library for HTTP downloads.
"""
import argparse
import collections
from dataclasses import dataclass
from enum import Enum
import functools
import io
import logging
import math
import sys
from typing import IO, Iterator, List, Optional, TypeAlias
from urllib.parse import SplitResult, urlsplit, urlunsplit
from xml.etree import ElementTree
import requests
logger = logging.getLogger(__name__)
XML: TypeAlias = Iterator[ElementTree.Element]
def file_size(size: int, traditional: bool = False) -> str:
"""
Convert a file size in bytes to a easily human-parseable form, using only
one or two significant figures.
Raises:
ValueError: If given size has an error in its... ah... value.
Args:
size:
file size in bytes
traditional:
Use traditional base-2 units, otherwise default to using
'proper' SI multiples of 1000.
Returns:
Short string, like '4.3kB'
"""
try:
size = int(size)
except (ValueError, TypeError):
raise ValueError("Given file size '{}' not numeric.".format(size))
if size < 0:
raise ValueError("Given file size '{}' not positive".format(size))
if size < 1000:
return '{}B'.format(size)
suffixes = {
1000: ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
}
multiple = 1024 if traditional else 1000
divided = float(size)
for suffix in suffixes[multiple]:
divided /= multiple
if divided < multiple:
divided = round_significant(divided, 2)
divided = int(divided) if divided >= 10 else divided
return '{:,}{}'.format(divided, suffix)
# Greater than 1000 Yottabytes!? That is a pile of 64GB MicroSD cards
# as large as the Great Pyramid of Giza! You're dreaming, but in the
# interests of completeness...
# http://en.wikipedia.org/wiki/Yottabyte
return '{:,}{}'.format(int(round(divided)), suffix)
def find_text(elem: ElementTree.Element, tag: str) -> Optional[str]:
"""
Simple helper to find text inside tag inside element or return none.
"""
found = elem.find(tag)
return None if found is None else found.text
def round_significant(number: float, digits: int = 2) -> float:
"""
Round number to the given number of sigificant digits. eg::
>>> round_significant(1235, digits=2)
1200
Returns:
Number rounded to the given number of digits
"""
digits = int(digits)
if digits <= 0:
raise ValueError("Must have more than zero significant digits")
if not number:
return 0
number = float(number)
magnitude = int(math.floor(math.log10(abs(number))))
ndigits = digits - magnitude - 1
return round(number, ndigits)
def xml_strip_iterparse(file_: IO[str]) -> XML:
"""
Iterate the given file into XML, while stripping namespace prefixes.
Python's `xml.etree` parser is performant and its API is easy to work
*except* when the document has a namespace, in which case that namespace
is forced into every lookup, eg.
>>> elem.find('{http://www.sitemaps.org/schemas/sitemap/0.9}urlset')
This function manipulates the tree so that that is no longer required.
>>> elem.find('urlset')
There has been discussion about addressing this problem in core Python,
but at the time of writing, that has yet to be introduced into the stdlib.
Args:
file_:
A file-like object.
Returns:
A generator over input file, to allow for large inputs, yielding
`ElementTree.Element` objects
"""
for event, element in ElementTree.iterparse(file_):
# Replace 'tag' string
tag = element.tag
if tag and isinstance(tag, str) and tag[0] == '{':
element.tag = tag.partition('}')[2]
# Modify 'attrib' dictionary in-place
attrib = element.attrib
if attrib:
for name, value in list(attrib.items()):
if name and isinstance(name, str) and name[0] == '{':
del attrib[name]
attrib[name.partition('}')[2]] = value
yield element
class ChangeFreq(Enum):
ALWAYS = 'always'
HOURLY = 'hourly'
DAILY = 'daily'
WEEKLY = 'weekly'
MONTHLY = 'monthly'
YEARLY = 'yearly'
NEVER = 'never'
@dataclass
class Location:
"""
Collect the core fields for sitemap elements that describe an end-point.
Used by `SitemapReader.read_sitemap()`
"""
loc: str
lastmod: Optional[str]
changefreq: Optional[str]
priority: Optional[float]
class Downloader:
def __init__(self, base_url: str):
"""
Initialiser.
Args:
base_url (str):
The URL of the top-level sitemap resource, eg.
'https://example.com'
"""
self.base_url = self._clean_url(base_url)
# Allow reuse of TCP connection
self.session = requests.session()
def build_url(self, path: str) -> str:
"""
Join base URL and path to create full URL.
Args:
path:
Path part of url, eg. '/sitemap.xml'
Returns:
Absolute URL
"""
parts = urlsplit(self.base_url)
if not parts.scheme:
parts._replace(scheme='https')
parts = parts._replace(path=path)
url = urlunsplit(parts)
return url
@functools.lru_cache(maxsize=1)
def get_text(self, url: str) -> str:
"""
Fetch a single resource and convert to unicode string.
Args:
url:
Absolute URL to resource, eg. 'https://example.com/robots.txt'
Raises:
RuntimeError:
On any network problems.
Returns:
Plain unicode string.
"""
try:
response = self.session.get(url, stream=True, timeout=5.0)
response.raise_for_status()
text = response.text
except requests.exceptions.HTTPError as e:
logger.error(e)
raise RuntimeError(e) from None
except requests.exceptions.RequestException as e:
logger.error(e)
raise RuntimeError(e) from None
logger.info('Downloaded %s from: %s', file_size(len(text)), url)
return text
def get_xml(self, url: str) -> XML:
"""
Fetch and pre-parse XML resource
Args:
url:
Full URL to resource, eg. 'https://example.com/sitemap.xml'
Returns:
Iterator, as built by `xml_strip_iterparse()`
"""
# Build file-like object from string for `xml.etree`
text = self.get_text(url)
file_ = io.StringIO(text)
yield from xml_strip_iterparse(file_)
def _clean_url(self, base_url: str) -> str:
"""
Add schema to base_url, if missing.
"""
parts = urlsplit(base_url)
if not parts.scheme:
parts = SplitResult('https', parts.path, '', '', '')
return urlunsplit(parts)
class SitemapReader:
"""
Parse sitemap index and list files.
"""
def __init__(self, domain: str):
"""
Initialiser.
Args:
domain:
The hostname of website to check, eg. example.com
"""
self.downloader = Downloader(domain)
def print_index(self, urls: list[str]) -> None:
"""
Print details about the sitemap index, and the sitemaps pointed to.
"""
total_urls = 1 # We started with '/sitemap.xml'
print(f" Sitemap index found containing {len(urls):,} URLs")
print()
for url in urls:
locations = self.read_sitemap(url)
self.print_sitemap(locations)
total_urls += len(locations)
print()
print(f"Total of {total_urls:,} URLs found in {len(urls) + 1} files")
def print_sitemap(self, locations: List[Location]) -> None:
"""
Print basic details about sitemap file.
"""
fields = ('loc', 'lastmod', 'changefreq', 'priority')
# Build count of implemented fields
counts: dict[str, int] = collections.Counter()
for location in locations:
for field in fields:
if hasattr(location, field):
counts[field] += 1
print(
f" Found {counts['loc']:,} URLs, {counts['lastmod']:,} lastmod, "
f"{counts['changefreq']:,} changefreq, and "
f"{counts['priority']:,} priority fields"
)
def read_index(self, path: str) -> tuple[list[str], list[Location]]:
"""
Extract URLs to detail sitemaps, if any, from sitemap index.
<sitemapindex>
<sitemap>
<loc>https://example.com/sitemap-news-articles.xml</loc>
</sitemap>
...
</sitemapindex>
We should download these other sitemaps and add their 'url' elements
to the processing queue.
Args:
path:
Path to file, eg '/sitemap.xml'
Returns:
List of URLs to sitemap files.
"""
url = self.downloader.build_url(path)
data = self.downloader.get_xml(url)
urls = []
for elem in data:
if elem.tag == 'sitemap':
loc = find_text(elem, 'loc')
if loc is None:
raise ValueError('Missing required <loc> element')
else:
urls.append(loc)
locations = self.read_sitemap(url)
return (urls, locations)
def read_sitemap(self, path: str) -> List[Location]:
"""
Download sitemap file, build list of `Location` data objects.
<urlset>
<url>
<loc>https://example.com/articles/question-empathy/</loc>
<changefreq>daily</changefreq>
<lastmod>2019-03-25</lastmod>
<priority>0.9</priority>
</url>
...
</urlset>
Args:
path:
Path to sitemap file, eg, "/sitemap-products.xml"
Raises:
ValueError:
If any required elements are missing.
Returns:
List of `Location` dataclass instances.
"""
locations = []
data = self.downloader.get_xml(path)
for elem in data:
if elem.tag == 'url':
priority = (
float(temp)
if (temp := find_text(elem, 'priority'))
else None
)
loc = find_text(elem, 'loc')
if loc is None:
raise ValueError('Missing required <loc> element')
location = Location(
loc=loc,
lastmod=find_text(elem, 'lastmod'),
changefreq=find_text(elem, 'changefreq'),
priority=priority,
)
locations.append(location)
return locations
def parse(arguments: List[str]) -> argparse.Namespace:
"""
Parse command-line arguments to produce set of options to give to `main()`.
"""
description = "Test sitemap.xml files and their links"
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
'url_base',
metavar='BASE_URL',
help='Base url of site, eg. https://example.com/')
return parser.parse_args()
def setup_logging() -> None:
logging.basicConfig(
format='%(message)s',
level=logging.INFO,
)
def main(options: argparse.Namespace) -> None:
"""
Script entry point.
"""
setup_logging()
sitemaps = SitemapReader(options.url_base)
urls, locations = sitemaps.read_index('/sitemap.xml')
# Sitemap index or plain file?
if urls:
sitemaps.print_index(urls)
else:
sitemaps.print_sitemap(locations)
if __name__ == '__main__':
exit_code = 0
options = parse(sys.argv[1:])
try:
main(options)
except RuntimeError as e:
print(e, file=sys.stderr)
exit_code = 1
sys.exit(exit_code)