Skip to content

Instantly share code, notes, and snippets.

@BrekiTomasson
Created October 25, 2022 11:21
Show Gist options
  • Save BrekiTomasson/745863874df78f779b7f50471195b824 to your computer and use it in GitHub Desktop.
Save BrekiTomasson/745863874df78f779b7f50471195b824 to your computer and use it in GitHub Desktop.
put.io Download Script.
#!/bin/bash
TARGET=/mnt/user/Media/handbrake/watch/
PUTIO=/mnt/user/Media/PutioTemp/
USERNAME=###REDACTED###
PASSWORD=###REDACTED###
HOST=ftp.put.io
# Exit immediately if Lockfile exists.
[ -f /tmp/downloadLock.file ] && exit
echo " - Creating temp file"
echo $$ > /tmp/downloadLock.file
sleep 1
[ "x$(cat /tmp/downloadLock.file)" == "x"$$ ] || exit
if [ -d "${TARGET}" ]; then
if [ ! -d "${PUTIO}" ]; then
echo " - Creating downloads directory."
mkdir -p "${PUTIO}"
fi
echo " - Downloading files from put.io."
cd "${PUTIO}"
/usr/bin/lftp ftp://${USERNAME}@${HOST} -u ${USERNAME},${PASSWORD} -e "set ftp:ssl-allow no; mirror -v --Remove-source-dirs -c ./ ${PUTIO}; quit"
echo " - Ensuring downloaded files have valid permissions."
find "${PUTIO}" -type d -exec /usr/bin/chmod 775 {} \;
find "${PUTIO}" -type f -exec /usr/bin/chmod 664 {} \;
/usr/local/sbin/newperms "${PUTIO}" > /dev/null 2>&1
if [ -d "${PUTIO}" ]; then
echo " - Deleting extraneous files."
find "${PUTIO}" -type f \( \
-iname "*.txt" \
-o -iname "*.pdf" \
-o -iname "*.htm" \
-o -iname "*.html" \
-o -iname "*.nfo" \
-o -iname "1.jpg" \
-o -iname "pic.jpg" \
-o -iname "front.jpg" \
-o -iname "*.exe" \
-o -iname "*-sample*" \
-o -iname "*.bts.*" \) -delete
echo " - Moving remaining video files for processing."
find "${PUTIO}" -type f \( -iname "*.mp4" -o -iname "*.mkv" -o -iname "*.m4v" -o -iname "*.avi" -o -iname "*.wmv" \) -exec mv "{}" "${TARGET}" \;
# echo " - Renaming files wherever needed."
# cd ${TARGET}
# for f in [*; do mv "$f" "$(echo "$f" | sed -re 's/(\[.*])\s//')"; done > /dev/null 2>&1
echo " - Deleting empty folders."
cd "${PUTIO}" && find . -type d -empty -print0 | /usr/bin/xargs -0 rmdir '{}' > /dev/null 2>&1
echo " - Ensuring transferred files have valid permissions."
find "${TARGET}" -type d -exec /usr/bin/chmod 775 {} \;
find "${TARGET}" -type f -exec /usr/bin/chmod 664 {} \;
/usr/local/sbin/newperms "${TARGET}" > /dev/null 2>&1
fi
fi
echo " - Removing lock file."
rm /tmp/downloadLock.file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment