recentlyUnwatched.sh 3.0 KB

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