Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.21 KB | None | 0 0
  1. #!/bin/bash
  2. ## plexFlex
  3. ## by Windows10 Fanboi
  4. ########################
  5.  
  6. ### config
  7. PLEXHOST="" # url:port
  8. PLEXTOKEN=""
  9.  
  10. DESTPATH=""
  11.  
  12. ### internal stuff
  13. SRCHNEEDLE=""
  14. SRCHSEASON=0
  15. SRCHEPISODE=0
  16. DODEBUG=0
  17.  
  18. # errors are reported as HTML, good response is XML
  19. function isHTML () {
  20.         cat ${1} |  grep -E "^<html>" 2>&1 > /dev/null
  21.         return $?
  22. }
  23.  
  24. function dbgOut() {
  25.         [ ${DODEBUG} -eq 0 ] && return;
  26.         printf "%s " $( date +"%R:%S" )
  27.         echo "${1}"
  28. }
  29.  
  30. function cleanUp() {
  31.         rm /tmp/plexflex.$$ 2>&1 > /dev/null
  32.         [ -z "${1}" ] && exit 0
  33.         exit ${1}
  34. }
  35.  
  36. function urlencode() {
  37.     # urlencode <string>
  38.     old_lc_collate=$LC_COLLATE
  39.     LC_COLLATE=C
  40.    
  41.     local length="${#1}"
  42.     for (( i = 0; i < length; i++ )); do
  43.         local c="${1:i:1}"
  44.         case $c in
  45.             [a-zA-Z0-9.~_-]) printf "$c" ;;
  46.             *) printf '%%%02X' "'$c" ;;
  47.         esac
  48.     done
  49.    
  50.     LC_COLLATE=$old_lc_collate
  51. }
  52.  
  53. # cleanup if interrupted
  54. trap cleanUp SIGINT SIGTERM
  55.  
  56. # check if necessary utilites are available
  57. [ ! -x $( which curl ) ] && { echo "curl not installed/in PATH, fix it!"; cleanUp 1; }
  58. [ ! -x $( which xmllint ) ] && { echo "xmllint not installed/in PATH, fix it!"; cleanUp 1; }
  59. [ ! -x $( which jq ) ] && { echo "jq not installed/in PATH, fix it!"; cleanUp 1; }
  60.  
  61. [ $# -lt 3 ] && { echo "${0} <SHOW> <SEASON> <EPISODE> [-d]"; cleanUp 1; }
  62. SRCHNEEDLE="${1}"
  63. SRCHSEASON=${2}
  64. SRCHEPISODE=${3}
  65. [ "${4}" == "-d" ] && DODEBUG=1
  66. # encode url because it has to be a GET request -.-
  67. ENCNEEDLE=$( urlencode "${SRCHNEEDLE}" )
  68.  
  69. # fetch result
  70. curl -s -o /tmp/plexflex.$$ -b --ssl-no-revoke -k "https://${PLEXHOST}/search?query=${ENCNEEDLE}&X-Plex-Token=${PLEXTOKEN}"
  71. [ $? -ne 0 ] && { echo "Search request to ${PLEXHOST} failed, stopping."; cleanUp 1; }
  72.  
  73. # check if we have only one result ( no decisions are made.. )
  74. VALCHK=$( xmllint --xpath "count (/MediaContainer/Directory)" /tmp/plexflex.$$ )
  75. [ ${VALCHK} -ne 1 ] && { echo "${SRCHNEEDLE} is not a unique name, exiting."; cleanUp 1; }
  76.  
  77. # get path to XML where all the juicy seasons are stored..
  78. PLEXSEASONS=$( xmllint --xpath "string(/MediaContainer/Directory/@key)" /tmp/plexflex.$$ )
  79. [ -z "${PLEXSEASONS}" ] && { echo "xmllint query for seasons overview failed, exiting."; cleanUp 1; }
  80.  
  81. # query plex
  82. curl -s -o /tmp/plexflex.$$ -b --ssl-no-revoke -k "https://${PLEXHOST}${PLEXSEASONS}?X-Plex-Token=${PLEXTOKEN}"
  83. [ $? -ne 0 ] && { echo "Seasons-overview query to ${PLEXHOST} failed, stopping."; cleanUp 1; }
  84.  
  85. # do the same stuff with seasons..first get key to episodes for the wanted season
  86. PLEXEPISODES=$( xmllint --xpath "string(/MediaContainer/Directory[@index=${SRCHSEASON}]/@key)" /tmp/plexflex.$$ )
  87. [ -z "${PLEXEPISODES}" ] && { echo "xmllint query for episode overview for Season ${SRCHSEASON} failed, exiting."; cleanUp 1; }
  88.  
  89. # fetch episode overview
  90. curl -s -o /tmp/plexflex.$$ -b --ssl-no-revoke -k "https://${PLEXHOST}${PLEXEPISODES}?X-Plex-Token=${PLEXTOKEN}"
  91. [ $? -ne 0 ] && { echo "Episode-overview query to ${PLEXHOST} failed, stopping."; cleanUp 1; }
  92.  
  93. # now get the download link to the wanted episode
  94. PLEXDOWNLOAD=$( xmllint --xpath "string(/MediaContainer/Video[@index=${SRCHEPISODE}]/Media/Part/@key)" /tmp/plexflex.$$ )
  95. [ -z "${PLEXDOWNLOAD}" ] && { echo "xmllint query for download link failed, exiting."; cleanUp 1; }
  96. # get the size and the extension of the file
  97. DOWNLOADSIZE=$( xmllint --xpath "string(/MediaContainer/Video[@index=${SRCHEPISODE}]/Media/Part/@size)" /tmp/plexflex.$$ )
  98. SRCHEXTENSION=$( echo "${PLEXDOWNLOAD#*.}" )
  99.  
  100. # concernate the final download path/name
  101. LOCDOWNLOAD=$( printf "${DESTPATH}/${SRCHNEEDLE} S%.2iE%.2i.%s" ${SRCHSEASON} ${SRCHEPISODE} ${SRCHEXTENSION} )
  102.  
  103. # start download
  104. curl -s -o "${LOCDOWNLOAD}" -b --ssl-no-revoke -k "https://${PLEXHOST}${PLEXDOWNLOAD}?download=1&X-Plex-Token=${PLEXTOKEN}"
  105. [ $? -ne 0 ] && { echo "Download failed (${PLEXDOWNLOAD})"; cleanUp 1 }
  106.  
  107. # get destination file size
  108. DLSIZE=$( stat "${LOCDOWNLOAD}"  | grep Size: | awk -F\  '{ print $2 }' )
  109.  
  110. [ "${DLSIZE}" != "${DOWNLOADSIZE}" ] && { echo "Downloaded size differs from size in metadata!" }
  111.  
  112. # print destination name, for sonarr update purposes
  113. echo "${LOCDOWNLOAD}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement