Skip to content

Instantly share code, notes, and snippets.

@bootrino
Created August 12, 2019 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bootrino/07ee1a45816827a2d74269c251c5b020 to your computer and use it in GitHub Desktop.
Save bootrino/07ee1a45816827a2d74269c251c5b020 to your computer and use it in GitHub Desktop.
"""
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
# this program takes a zipfile exported from codepen and downloads the resources and
# packages it up into a new zipfile
#
# usage:
# python codepen_offline.py <zipfilename>
#
import subprocess
import sys
from pathlib import Path
import tempfile
import os
if len(sys.argv) != 2:
print('a zip filename is required')
if not sys.argv[1].endswith('.zip'):
print('a zip filename is required')
p = Path(sys.argv[1])
print(p.stem)
zipfile_name = p.stem
def run_command(command, cwd=None):
if cwd:
print(f'cwd is {cwd}')
output,error = subprocess.Popen(
command,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=cwd
).communicate()
else:
output,error = subprocess.Popen(
command, universal_newlines=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print(output)
print(error)
with tempfile.TemporaryDirectory() as unzip_tmpdirname:
print('created temporary directory', unzip_tmpdirname)
command = [
"/usr/bin/unzip",
f"{p.stem}.zip",
"-d",
unzip_tmpdirname,
]
print('unzipping to ', unzip_tmpdirname)
run_command(command)
with tempfile.TemporaryDirectory() as httrack_tmpdirname:
# do httrack
# notice here we are compensating for a lonely root from the zipfile
command = [
'/usr/bin/httrack',
'--user-agent',
f'"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9"',
'-p7',
'-N4',
'--clean',
'--verbose',
'−−mirror-links',
'--path',
f'"{httrack_tmpdirname}"',
'-n ',
f'"file://{unzip_tmpdirname}/{zipfile_name}/dist/index.html"',
]
print('doing httrack site mirror......')
print(' '.join(command))
run_command(command, cwd=f'{unzip_tmpdirname}/{zipfile_name}/dist')
print('remove httrack garbage hts-cache')
command = ['/bin/rm', '-rf', f'{httrack_tmpdirname}/hts-cache']
print(command)
run_command(command)
print('remove httrack garbage backblue.gif')
command = ['/bin/rm', '-rf', f'{httrack_tmpdirname}/backblue.gif']
print(command)
run_command(command)
print('remove httrack garbage *mirror-links')
command = ['/bin/rm', '-rf', f'{httrack_tmpdirname}/*mirror-links']
print(command)
run_command(command)
print('remove httrack garbage file fade.gif')
command = ['/bin/rm', '-rf', f'{httrack_tmpdirname}/fade.gif']
print(command)
run_command(command)
print('remove httrack garbage file index.html')
command = ['/bin/rm', '-rf', f'{httrack_tmpdirname}/index.html']
print(command)
run_command(command)
print('rename index-2.html to index.html due to httrack weirdness')
command = ['/bin/mv', f'{httrack_tmpdirname}/web/index-2.html', f'{httrack_tmpdirname}/web/index.html']
print(command)
run_command(command)
print('zip the output back to the current working directory')
# i.e. the directory that this command was run from
directory_to_save_zip_to = os.getcwd()
command = ['/usr/bin/zip', '-r', f'{directory_to_save_zip_to}/{zipfile_name}_out.zip', f'.']
print(command)
# set the working directory for zipping
run_command(command, f'{httrack_tmpdirname}/web')
print(f'finished, filename is: {directory_to_save_zip_to}/{zipfile_name}_out.zip')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment