Add source set handling.
This commit is contained in:
parent
1c609454ed
commit
99274cf6d7
58
macli.py
58
macli.py
@ -25,6 +25,13 @@ OBJECT_TYPE_DESCRIPTORS = (
|
||||
'styles',
|
||||
'href',
|
||||
),
|
||||
(
|
||||
{
|
||||
'name': 'source'
|
||||
},
|
||||
'sets',
|
||||
'srcset',
|
||||
),
|
||||
(
|
||||
{
|
||||
'name': 'img'
|
||||
@ -94,27 +101,44 @@ def archive(content, base_url, selector, directory, attribute, progbar):
|
||||
if part[attribute].startswith('data:'):
|
||||
continue
|
||||
|
||||
href = urllib.parse.urljoin(base_url, part[attribute],
|
||||
allow_fragments=False)
|
||||
progbar.next_iter(href)
|
||||
selectors = []
|
||||
if attribute == "srcset":
|
||||
# Dirty hack to support srcset syntax
|
||||
hrefs = []
|
||||
for option in part[attribute].split(','):
|
||||
val, selector = option.split()
|
||||
hrefs.append(urllib.parse.urljoin(base_url, val,
|
||||
allow_fragments=False))
|
||||
selectors.append(selector)
|
||||
else:
|
||||
hrefs = [urllib.parse.urljoin(base_url, part[attribute],
|
||||
allow_fragments=False)]
|
||||
|
||||
name = (hashlib.sha1(href.encode()).hexdigest()
|
||||
+ '/'
|
||||
+ get_filename(part[attribute]))
|
||||
resolveds = []
|
||||
for i, href in enumerate(hrefs):
|
||||
progbar.next_iter(href)
|
||||
|
||||
path = os.path.join(directory, name)
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
if not os.path.exists(path):
|
||||
try:
|
||||
content = request(href).read()
|
||||
except Exception as e:
|
||||
show_error(e, href)
|
||||
continue
|
||||
name = (hashlib.sha1(href.encode()).hexdigest()
|
||||
+ '/'
|
||||
+ get_filename(part[attribute]))
|
||||
|
||||
with open(path, 'wb') as f:
|
||||
f.write(content)
|
||||
path = os.path.join(directory, name)
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
if not os.path.exists(path):
|
||||
try:
|
||||
content = request(href).read()
|
||||
except Exception as e:
|
||||
show_error(e, href)
|
||||
continue
|
||||
|
||||
part[attribute] = path
|
||||
with open(path, 'wb') as f:
|
||||
f.write(content)
|
||||
|
||||
resolveds.append(path)
|
||||
if i < len(selectors):
|
||||
resolveds[-1] += ' ' + selectors[i]
|
||||
|
||||
part[attribute] = ', '.join(resolveds)
|
||||
|
||||
|
||||
def relink_links(content, base_url):
|
||||
|
Loading…
Reference in New Issue
Block a user