recentlyUnwatched.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/bash
  2. IFS=''
  3. CURL_OPTS=(--silent --connect-timeout 5 --max-time 30)
  4. NUM_SERIES=18
  5. NUM_EPISODES=4
  6. MAX_RESULTS=100
  7. # Construct URL components from the environment
  8. if [ -z "${PMS_URL}" ]; then
  9. if [ -z "${PMS_HOST}" ]; then
  10. PMS_HOST="localhost"
  11. fi
  12. if [ -z "${PMS_PORT}" ]; then
  13. PMS_PORT=32400
  14. fi
  15. PMS_URL="http://${PMS_HOST}:${PMS_PORT}"
  16. fi
  17. if [ -z "${PMS_TOKEN}" ]; then
  18. echo "No PMS_TOKEN provided" 1>&2
  19. fi
  20. CURL_OPTS+=(-H "X-Plex-Token: ${PMS_TOKEN}")
  21. # Select a configuration mode
  22. URL1="${PMS_URL}/library/sections/2/onDeck/"
  23. URL2_POST="children/allLeaves?unwatched=1"
  24. if echo "${1}" | grep -iq Movie; then
  25. URL1="${PMS_URL}/library/sections/1/recentlyAdded/"
  26. URL2_POST=""
  27. elif echo "${1}" | grep -iq YouTube; then
  28. URL1="${PMS_URL}/library/sections/16/onDeck/"
  29. NUM_EPISODES=$(( $NUM_EPISODES * 3 ))
  30. NUM_SERIES=$(( $NUM_SERIES * 2 ))
  31. fi
  32. # Find recent items
  33. ITEMS="`curl ${CURL_OPTS[@]} "${URL1}" | \
  34. grep '<Video ' | \
  35. head -n "${MAX_RESULTS}" | \
  36. sed 's%^.* ratingKey="\([0-9]*\)".*$%\1%'`"
  37. # Resolve the series item
  38. SERIES=""
  39. IFS=$'\n'
  40. for i in $ITEMS; do
  41. ITEM="`curl ${CURL_OPTS[@]} "${PMS_URL}/library/metadata/${i}/" | \
  42. grep '<Video ' | \
  43. head -n "${MAX_RESULTS}"`"
  44. if echo "${ITEM}" | grep -q 'type="episode"'; then
  45. KEY="`echo "${ITEM}" | \
  46. sed 's%^.* parentRatingKey="\([0-9]*\)".*$%\1%'`"
  47. elif echo "${ITEM}" | grep -q 'type="movie"'; then
  48. KEY="`echo "${ITEM}" | \
  49. grep -v 'lastViewedAt="' | \
  50. sed 's%^.* ratingKey="\([0-9]*\)".*$%\1%'`"
  51. else
  52. echo "Unknown type: ${ITEM}" 1>&2
  53. exit -1
  54. fi
  55. if [ -n "${KEY}" ]; then
  56. SERIES="${SERIES}${KEY}"$'\n'
  57. fi
  58. done
  59. SERIES_COUNT=0
  60. IFS=$'\n'
  61. for i in $SERIES; do
  62. SERIES_COUNT=$(( $SERIES_COUNT + 1 ))
  63. FILES="`curl ${CURL_OPTS[@]} "${PMS_URL}/library/metadata/${i}/${URL2_POST}" 2>/dev/null | \
  64. grep '<Part ' | \
  65. head -n "${MAX_RESULTS}" | \
  66. sed 's%^.*file="\([^\"]*\)".*$%\1%' | \
  67. sed "s%^.*/media/%%"`"
  68. SEASON_COUNTS=()
  69. IFS=$'\n'
  70. for j in $FILES; do
  71. # Find the season number and increment the count
  72. # I know the metadata has the season number in it, but this is less work
  73. # Account no-season items (i.e. movies) separately
  74. SEASON="`echo "${j}" | sed 's%^.*/Season \([0-9]*\)/.*$%\1%'`"
  75. if ! echo "${SEASON}" | grep -q '^[0-9]*$'; then
  76. SEASON="`echo "${j}" | sed 's%^.*/S\([0-9]*\)E[0-9].*$%\1%'`"
  77. fi
  78. if ! echo "${SEASON}" | grep -q '^[0-9]*$'; then
  79. SEASON=-1
  80. else
  81. SEASON_COUNTS[$SEASON]=$(( ${SEASON_COUNTS[$SEASON]} + 1 ))
  82. fi
  83. # Limit the number of series (or movies)
  84. if [ $SERIES_COUNT -gt $NUM_SERIES ]; then
  85. continue
  86. # Only output NUM_EPISODES files per season
  87. elif [ $SEASON -ge 0 ] && [ ${SEASON_COUNTS[$SEASON]} -gt $NUM_EPISODES ]; then
  88. continue
  89. fi
  90. # Print the name, decoding both hex encoding and XML entities, and fixing SMB mapping
  91. echo "${j}" | \
  92. sed 's/%C3%A9/é/' | \
  93. perl -pe 's/%([0-9a-f]{2})/sprintf("%s", pack("H2",$1))/eig' | \
  94. perl -e 'use XML::Entities; while (<>) { print XML::Entities::decode('all', $_); }'
  95. done
  96. done