Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ## plexFlex
- ## by Windows10 Fanboi
- ########################
- ### config
- PLEXHOST="" # url:port
- PLEXTOKEN=""
- DESTPATH=""
- ### internal stuff
- SRCHNEEDLE=""
- SRCHSEASON=0
- SRCHEPISODE=0
- DODEBUG=0
- # errors are reported as HTML, good response is XML
- function isHTML () {
- cat ${1} | grep -E "^<html>" 2>&1 > /dev/null
- return $?
- }
- function dbgOut() {
- [ ${DODEBUG} -eq 0 ] && return;
- printf "%s " $( date +"%R:%S" )
- echo "${1}"
- }
- function cleanUp() {
- rm /tmp/plexflex.$$ 2>&1 > /dev/null
- [ -z "${1}" ] && exit 0
- exit ${1}
- }
- function urlencode() {
- # urlencode <string>
- old_lc_collate=$LC_COLLATE
- LC_COLLATE=C
- local length="${#1}"
- for (( i = 0; i < length; i++ )); do
- local c="${1:i:1}"
- case $c in
- [a-zA-Z0-9.~_-]) printf "$c" ;;
- *) printf '%%%02X' "'$c" ;;
- esac
- done
- LC_COLLATE=$old_lc_collate
- }
- # cleanup if interrupted
- trap cleanUp SIGINT SIGTERM
- # check if necessary utilites are available
- [ ! -x $( which curl ) ] && { echo "curl not installed/in PATH, fix it!"; cleanUp 1; }
- [ ! -x $( which xmllint ) ] && { echo "xmllint not installed/in PATH, fix it!"; cleanUp 1; }
- [ ! -x $( which jq ) ] && { echo "jq not installed/in PATH, fix it!"; cleanUp 1; }
- [ $# -lt 3 ] && { echo "${0} <SHOW> <SEASON> <EPISODE> [-d]"; cleanUp 1; }
- SRCHNEEDLE="${1}"
- SRCHSEASON=${2}
- SRCHEPISODE=${3}
- [ "${4}" == "-d" ] && DODEBUG=1
- # encode url because it has to be a GET request -.-
- ENCNEEDLE=$( urlencode "${SRCHNEEDLE}" )
- # fetch result
- curl -s -o /tmp/plexflex.$$ -b --ssl-no-revoke -k "https://${PLEXHOST}/search?query=${ENCNEEDLE}&X-Plex-Token=${PLEXTOKEN}"
- [ $? -ne 0 ] && { echo "Search request to ${PLEXHOST} failed, stopping."; cleanUp 1; }
- # check if we have only one result ( no decisions are made.. )
- VALCHK=$( xmllint --xpath "count (/MediaContainer/Directory)" /tmp/plexflex.$$ )
- [ ${VALCHK} -ne 1 ] && { echo "${SRCHNEEDLE} is not a unique name, exiting."; cleanUp 1; }
- # get path to XML where all the juicy seasons are stored..
- PLEXSEASONS=$( xmllint --xpath "string(/MediaContainer/Directory/@key)" /tmp/plexflex.$$ )
- [ -z "${PLEXSEASONS}" ] && { echo "xmllint query for seasons overview failed, exiting."; cleanUp 1; }
- # query plex
- curl -s -o /tmp/plexflex.$$ -b --ssl-no-revoke -k "https://${PLEXHOST}${PLEXSEASONS}?X-Plex-Token=${PLEXTOKEN}"
- [ $? -ne 0 ] && { echo "Seasons-overview query to ${PLEXHOST} failed, stopping."; cleanUp 1; }
- # do the same stuff with seasons..first get key to episodes for the wanted season
- PLEXEPISODES=$( xmllint --xpath "string(/MediaContainer/Directory[@index=${SRCHSEASON}]/@key)" /tmp/plexflex.$$ )
- [ -z "${PLEXEPISODES}" ] && { echo "xmllint query for episode overview for Season ${SRCHSEASON} failed, exiting."; cleanUp 1; }
- # fetch episode overview
- curl -s -o /tmp/plexflex.$$ -b --ssl-no-revoke -k "https://${PLEXHOST}${PLEXEPISODES}?X-Plex-Token=${PLEXTOKEN}"
- [ $? -ne 0 ] && { echo "Episode-overview query to ${PLEXHOST} failed, stopping."; cleanUp 1; }
- # now get the download link to the wanted episode
- PLEXDOWNLOAD=$( xmllint --xpath "string(/MediaContainer/Video[@index=${SRCHEPISODE}]/Media/Part/@key)" /tmp/plexflex.$$ )
- [ -z "${PLEXDOWNLOAD}" ] && { echo "xmllint query for download link failed, exiting."; cleanUp 1; }
- # get the size and the extension of the file
- DOWNLOADSIZE=$( xmllint --xpath "string(/MediaContainer/Video[@index=${SRCHEPISODE}]/Media/Part/@size)" /tmp/plexflex.$$ )
- SRCHEXTENSION=$( echo "${PLEXDOWNLOAD#*.}" )
- # concernate the final download path/name
- LOCDOWNLOAD=$( printf "${DESTPATH}/${SRCHNEEDLE} S%.2iE%.2i.%s" ${SRCHSEASON} ${SRCHEPISODE} ${SRCHEXTENSION} )
- # start download
- curl -s -o "${LOCDOWNLOAD}" -b --ssl-no-revoke -k "https://${PLEXHOST}${PLEXDOWNLOAD}?download=1&X-Plex-Token=${PLEXTOKEN}"
- [ $? -ne 0 ] && { echo "Download failed (${PLEXDOWNLOAD})"; cleanUp 1 }
- # get destination file size
- DLSIZE=$( stat "${LOCDOWNLOAD}" | grep Size: | awk -F\ '{ print $2 }' )
- [ "${DLSIZE}" != "${DOWNLOADSIZE}" ] && { echo "Downloaded size differs from size in metadata!" }
- # print destination name, for sonarr update purposes
- echo "${LOCDOWNLOAD}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement