linkchecker/linkcheck/logger/sitemapxml.py

120 lines
4.2 KiB
Python
Raw Normal View History

2014-01-08 21:33:04 +00:00
# Copyright (C) 2012-2014 Bastian Kleineidam
2012-09-18 07:16:34 +00:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
A sitemap XML logger.
"""
from . import xmllog
2012-09-23 18:59:38 +00:00
from .. import log, LOG_CHECK
2012-09-18 07:16:34 +00:00
ChangeFreqs = (
'always',
'hourly',
'daily',
'weekly',
'monthly',
'yearly',
'never',
)
2020-04-30 19:11:59 +00:00
HTTP_SCHEMES = ('http:', 'https:')
2012-09-23 18:59:38 +00:00
HTML_TYPES = ('text/html', "application/xhtml+xml")
class SitemapXmlLogger(xmllog._XMLLogger):
2012-09-23 18:59:38 +00:00
"""Sitemap XML output according to http://www.sitemaps.org/protocol.html
2012-09-18 07:16:34 +00:00
"""
2013-12-11 17:41:55 +00:00
LoggerName = 'sitemap'
LoggerArgs = {
"filename": "linkchecker-out.sitemap.xml",
"encoding": "utf-8",
}
def __init__(self, **kwargs):
2012-09-23 18:59:38 +00:00
"""Initialize graph node list and internal id counter."""
2013-12-11 17:41:55 +00:00
args = self.get_args(kwargs)
2012-09-18 07:16:34 +00:00
super(SitemapXmlLogger, self).__init__(**args)
# All URLs must have the given prefix, which is determined
# by the first logged URL.
self.prefix = None
2012-09-23 18:59:38 +00:00
# If first URL does not have a valid HTTP scheme, disable this
# logger
self.disabled = False
2012-09-18 07:16:34 +00:00
if 'frequency' in args:
if args['frequency'] not in ChangeFreqs:
raise ValueError("Invalid change frequency %r" % args['frequency'])
self.frequency = args['frequency']
else:
self.frequency = 'daily'
self.priority = None
if 'priority' in args:
self.priority = float(args['priority'])
def start_output(self):
2012-09-23 18:59:38 +00:00
"""Write start of checking info as xml comment."""
2012-09-18 07:16:34 +00:00
super(SitemapXmlLogger, self).start_output()
self.xml_start_output()
2020-04-30 19:11:59 +00:00
attrs = {"xmlns": "http://www.sitemaps.org/schemas/sitemap/0.9"}
self.xml_starttag('urlset', attrs)
2012-09-18 07:16:34 +00:00
self.flush()
2012-09-19 07:17:08 +00:00
def log_filter_url(self, url_data, do_print):
2012-09-23 18:59:38 +00:00
"""Update accounting data and determine if URL should be included
in the sitemap.
2012-09-19 07:17:08 +00:00
"""
self.stats.log_url(url_data, do_print)
2012-09-23 18:59:38 +00:00
if self.disabled:
return
2012-09-19 09:05:26 +00:00
# initialize prefix and priority
2012-09-18 07:16:34 +00:00
if self.prefix is None:
2012-09-23 18:59:38 +00:00
if not url_data.url.startswith(HTTP_SCHEMES):
log.warn(LOG_CHECK, "Sitemap URL %r does not start with http: or https:.", url_data.url)
self.disabled = True
return
2012-09-18 07:16:34 +00:00
self.prefix = url_data.url
2012-09-19 09:05:26 +00:00
# first URL (ie. the homepage) gets priority 1.0 per default
2012-09-18 07:16:34 +00:00
priority = 1.0
2012-09-23 21:20:21 +00:00
elif url_data.url == self.prefix:
return
2012-09-18 07:16:34 +00:00
else:
2012-09-19 07:17:08 +00:00
# all other pages get priority 0.5 per default
2012-09-18 07:16:34 +00:00
priority = 0.5
if self.priority is not None:
priority = self.priority
2012-09-19 09:05:26 +00:00
# ignore the do_print flag and determine ourselves if we filter the url
2012-09-23 18:59:38 +00:00
if (url_data.valid
and url_data.url.startswith(HTTP_SCHEMES)
and url_data.url.startswith(self.prefix)
2012-11-06 20:34:22 +00:00
and url_data.content_type in HTML_TYPES):
2012-09-19 09:05:26 +00:00
self.log_url(url_data, priority=priority)
def log_url(self, url_data, priority=None):
2012-09-23 18:59:38 +00:00
"""Log URL data in sitemap format."""
2020-04-30 19:11:59 +00:00
self.xml_starttag('url')
self.xml_tag('loc', url_data.url)
2012-09-18 10:12:00 +00:00
if url_data.modified:
2020-04-30 19:11:59 +00:00
self.xml_tag('lastmod', self.format_modified(url_data.modified, sep="T"))
self.xml_tag('changefreq', self.frequency)
self.xml_tag('priority', "%.2f" % priority)
self.xml_endtag('url')
2012-09-18 07:16:34 +00:00
self.flush()
def end_output(self, **kwargs):
2012-09-23 18:59:38 +00:00
"""Write XML end tag."""
2020-04-30 19:11:59 +00:00
self.xml_endtag("urlset")
2012-09-18 07:16:34 +00:00
self.xml_end_output()
self.close_fileoutput()