recentlyUnwatched.sh 2.6 KB

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