grab-site/libgrabsite/ignoracle.py

86 lines
2.1 KiB
Python
Raw Normal View History

import re
import sys
from urllib.parse import urlparse
# Increase the compiled regexp cache limit from 512 to 4096 in case
# someone uses a lot of ignores.
re._MAXCACHE = 4096
2015-07-18 11:00:08 +00:00
class Ignoracle(object):
2015-07-18 11:00:08 +00:00
"""
An Ignoracle tests a URL against a list of patterns and returns whether or
not that URL should be grabbed.
2015-07-18 11:00:08 +00:00
An Ignoracle's pattern list starts as the empty list.
"""
2015-07-18 11:00:08 +00:00
patterns = []
2015-07-18 11:00:08 +00:00
def set_patterns(self, strings):
"""
Given a list of strings, replaces this Ignoracle's pattern state with
that list.
"""
self.patterns = []
2015-07-18 11:00:08 +00:00
for string in strings:
if isinstance(string, bytes):
string = string.decode('utf-8')
2015-07-18 11:00:08 +00:00
self.patterns.append(string)
2015-07-18 11:00:08 +00:00
def ignores(self, url, **kwargs):
"""
If an ignore pattern matches the given URL, returns that pattern as a string.
Otherwise, returns False.
"""
pu = re.escape(kwargs.get('primary_url', ''))
ph = re.escape(kwargs.get('primary_netloc', ''))
2015-07-18 11:00:08 +00:00
for pattern in self.patterns:
regexp = pattern
if '{' in regexp:
regexp = regexp.replace('{primary_url}', pu).replace('{primary_netloc}', ph)
2015-07-18 11:00:08 +00:00
try:
if re.search(regexp, url):
2015-07-18 11:00:08 +00:00
return pattern
except re.error as error:
print('Pattern %s is invalid (error: %s). Ignored.' % (pattern, str(error)), file=sys.stderr)
2015-07-18 11:00:08 +00:00
return False
def parameterize_record_info(record_info):
2015-07-18 11:00:08 +00:00
"""
Given a wpull record_info dict, generates a dict with primary_url and
primary_netloc keys. This is meant to be used in Ignoracle.ignores.
2015-07-18 11:00:08 +00:00
The primary_url key is:
2015-07-18 11:00:08 +00:00
1. record_info['top_url'], or
2. record_info['url'] if record_info['level'] is zero, or
3. None otherwise.
2015-07-18 11:00:08 +00:00
If primary_url is a valid URL, the primary_netloc key is the network
location component of primary_url (i.e. for HTTP,
[user:password@]host[:port]). Otherwise, primary_netloc is None.
"""
primary_url = None
primary_netloc = None
2015-07-18 11:00:08 +00:00
if record_info.get('level') == 0:
primary_url = record_info.get('url')
else:
primary_url = record_info.get('top_url')
2015-07-18 11:00:08 +00:00
if primary_url:
parsed = urlparse(primary_url)
primary_netloc = parsed.netloc
2015-07-18 11:00:08 +00:00
return dict(
primary_url=primary_url,
primary_netloc=primary_netloc
)