188 lines
5.7 KiB
Bash
Executable File
188 lines
5.7 KiB
Bash
Executable File
###############################################################################
|
|
# Copyright 2020 Michael Pfaff. See LICENSE.md for license.
|
|
###############################################################################
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -eE
|
|
|
|
_context='script init'
|
|
|
|
function trap_error {
|
|
echo "Error occurred during $_context on line $1" 1>&2
|
|
}
|
|
|
|
trap 'trap_error $LINENO' ERR
|
|
|
|
###############################################################################
|
|
# BEGIN CODE BY Teddy Skarin
|
|
# https://github.com/fearside/ProgressBar/blob/master/progressbar.sh
|
|
###############################################################################
|
|
|
|
# 1. Create ProgressBar function
|
|
# 1.1 Input is currentState($1) and totalState($2)
|
|
function ProgressBar {
|
|
# Process data
|
|
let _progress=(${1}*100/${2}*100)/100
|
|
let _done=(${_progress}*4)/10
|
|
let _left=40-$_done
|
|
# Build progressbar string lengths
|
|
_done=$(printf "%${_done}s")
|
|
_left=$(printf "%${_left}s")
|
|
|
|
# 1.2 Build progressbar strings and print the ProgressBar line
|
|
# 1.2.1 Output example:
|
|
# 1.2.1.1 Progress : [########################################] 100%
|
|
printf "\rProgress : [${_done// /#}${_left// /-}] ${_progress}%%"
|
|
|
|
}
|
|
|
|
#"} fixes stupid vim highlighting
|
|
###############################################################################
|
|
# END CODE BY Teddy Skarin
|
|
###############################################################################
|
|
|
|
true=1
|
|
false=0
|
|
|
|
_name=""
|
|
_fiddleId=""
|
|
_firstRevision=""
|
|
_lastRevision=""
|
|
|
|
fail=$false
|
|
|
|
while getopts ":hn:i:l:f:" opt; do
|
|
case ${opt} in
|
|
h )
|
|
echo "Usage:"
|
|
echo " fiddledl -h"
|
|
echo " fiddledl -n NAME -i FIDDLE_ID -l LAST_REVISION [-f FIRST_REVISION]"
|
|
echo ""
|
|
echo "Options:"
|
|
echo " -h"
|
|
echo " Show this help message."
|
|
echo " -n NAME"
|
|
echo " A friendly name to append to the result directory to assist in locating later."
|
|
echo " -i FIDDLE_ID"
|
|
echo " The id of the fiddle you want to download."
|
|
echo " -l LAST_REVISION"
|
|
echo " The last revision of the fiddle you would like to download."
|
|
echo " -f FIRST_REVISION"
|
|
echo " The first revision of the fiddle you would like to download."
|
|
exit 0
|
|
;;
|
|
n )
|
|
_name=$OPTARG
|
|
echo "Name: $_name"
|
|
;;
|
|
i )
|
|
_fiddleId=$OPTARG
|
|
echo "FiddleId: $_fiddleId"
|
|
;;
|
|
l )
|
|
_lastRevision=$OPTARG
|
|
echo "Last revision: $_lastRevision"
|
|
;;
|
|
f )
|
|
_firstRevision=$OPTARG
|
|
echo "First revision: $_firstRevision"
|
|
;;
|
|
\? )
|
|
echo "Unrecognized option: $OPTARG" 1>&2
|
|
exit 64
|
|
;;
|
|
: )
|
|
echo "Missing argument: $OPTARG requires an argument" 1>&2
|
|
exit 64
|
|
;;
|
|
esac
|
|
done
|
|
shift $((OPTIND -1))
|
|
|
|
if [[ ! -n $_name ]]; then
|
|
echo "Name not specified. Only fiddleId will be used for directory name."
|
|
fi
|
|
|
|
if [[ ! -n $_fiddleId ]]; then
|
|
echo "fiddleId must be specified."
|
|
#fail=$true
|
|
exit 64
|
|
fi
|
|
|
|
if [[ ! -n $_lastRevision ]]; then
|
|
echo "Last revision was not specified. First revision will be ignored and only the last revision will be downloaded."
|
|
#fail=$true
|
|
fi
|
|
|
|
if [[ ! -n $_firstRevision ]]; then
|
|
if [[ -n $_lastRevision ]]; then
|
|
echo "First revision not specified. Will be 1."
|
|
_firstRevision=1
|
|
fi
|
|
fi
|
|
|
|
read -p "Are you sure? [y/N] " -n 1 -r
|
|
echo # (optional) move to a new line
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
exit 1
|
|
fi
|
|
|
|
if [[ ! -n $_name ]]; then
|
|
_dir="${_fiddleId}"
|
|
else
|
|
_dir="${_fiddleId}-${_name}"
|
|
fi
|
|
|
|
mkdir "$_dir"
|
|
|
|
if [[ -n $_lastRevision ]]; then
|
|
echo "Downloading..."
|
|
|
|
# start jobs in background
|
|
for (( i=_firstRevision; i<=_lastRevision; i++)); do
|
|
curl "https://fiddle.jshell.net/${_fiddleId}/${i}/show/" -H "Referer: http://fiddle.jshell.net/${_fiddleId}/${i}/" --output "${_dir}/${i}.html" >> /dev/null 2>&1 &
|
|
done
|
|
|
|
let _total=$((_lastRevision - _firstRevision))
|
|
|
|
# wait for jobs, counting as we go so we can track progress
|
|
while (( $(jobs | wc -l) > 0 )); do
|
|
_jobCount=$(jobs | wc -l)
|
|
let _doneCount=$(( _total - _jobCount))
|
|
#let _progress=$(( _doneCount / _total * 100))
|
|
#echo "$_progress% | $_doneCount/$_total ($_jobCount remaining)"
|
|
ProgressBar $_doneCount $_total
|
|
jobs > /dev/null
|
|
sleep 0.1
|
|
done
|
|
echo ""
|
|
|
|
echo "Patching..."
|
|
|
|
# patch files
|
|
for (( i=_firstRevision; i<=_lastRevision; i++)); do
|
|
{ sed -i .scriptendfunk -e 's;//]]>;;g' "${_dir}/${i}.html"; sed -i .unpatched -e 's "// "https:// g' "${_dir}/${i}.html"; sed -i .stockcss -e 's;<link rel="stylesheet" type="text/css" href="/css/result-light.css">;;g' "${_dir}/${i}.html"; } &
|
|
done
|
|
|
|
# we run three different processes on each file, hence 3.
|
|
#let _total=$(( _total * 3 ))
|
|
|
|
# wait for jobs, counting as we go so we can track progress
|
|
while (( $(jobs | wc -l) > 0 )); do
|
|
_jobCount=$(jobs | wc -l)
|
|
let _doneCount=$(( _total - _jobCount))
|
|
#let _progress=$(( _doneCount / _total * 100))
|
|
#echo "$_progress% | $_doneCount/$_total ($_jobCount remaining)"
|
|
ProgressBar $_doneCount $_total
|
|
jobs > /dev/null
|
|
sleep 0.1
|
|
done
|
|
echo ""
|
|
else
|
|
curl "https://fiddle.jshell.net/${_fiddleId}/show/" -H "Referer: http://fiddle.jshell.net/${_fiddleId}/" --output "${_dir}/latest.html"
|
|
sed -i .scriptendfunk -e 's;//]]>;;g' "${_dir}/latest.html"
|
|
sed -i .unpatched -e 's "// "https:// g' "${_dir}/latest.html"
|
|
sed -i .stockcss -e 's;<link rel="stylesheet" type="text/css" href="/css/result-light.css">;;g' "${_dir}/latest.html"
|
|
fi
|