Created
January 31, 2024 05:42
-
-
Save benkant/58c694a5a8697465a69365d79c39ec00 to your computer and use it in GitHub Desktop.
Download MP4s follow redirects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Downloads MP4 files from a specified webpage URL provided as a command-line argument, | |
# following redirects. It requires 'curl' for operation. If 'curl' is not found, it | |
# advises the user to install it. | |
# | |
# Usage: | |
# ./download_mp4s.sh <URL> | |
check_for_curl() { | |
if ! command -v curl &> /dev/null; then | |
echo "'curl' is required but not installed. Please install 'curl' and try again." | |
exit 1 | |
fi | |
} | |
download_file() { | |
local url="$1" | |
echo "Downloading ${url} with curl..." | |
curl -LO "${url}" | |
} | |
fetch_html_content() { | |
local url="$1" | |
curl -Ls "${url}" | |
} | |
parse_mp4_links() { | |
local html="$1" | |
# This pattern looks for href attributes that end with .mp4, accounting for potential HTML entity encoding within the href value. | |
# Use egrep in case we're on macOS | |
echo "${html}" | egrep -o 'href="[^\"]+\.mp4"' | cut -d'"' -f2 | |
} | |
main() { | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: $0 <URL>" | |
exit 1 | |
fi | |
local url="$1" | |
check_for_curl | |
local html=$(fetch_html_content "${url}") | |
local links=$(parse_mp4_links "${html}") | |
for link in ${links}; do | |
if [[ "${link}" =~ ^http ]]; then | |
download_file "${link}" | |
else | |
# Assuming the provided URL is the base path for relative links | |
local absolute_link=$(printf "%s%s" "${url}" "${link}") | |
download_file "${absolute_link}" | |
fi | |
done | |
echo "Download completed." | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment