help me optimize my pp script?

Come up with a useful post-processing script? Share it here!
Post Reply
drawde
Newbie
Newbie
Posts: 11
Joined: January 17th, 2012, 6:34 pm

help me optimize my pp script?

Post by drawde »

been seeing a lot of times where my downloads aren't coming with nfo's for some reason. not all the time but enough to bother me.

i've done a little bit of coding in my day but i'm very much out of practice, and bash scripting is not something i've ever really done. this is what i'm working on now. it's sloppy and took me a couple hours but it works (if certain conditions are met).

Code: Select all

#!/bin/bash

if [ -f $1/*.nfo ]; then
	echo "Folder already has an nfo."
	# folder has a nfo file, (hopefully with imdb url).. skip to end
else
	echo "" > movie.txt
	var1=`echo ${3%1080p*} | sed 's/\./+/g;s/2013//g;'`
	# clean up release name
	echo " "
	echo "Searching IMDBAPI.com using:" $var1
	echo " "
	curl http://www.imdbapi.com/?t=$var1 >> movie.txt
	# save api request to movie.txt
	imdb="http://www.imdb.com/title/"
	var2=`echo $imdb ``sed -e 's/^.*"imdbID":"\([^"]*\)".*$/\1/' movie.txt```
	imdburl=`echo $var2 | tr -d ' '`
	# find imdb id in movie.txt, append imdb prefix, and trim spaces

		len=$(echo ${#imdburl})
		if [ $len -eq 35 ]; then
		# assuming all imdb tt's are 7 digits, if more or less, imdbapi did not find good results
			echo $imdburl > movie.nfo
			nfogoes=`echo $1 "/movie.nfo" | tr -d ' '`
			# full path + /movie.nfo to be used later
			echo " "
			echo "Found IMDB URL:" $imdburl
			echo " "
			echo "Target location:" $nfogoes
			echo " "
			mv movie.nfo $nfogoes
			echo "Moved movie.nfo to $1"
			# move nfo to directory
			echo " "
		else
		# if we get bad data from imdbapi..
			echo "IMDB URL not found."
			echo -e "Subject: No suitable NFO found: $3\n\n$1" | ssmtp -d root
			# will email me so i remember to fix manually. imdbapi can be kinda picky.
		fi
fi
echo " "
echo "Sending JSON to update XBMC Library.."
echo " "
echo "curl -s -H \"Content-Type: application/json\" -u xbmc:xbmc -X POST -d '{\"jsonrpc\": \"2.0\", \"method\": \"VideoLibrary.Scan\", \"params\":{\"directory\":\"smb://tower/x264/rls/\"}, \"id\": \"scan\"}' http://tower:9091/jsonrpc" | sed 's_rls_'"$3"'_g' | sh
# update xbmc library for this directory
echo " "
echo " "
based on the above and my quick testing tonight, it works if:

[*] releases are named as "1080p" and doesn't have extra stuff like DOCU, PROPER, STV, etc. before the "1080p"
[*] release name doesn't have a year, or a year other than the one i specify (which is 2013 above). couldn't find a better way to get rid of NNNN.

there's a couple other things on my list that i need this to do but for now i'd like a better way to clean up the release name. any thoughts? all i have right now is delete everything including and after 1080p and remove the year (if it's 2013).
unRAID 4.7 Pro: ASUS M4A88T-M | AMD Sempron 145 Sargas @ 2.8GHz | 1GB DDR3 SDRAM 1066 (PC3 8500) RAM | Antec NEO ECO 620C 620W PSU | NETGEAR gigE
drawde
Newbie
Newbie
Posts: 11
Joined: January 17th, 2012, 6:34 pm

Re: help me optimize my pp script?

Post by drawde »

For anyone that cares, this is more or less a final version of my script

Code: Select all

#!/bin/bash

fullpath=$1
name=$3

if [ -f $fullpath/*.nfo ]; then
	echo " "
	echo "Folder already has an nfo."
else

	echo "" > movie.txt
	var1=`echo ${name%?1080*} | sed 's/proper//gI;s/nfofix//gI;s/subfix//gI;s/limited//gI;s/readnfo//gI' | tr " " + | tr . + | sed 's/++//g'`
	year=`echo $var1 | egrep -o [0-9]{4}`
	clean=`echo $var1 | sed 's/[0-9][0-9][0-9][0-9]//g;'`
	echo " "
	echo "Searching IMDBAPI.com using:" $clean
	echo " "
	curl http://www.imdbapi.com/?t=$clean"&y="$year >> movie.txt
	var2=`echo ``sed -e 's/^.*"imdbID":"\([^"]*\)".*$/\1/' movie.txt```
	imdburl=`echo "http://www.imdb.com/title/" $var2 | tr -d ' '`

		len=$(echo ${#imdburl})
		if [ $len -eq 35 ]; then
			echo $imdburl > movie.nfo
			nfogoes=`echo $fullpath"/movie.nfo"`
			echo " "
			echo "Found IMDB URL:" $imdburl
			echo " "
			echo "Target location:" $nfogoes
			echo " "
			mv movie.nfo "$nfogoes"
			echo "Moved movie.nfo to $fullpath"
			echo " "
		else
			echo "IMDB URL not found."
			echo -e "Subject: No suitable NFO found: $name\n\n$fullpath" | ssmtp -d root
		fi
fi
echo " "
echo "Sending JSON to update XBMC Library.."
echo " "
echo "curl -s -H \"Content-Type: application/json\" -u xbmc:xbmc -X POST -d '{\"jsonrpc\": \"2.0\", \"method\": \"VideoLibrary.Scan\", \"params\":{\"directory\":\"smb://tower/x264/rls/\"}, \"id\": \"scan\"}' http://tower:9091/jsonrpc" | sed 's_rls_'"$name"'_g' | sh
echo " "
echo " "
what it does:

[*] if it finds an nfo, nothing.

otherwise:

[*] will grab movie name and year (if there) and dl json data from imdbapi.com and pull out the imdb URL and save that to nfo.
[*] will put the nfo in $1 (full dl path)
[*] update library on just that folder in xbmc

this is specifically tailored to my needs but maybe this can help others out there maybe trying to do something similar.
unRAID 4.7 Pro: ASUS M4A88T-M | AMD Sempron 145 Sargas @ 2.8GHz | 1GB DDR3 SDRAM 1066 (PC3 8500) RAM | Antec NEO ECO 620C 620W PSU | NETGEAR gigE
Post Reply