recentlyUnwatched.sh 2.1 KB

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