Ver código fonte

Import the sync scripts

Zachary Kotlarek 12 anos atrás
pai
commit
35432ff047
3 arquivos alterados com 264 adições e 0 exclusões
  1. 83 0
      sync/recentlyUnwatched.sh
  2. 90 0
      sync/sync.sh
  3. 91 0
      sync/syncUnwatched.sh

+ 83 - 0
sync/recentlyUnwatched.sh

@@ -0,0 +1,83 @@
+#!/bin/bash
+
+HOST="http://pms.uberzach.com:32400"
+CURL_OPTS=(--silent --connect-timeout 5 --max-time 30)
+NUM_SERIES=15
+NUM_EPISODES=6
+MAX_RESULTS=100
+
+# Select a configuration mode
+URL1="${HOST}/library/sections/2/onDeck/"
+URL2_POST="children/allLeaves?unwatched=1"
+if echo "${1}" | grep -iq Movie; then
+	URL1="${HOST}/library/sections/1/recentlyAdded/"
+	URL2_POST=""
+fi
+
+# Find recent items
+ITEMS="`curl ${CURL_OPTS[@]} "${URL1}" | \
+	grep '<Video ' | \
+	head -n "${MAX_RESULTS}" | \
+	sed 's%^.* ratingKey="\([0-9]*\)".*$%\1%'`"
+
+# Resolve the series item
+SERIES=""
+IFS=$'\n'
+for i in $ITEMS; do
+	ITEM="`curl ${CURL_OPTS[@]} "${HOST}/library/metadata/${i}/" | \
+		grep '<Video ' | \
+		head -n "${MAX_RESULTS}"`"
+	if echo "${ITEM}" | grep -q 'type="episode"'; then
+		KEY="`echo "${ITEM}" | \
+			sed 's%^.* parentRatingKey="\([0-9]*\)".*$%\1%'`"
+	elif  echo "${ITEM}" | grep -q 'type="movie"'; then
+		KEY="`echo "${ITEM}" | \
+			grep -v 'lastViewedAt="' | \
+			sed 's%^.* ratingKey="\([0-9]*\)".*$%\1%'`"
+	else
+		echo "Unknown type: ${ITEM}" 1>&2
+		exit -1
+	fi
+
+	if [ -n "${KEY}" ]; then
+		SERIES="${SERIES}${KEY}"$'\n'
+	fi
+done
+
+SERIES_COUNT=0
+IFS=$'\n'
+for i in $SERIES; do
+	SERIES_COUNT=$(( $SERIES_COUNT + 1 ))
+	FILES="`curl ${CURL_OPTS[@]} "${HOST}/library/metadata/${i}/${URL2_POST}" | \
+		grep '<Part ' | \
+		head -n "${MAX_RESULTS}" | \
+		sed 's%^.*file="\([^\"]*\)".*$%\1%' | \
+		sed "s%^.*/media/%%"`"
+
+	SEASON_COUNTS=()
+	IFS=$'\n'
+	for j in $FILES; do
+		# Find the season number and increment the count
+		# Account no-season items (i.e. movies) separately
+		SEASON="`echo "${j}" | sed 's%^.*/Season \([0-9]*\)/.*$%\1%'`"
+		if ! echo "${SEASON}" | grep -q '^[0-9]*$'; then
+			SEASON=-1
+		else
+			SEASON_COUNTS[$SEASON]=$(( ${SEASON_COUNTS[$SEASON]} + 1 ))
+		fi
+
+		# Limit the number of series (or movies)
+		if [ $SERIES_COUNT -gt $NUM_SERIES ]; then
+			continue
+		# Only output NUM_EPISODES files per season
+		elif [ $SEASON -ge 0 ] && [ ${SEASON_COUNTS[$SEASON]} -gt $NUM_EPISODES ]; then
+			continue
+		fi
+
+		# Print the name, decoding both hex encoding and XML entities
+		echo "${j}" | \
+			sed 's/%C3%A9/é/' | \
+			perl -pe 's/%([0-9a-f]{2})/sprintf("%s", pack("H2",$1))/eig' | \
+			perl -e 'use XML::Entities; while (<>) { print XML::Entities::decode('all', $_); }'
+	done
+done

+ 90 - 0
sync/sync.sh

@@ -0,0 +1,90 @@
+#!/bin/bash
+
+# Target path
+TARGET="Heady"
+
+# Establish a base of operations
+BASE_DIR="`~/bin/video/mediaPath`"
+
+# We need an input directory
+SUB_DIR="${1}"
+
+# Allow absolute paths, or those relative to the media path
+if [ "`echo "${SUB_DIR}" | head -c 1`" == '/' ]; then
+	SUB_DIR="`echo "${SUB_DIR}" | sed "s%^${BASE_DIR}/%%"`"
+fi
+
+# Allow single-file use
+if [ -f "${BASE_DIR}/${SUB_DIR}" ]; then
+	FILE="`basename "${SUB_DIR}"`"
+	SUB_DIR="`dirname "${SUB_DIR}"`"
+fi
+
+# Construct the final paths
+IN_DIR="${BASE_DIR}/${SUB_DIR}"
+OUT_DIR="${BASE_DIR}/${TARGET}/${SUB_DIR}"
+
+# Sanity check
+if [ ! -d "${IN_DIR}" ]; then
+	echo "Invalid input directory: ${IN_DIR}" 1>&2
+	exit 1
+fi
+
+# Remember where we came from
+START_DIR="`pwd`"
+
+# Go home
+cd "${IN_DIR}"
+
+# Ensure the output directory exists
+if [ ! -d "${OUT_DIR}" ]; then
+	mkdir -p "${OUT_DIR}"
+fi
+
+# Export options for the encode script
+export OUT_DIR
+export MOBILE=1
+
+# Handle single-file, whole-folder, or recursive use
+if [ -n "${FILE}" ]; then
+	FILES="${FILE}"
+else
+	FILES="`ls [0-9]*\ -\ *.* 2>/dev/null`"
+fi
+
+# Allow recursive use for seasons
+for season in Season\ [0-9]*; do
+	if [ ! -d "${season}" ]; then
+		continue
+	fi
+
+	# Recurse for the season directory
+	CMD="${0}"
+	if [ "`echo "${CMD}" | head -c 1`" != '/' ]; then
+		CMD="${START_DIR}/${0}"
+	fi
+	"${CMD}" "${IN_DIR}/${season}"
+
+	# Disable the non-recursive case processing
+	FILES=""
+done
+
+# Encode any missing files
+IFS=$'\n'
+for infile in $FILES; do
+	# Skip non-files
+	if [ ! -f "${infile}" ]; then
+		continue
+	fi
+
+	# Don't get hung up on file extensions
+	nobase="`echo "${infile}" | sed 's%\....$%%'`"
+
+	# Skip files that exist
+	if ls "${OUT_DIR}/${nobase}".* >/dev/null 2>&1; then
+		continue
+	fi
+
+	# Encode things that do not
+	~/bin/video/encode.pl "${IN_DIR}/${infile}"
+done

+ 91 - 0
sync/syncUnwatched.sh

@@ -0,0 +1,91 @@
+#!/bin/bash
+
+# Parameters
+TARGET="Heady"
+BASE_DIR="`~/bin/video/mediaPath`/${TARGET}"
+if [ -n "${DEBUG}" ]; then
+	DEBUG=1
+else
+	DEBUG=0
+fi
+
+# Command-line options
+DIR="TV"
+if echo "${1}" | grep -qi "Movie"; then
+	DIR="Movies"
+fi
+
+# Ensure we have a valid TMPDIR
+if [ ! -d "${TMPDIR}" ]; then
+	TMPDIR="`getconf DARWIN_USER_TEMP_DIR 2>/dev/null`"
+	if [ ! -d "${TMPDIR}" ]; then
+		TMPDIR="/var/tmp"
+	fi
+	if [ ! -d "${TMPDIR}" ]; then
+		TMPDIR="/tmp"
+	fi
+fi
+
+# Check and write the run file
+PID_FILE="${TMPDIR}/syncUnwatched.pid"
+if [ -f "${PID_FILE}" ]; then
+	PID=`cat "${PID_FILE}"`
+	if ps auwx | grep -v grep | grep "`basename "${0}"`" | grep -q "${PID}"; then
+		if [ $DEBUG -gt 0 ]; then
+			echo "Already running: ${PID}" 1>&2
+			exit -1
+		else
+			exit 0
+		fi
+	fi
+fi
+echo $$ > "${PID_FILE}"
+
+# Construct directories
+DEST_DIR="${BASE_DIR}/${DIR}"
+
+# Figure out what we want
+FILES="`~/bin/video/sync/recentlyUnwatched.sh "${1}"`"
+
+# Ensure the input is sane
+if [ `echo "${FILES}" | wc -l` -lt 5 ]; then
+	echo -e "Invalid sync file list:\n${FILE}" 1>&2
+	exit -2
+fi
+
+# Figure out what we already have
+OLD_FILES="`find "${DEST_DIR}" -type f | sed "s%^${BASE_DIR}/%%"`"
+
+# Allow special files
+EXTRAS="`cat ~/.sync_extras`"
+IFS=$'\n'
+for i in $EXTRAS; do
+	OLD_FILES="`echo "${OLD_FILES}" | grep -v "^${i}"`"
+done
+
+# Encode and allow any expected files
+IFS=$'\n'
+for i in $FILES; do
+	if [ $DEBUG -eq 0 ]; then
+		~/bin/video/sync/sync.sh "${i}"
+	else
+		echo "Would encode: ${i}"
+	fi
+
+	nobase="`echo "${i}" | sed 's%\....$%%'`"
+	OLD_FILES="`echo "${OLD_FILES}" | grep -v "${nobase}"`"
+done
+
+# Delete anything leftover
+IFS=$'\n'
+for i in $OLD_FILES; do
+	if [ $DEBUG -eq 0 ]; then
+		rm -f "${BASE_DIR}/${i}"
+	else
+		echo "Would delete: ${BASE_DIR}/${i}"
+	fi
+done
+find "${DEST_DIR}" -type d -empty -delete
+
+# Cleanup
+rm -f "${PID_FILE}"