Skip to content

Instantly share code, notes, and snippets.

@dewey
Created February 17, 2014 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dewey/9042882 to your computer and use it in GitHub Desktop.
Save dewey/9042882 to your computer and use it in GitHub Desktop.
Randomly pick a movie from a given directory based on various optional input parameters.
#!/bin/bash
#title : popcorntime.sh
#description : This script picks a random movie from a given directory based on various input parameters.
#author : dewey
#date : 17.02.2014
# Initialise some variables
quality=
extension=
medium=
path=
needles=()
SUCCESS="You should watch: "
function printhelp
{
echo "Usage: popcorntime /path/to/movies"
echo "OR popcorntime [[-p | --path] (mandatory) | [-q | --quality] [-e | --extension] [-m | --medium]]"
echo "OR popcorntime [-h | --help]"
}
# If there are !1 arguments parse the arguments and do things...
if [ $# -ne 1 ]
then
while [ "$1" != "" ]; do
case $1 in
-q | --quality ) shift
quality=$1
;;
-e | --extension ) shift
extension=$1
;;
-m | --medium ) shift
medium=$1
;;
-p | --path ) shift
path=$1
;;
-h | --help ) usage
exit
;;
* ) printhelp
exit 1
esac
shift
done
# If the arguments are requested add needle to our needles array
if [[ ! "$quality" == "" ]]
then
needles+=(-iname "*$quality*")
fi
if [[ ! "$extension" == "" ]]
then
needles+=(-iname "*$extension*")
fi
if [[ ! "$medium" == "" ]]
then
needles+=(-iname "*$medium*")
fi
# Add all the needles to our find command
echo $SUCCESS
find "$path" "${needles[@]}" -type f -exec basename {} \; | gshuf -n 1
# If there's only one argument (The path to the movie directory), just pick one random movie.
else
if [[ -d $1 ]]
then
echo $SUCCESS
find "$1" -type f -exec basename {} \; | gshuf -n 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment