Do more checks before jumping to relinking.

This commit is contained in:
kenkeiras 2017-07-05 00:15:43 +02:00
parent 36b375d9d4
commit 4b3dd00086

View File

@ -52,6 +52,9 @@ def relink_css(content, base_url, directory='styles'):
def relink_images(content, base_url, directory='images'): def relink_images(content, base_url, directory='images'):
os.makedirs(directory, exist_ok=True) os.makedirs(directory, exist_ok=True)
for image in content.find_all('img'): for image in content.find_all('img'):
if 'src' not in image.attrs:
continue
src = urllib.parse.urljoin(base_url, image['src'], allow_fragments=False) src = urllib.parse.urljoin(base_url, image['src'], allow_fragments=False)
name = hashlib.sha1(src.encode()).hexdigest() + '.' + get_extension(src) name = hashlib.sha1(src.encode()).hexdigest() + '.' + get_extension(src)
@ -67,6 +70,9 @@ def relink_images(content, base_url, directory='images'):
def relink_scripts(content, base_url, directory='scripts'): def relink_scripts(content, base_url, directory='scripts'):
os.makedirs(directory, exist_ok=True) os.makedirs(directory, exist_ok=True)
for script in content.find_all('script'): for script in content.find_all('script'):
if 'src' not in script.attrs:
continue
href = urllib.parse.urljoin(base_url, script['src'], allow_fragments=False) href = urllib.parse.urljoin(base_url, script['src'], allow_fragments=False)
name = hashlib.sha1(href.encode()).hexdigest() + '.' + get_extension(href) name = hashlib.sha1(href.encode()).hexdigest() + '.' + get_extension(href)