recentlyUnwatched.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash
  2. HOST="http://beddy.uberzach.com:32400"
  3. # Select a configuration mode
  4. URL1="${HOST}/library/sections/2/recentlyViewedShows/"
  5. URL2_POST="allLeaves?unwatched=1"
  6. ELEMENT="Directory"
  7. NUM_EPISODES=5
  8. MOVIES=0
  9. if echo "${1}" | grep -iq Movie; then
  10. MOVIES=1
  11. URL1="${HOST}/library/sections/1/recentlyAdded/"
  12. URL2_POST=""
  13. ELEMENT="Video"
  14. NUM_EPISODES=10
  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. nscount=0
  34. IFS=$'\n'
  35. for i in $SERIES; do
  36. FILES="`curl --silent "${HOST}/library/metadata/${i}/${URL2_POST}" | \
  37. grep '<Part ' | \
  38. sed 's%^.*file="\([^\"]*\)".*$%\1%' | \
  39. sed "s%^.*/media/%%"`"
  40. scounts=()
  41. IFS=$'\n'
  42. for j in $FILES; do
  43. # Find the season number and increment the count
  44. # Account no-season items (i.e. movies) separately
  45. season="`echo "${j}" | sed 's%^.*/Season \([0-9]*\)/.*$%\1%'`"
  46. if ! echo "${season}" | grep -q '^[0-9]*$'; then
  47. season=-1
  48. nscount=$(( $nscount + 1 ))
  49. else
  50. scounts[$season]=$(( ${scounts[$season]} + 1 ))
  51. fi
  52. # For no-season items, output only NUM_EPISODES total
  53. if [ $season -lt 0 ]; then
  54. if [ $nscount -gt $NUM_EPISODES ]; 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 [ ${scounts[$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