recentlyUnwatched.sh 2.5 KB

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