Mautobu

Scrape Plex Movies

Feb 16th, 2017
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.11 KB | None | 0 0
  1. #!/bin/bash
  2. # V2. Now supports TV shows and Movies.
  3. #
  4. # User token. Take from the URL you have when navigating to the plex server in a browser.
  5. token=1111111111111111111111111111111111111111
  6.  
  7. # Plex URL
  8. host=http://hostsv.megaspeednet.com
  9.  
  10. # Plex server port, default is 32400
  11. port=32400
  12.  
  13. # The media directory you choose to drop scraped media into
  14. destination=/path/to/directory
  15.  
  16. if [ ! -d $destination ]; then mkdir -p $destination; fi
  17. if [ -f .temp1.xml ]; then rm .temp1.xml; fi
  18. if [ -f .temp2.xml ]; then rm .temp2.xml; fi
  19. if [ -f .tempshows.xml ]; then rm .tempshows.xml; fi
  20. if [ -f .tempshows1.xml ]; then rm .tempshows1.xml; fi
  21.  
  22. wget -qO- $host:$port/library/sections?X-Plex-Token=$token > .temp1.xml
  23. itemCount=`xmllint --xpath 'count(//MediaContainer/Directory)' .temp1.xml`
  24.  
  25. movie=()
  26. show=()
  27.  
  28. for (( y=0; y <= $itemCount; y++ )); do
  29.     sectionkey[$y]="$(xmllint --xpath '//MediaContainer/Directory['$y']/@key' .temp1.xml)"
  30.     sectiontype[$y]="$(xmllint --xpath '//MediaContainer/Directory['$y']/@type' .temp1.xml)"
  31.     sectionkey[$y]=${sectionkey[$y]:6:-1}
  32.     sectiontype[$y]=${sectiontype[$y]:7:-1}
  33.     if [[ ${sectiontype[$y]} == "movie" ]] ; then movie+=(${sectionkey[$y]}); fi
  34.     if [[ ${sectiontype[$y]} == "show" ]] ; then show+=(${sectionkey[$y]}); fi
  35. done
  36.  
  37.  
  38. for (( i=1; i <= $itemsCount; i++ )); do
  39.     tt[$i]="$(xmllint --xpath '//MediaContainer/Video['$i']/Media/Part/@key' .temp.xml)"
  40.     tg[$i]="$(xmllint --xpath '//MediaContainer/Video['$i']/@title' .temp.xml)"
  41.         name=${tg[$i]:8:-1}
  42.         name=${name// /_}
  43.         name=${name//:/_}
  44.         name=${name//!/}
  45.         name=${name//&amp;/&}
  46.         ext=`echo "${tt[$i]}" | cut -d'.' -f2`
  47.         if [ ! -f "$destination$name.${ext:0:-1}" ]; then
  48.                 wget --limit-rate $limit -O $destination$name.${ext:0:-1} "$host:$port${tt[$i]:6:-1}?download=1&X-Plex-Token=$token"
  49.         fi
  50. done
  51. rm .temp.xml
  52. done
  53.  
  54.  
  55. for f in "${show[@]}"; do
  56. wget -qO- $host:$port/library/sections/$f/all?X-Plex-Token=$token >> .tempshows.xml
  57. hh=`xmllint --xpath 'count(//MediaContainer/Directory)' .tempshows.xml`
  58. declare -a qq=( )
  59. for (( i=1; i <= $hh; i++ )); do
  60. qq[$i]="$(xmllint --xpath '//MediaContainer/Directory['$i']/@key' .tempshows.xml)"
  61. wget -qO- $host:$port${qq[$i]:6:-9}allLeaves?X-Plex-Token=$token >> .tempshows1.xml
  62. declare -a tt=( )
  63. declare -a tg=( )
  64. jj=`xmllint --xpath 'count(//MediaContainer/Video)' .tempshows1.xml`
  65. for (( uy=1; uy <= $jj; uy++ )); do
  66. tt[$uy]="$(xmllint --xpath '//MediaContainer/Video['$uy']/Media/Part/@key' .tempshows1.xml)"
  67. tg[$uy]="$(xmllint --xpath '//MediaContainer/Video['$uy']/@title' .tempshows1.xml)"
  68. season[$uy]="$(xmllint --xpath '//MediaContainer/Video['$uy']/@parentIndex' .tempshows1.xml)"
  69. episode[$uy]="$(xmllint --xpath '//MediaContainer/Video['$uy']/@index' .tempshows1.xml)"
  70. showname="$(xmllint --xpath '//MediaContainer/@title2' .tempshows1.xml)"
  71. season[$uy]=${season[$uy]:14:-1}
  72. episode[$uy]=${episode[$uy]:8:-1}
  73. showname=${showname:9:-1}
  74. showname=${showname// /_}
  75. showname=${showname//:/_}
  76. showname=${showname//!/}
  77. showname=${showname//&amp;/&}
  78. name=${tg[$uy]:8:-1}
  79. name=${name// /_}
  80. name=${name//:/_}
  81. name=${name//!/}
  82. name=${name//&amp;/&}
  83. name=${showname}_${season[$uy]}x${episode[$uy]}_$name
  84. ext=`echo "${tt[$uy]}" | cut -d'.' -f2`
  85. if [ ! -d $destination$showname ]; then mkdir -p $destination$showname; fi
  86. if [ ! -f "$destination$showname/$name.${ext:0:-1}" ]; then
  87. wget --limit-rate $limit -O $destination$showname/$name.${ext:0:-1} "$host:$port${tt[$uy]:6:-1}?download=1&X-Plex-Token=$token"
  88. fi
  89. done
  90. rm .tempshows1.xml
  91. done
  92. rm .tempshows.xml
  93. done
Add Comment
Please, Sign In to add comment