recentlyUnwatched.sh 2.2 KB

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