recentlyUnwatched.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash
  2. HOST="http://beddy.uberzach.com:32400"
  3. NUM_SERIES=10
  4. NUM_EPISODES=5
  5. # Select a configuration mode
  6. URL1="${HOST}/library/sections/2/recentlyViewedShows/"
  7. URL2_POST="allLeaves?unwatched=1"
  8. ELEMENT="Directory"
  9. MOVIES=0
  10. if echo "${1}" | grep -iq Movie; then
  11. MOVIES=1
  12. URL1="${HOST}/library/sections/1/recentlyAdded/"
  13. URL2_POST=""
  14. ELEMENT="Video"
  15. fi
  16. SERIES="`curl --silent "${URL1}" | \
  17. grep "<${ELEMENT} " | \
  18. sed 's%^.*key="/library/metadata/\([0-9]*\).*$%\1%'`"
  19. # Movies need an intermediate step
  20. if [ "${MOVIES}" -gt 0 ]; then
  21. IFS=$'\n'
  22. for i in $SERIES; do
  23. MOVIE="`curl --silent "${HOST}/library/metadata/${i}/" | \
  24. grep "<${ELEMENT} " | \
  25. grep -v 'lastViewedAt="' | \
  26. sed 's%^.*key="/library/metadata/\([0-9]*\).*$%\1%'`"
  27. if [ -n "${MOVIE}" ]; then
  28. UNWATCHED="${UNWATCHED}${MOVIE}"$'\n'
  29. fi
  30. done
  31. SERIES="${UNWATCHED}"
  32. fi
  33. SERIES_COUNT=0
  34. IFS=$'\n'
  35. for i in $SERIES; do
  36. SERIES_COUNT=$(( $SERIES_COUNT + 1 ))
  37. FILES="`curl --silent "${HOST}/library/metadata/${i}/${URL2_POST}" | \
  38. grep '<Part ' | \
  39. sed 's%^.*file="\([^\"]*\)".*$%\1%' | \
  40. sed "s%^.*/media/%%"`"
  41. SEASON_COUNTS=()
  42. IFS=$'\n'
  43. for j in $FILES; do
  44. # Find the season number and increment the count
  45. # Account no-season items (i.e. movies) separately
  46. SEASON="`echo "${j}" | sed 's%^.*/Season \([0-9]*\)/.*$%\1%'`"
  47. if ! echo "${SEASON}" | grep -q '^[0-9]*$'; then
  48. SEASON=-1
  49. else
  50. SEASON_COUNTS[$SEASON]=$(( ${SEASON_COUNTS[$SEASON]} + 1 ))
  51. fi
  52. # Limit the number of series (or movies)
  53. if [ $SEASON -lt 0 ]; then
  54. if [ $SERIES_COUNT -gt $NUM_SERIES ]; then
  55. continue
  56. fi
  57. # Only output NUM_EPISODES files per season
  58. # This allows discontinous output, but that's desirable compared to only getting season 0
  59. elif [ ${SEASON_COUNTS[$SEASON]} -gt $NUM_EPISODES ]; then
  60. continue
  61. fi
  62. # If we're still around, this is an item we want
  63. echo "${j}" | perl -pe 's/%([0-9a-f]{2})/sprintf("%s", pack("H2",$1))/eig'
  64. done
  65. done