-
Notifications
You must be signed in to change notification settings - Fork 9
/
simple_site_checker.py
151 lines (120 loc) · 4.72 KB
/
simple_site_checker.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
#!/usr/bin/env python
import argparse
from datetime import datetime
import logging
import os
import sys
import urllib2
from lxml import etree
USER_AGENT = ''
SITEMAP_NAMESPACE = 'http://www.sitemaps.org/schemas/sitemap/0.9'
XMLNS = {'sitemap': SITEMAP_NAMESPACE}
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
VERBOSE_HELP = (
"""Verbose mode. Controls the script output
0 - print output only in case of errors
1 - prints the result count plus list of failed URLs(if any)
2 - print all checked URLs \n""")
LOGGING_LEVELS = {
0: logging.ERROR,
1: logging.INFO,
2: logging.DEBUG,
}
logger = logging.getLogger(__name__)
class HeadRequest(urllib2.Request):
def get_method(self):
return "HEAD"
class XMLSitemapParser(object):
total = 0
succeeded = 0
failed = []
sitemaps = {}
def load_sitemap(self, url):
logger.debug('Loading sitemap %s' % url)
if '://' in url:
try:
sitemap = urllib2.urlopen(urllib2.Request(url, headers={'User-Agent': USER_AGENT}))
except urllib2.HTTPError, e:
if e.code == 404:
logger.error('Sitemap not found as %s' % url)
elif e.code == 500:
logger.error('Server error when accessing sitemap as %s' % url)
else:
logger.error('Server error \'%s\' when accessing sitemap as %s' % (e, url))
sys.exit(1)
except Exception, e:
logger.debug('Unexpected error', e)
logger.error('Unexpected error while loading sitemap.')
sys.exit(1)
else:
try:
path = os.path.abspath(url)
sitemap = open(url)
except Exception, e:
logger.error('Unable to load sitemap file from %s' % path)
logger.debug(e)
sys.exit(1)
try:
tree = etree.parse(sitemap)
except Exception, e:
logger.debug('Unexpected error', e)
logger.error('Unexpected error while parsing sitemap XML from %s' % url)
else:
root = tree.getroot()
if root.tag == '{%s}sitemapindex' % SITEMAP_NAMESPACE:
self.process_sitemapindex(tree)
else:
self.sitemaps[url] = tree
def process_sitemapindex(self, tree):
logger.debug('Processing sitemapindex')
for tag in tree.xpath('//sitemap:sitemap/sitemap:loc', namespaces=XMLNS):
sitemap_loc = tag.text
self.load_sitemap(sitemap_loc)
def process_sitemap(self, sitemap):
tree = self.sitemaps[sitemap]
logger.debug('Processing sitemap %s' % sitemap)
loc_tags = tree.xpath('//sitemap:loc', namespaces=XMLNS)
urls_found = len(loc_tags)
self.total += urls_found
logger.info('%i URLs found' % urls_found)
for tag in loc_tags:
loc_url = tag.text
logger.debug('Checking %s' % loc_url)
try:
response = urllib2.urlopen(HeadRequest(loc_url, headers={'User-Agent': USER_AGENT}))
self.succeeded += 1
logger.info('%s - OK' % loc_url)
except Exception, e:
self.failed.append((loc_url, e))
logger.error('%s -> %s' % (loc_url, e))
def process_sitemaps(self):
for sitemap in self.sitemaps:
self.process_sitemap(sitemap)
def time_info(start, end):
hours, remainder = divmod((end-start).seconds, 3600)
minutes, seconds = divmod(remainder, 60)
logger.info('Start - %s' % start.strftime(DATETIME_FORMAT))
logger.info('End - %s' % end.strftime(DATETIME_FORMAT))
logger.info('Time elapsed %s:%s:%s' % (hours, minutes, seconds))
def main():
arg_parser = argparse.ArgumentParser(description='Simple Site Checker',
formatter_class=argparse.RawTextHelpFormatter)
arg_parser.add_argument('sitemap', metavar='s', type=str,
help='XML sitemap URL/path')
arg_parser.add_argument('-v', '--verbose', type=int, required=False,
help=VERBOSE_HELP, default = 0, choices=LOGGING_LEVELS)
args = arg_parser.parse_args()
logging.basicConfig(format='%(levelname)s: %(message)s',
level = LOGGING_LEVELS[args.verbose])
start = datetime.now()
url = args.sitemap
parser = XMLSitemapParser()
parser.load_sitemap(url)
parser.process_sitemaps()
end = datetime.now()
failed_number = len(parser.failed)
logger.info('Result - Checked %i, succeeded %i, failed %i' %
(parser.total, parser.succeeded, failed_number))
time_info(start, end)
if __name__ == '__main__':
main()