Skip to content

Instantly share code, notes, and snippets.

@gavingc
Created March 5, 2011 08:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gavingc/856236 to your computer and use it in GitHub Desktop.
Save gavingc/856236 to your computer and use it in GitHub Desktop.
YouTubeMonitorAndUpload
#!/bin/sh
# Uses 'inoticoming' to efficiently monitor a directory and call an upload script.
#
# Usage:
# Remember to set the variables at the top of the script files.
# Can be run directly or at startup by adding lines to /etc/rc.local (Debian) example:
# sudo -b -u www-data /path/to/YouTubeMonitor.sh MONITOR_DIR COMPLETE_DIR [PLAYLIST_ID]
# Add one monitor line per directory/playlist.
# Now simply drop files into the monitored directory and they will be uploaded, named, tagged and playlisted.
#
# MONITOR_DIR might be an ftp, webdav or any directory.
#
# Depends on:
# - http://code.google.com/p/youtube-upload/ (download)
# - python-gdata (apt-get install)
# - inoticoming (apt-get install)
# - YouTubeUpload.sh (supplied)
## Set these variables.
UPLOAD_SCRIPT=/path/to/YouTubeUpload.sh
MONITOR_SCRIPT=/path/to/YouTubeMonitor.sh
## Command line args.
MONITOR_DIR="$1"
COMPLETE_DIR="$2"
PLAYLIST_ID="$3"
## To find UID: cat /etc/passwd |grep www-data
## You may disable this block if you don't want this protection.
## But seriously don't run as root!
if [ $UID != "33" ]; then
echo "Do NOT run this script as root!"
echo "Put something like this in /etc/sudoers:"
echo "%www-data ALL=(www-data) SETENV: NOPASSWD: $MONITOR_SCRIPT"
echo ""
echo "Then try: 'sudo -b -u www-data $MONITOR_SCRIPT'"
exit 1
fi
if [ $# -lt 2 ] ; then
echo "Usage: $0 MONITOR_DIR COMPLETE_DIR [PLAYLIST_ID]"
echo ""
echo "MONITOR_DIR is the directory to monitor."
echo "COMPLETE_DIR is the directory to move the file to."
echo "[PLAYLIST_ID] is optional and the playlist to add the uploaded file to"
exit 1
fi
if [ ! -d "$MONITOR_DIR" ] ; then
echo "$MONITOR_DIR: is not a directory"
exit 1
fi
if [ ! -d "$COMPLETE_DIR" ] ; then
echo "$COMPLETE_DIR is not a directory"
exit 1
fi
# inoticoming dir_to_monitor chdir dir action_to_call $1=dir $2=dest_dir $3=file $4=playlist_id
inoticoming "$MONITOR_DIR" --chdir "$MONITOR_DIR" "$UPLOAD_SCRIPT" "$MONITOR_DIR" "$COMPLETE_DIR" "{}" "$PLAYLIST_ID" \;
#!/bin/sh
# Upload video file to YouTube using youtube_upload.py
#
# Usage:
# Can be run directly or via YouTubeMonitor.sh
# Remember to set the variables at the top of the script files.
# Set rwx permissions: chmod 0750 ./YouTube*
# Set group: chown :www-data ./YouTube*
#
# Depends on:
# http://code.google.com/p/youtube-upload/ (download)
## Set these variables.
LOG_FILE=/path/to/YouTubeUpload.log
PYTHON_UPLOADER=/path/to/youtube_upload.py
UPLOADER_OPTS="--no-split"
LOGIN_EMAIL=user@example.com
LOGIN_PASS=MyPass
DESC=""
CATEGORY="Tech"
TAGS="tag1,tag2"
## Command line args.
UPLOAD_DIR="$1"
COMPLETE_DIR="$2"
UPLOAD_FILE="$3"
PLAYLIST_ID="$4"
## To find UID: cat /etc/passwd |grep www-data
## You may disable this block if you don't want this protection.
## But seriously don't run as root!
if [ $UID != "33" ]; then
echo "Do NOT run this script as root!"
echo "Put something like this in /etc/sudoers:"
echo "%www-data ALL=(www-data) SETENV: NOPASSWD: $PYTHON_UPLOADER"
echo ""
echo "Then try: 'sudo -b -u www-data $0'"
exit 1
fi
if [ $# -lt 3 ] ; then
echo "Usage: $0 UPLOAD_DIR COMPLETE_DIR UPLOAD_FILE [PLAYLIST_ID]"
echo ""
echo "UPLOAD_DIR is the directory upload from."
echo "COMPLETE_DIR is the directory to move the file to."
echo "UPLOAD_FILE is the file to upload."
echo "[PLAYLIST_ID] is optional and the playlist to add the uploaded file to"
echo "PLAYLIST_ID can be found at http://gdata.youtube.com/feeds/api/users/USERNAME/playlists"
echo "Play list URI can be checked at http://gdata.youtube.com/feeds/api/playlists/PLAYLIST_ID"
exit 1
fi
if [ ! -d "$UPLOAD_DIR" ] ; then
echo "$UPLOAD_DIR: is not a directory"
exit 1
fi
if [ ! -d "$COMPLETE_DIR" ] ; then
echo "$COMPLETE_DIR is not a directory"
exit 1
fi
if [ ! -f "$UPLOAD_DIR/$UPLOAD_FILE" ] ; then
echo "$UPLOAD_FILE: is not a file"
exit 1
fi
if [ -n "$PLAYLIST_ID" ] ; then
UPLOADER_OPTS="$UPLOADER_OPTS --playlist-uri=http://gdata.youtube.com/feeds/api/playlists/$PLAYLIST_ID"
fi
echo "" >> "$LOG_FILE"
date >> "$LOG_FILE"
## Test PYTHON_UPLOADER by logging returned YouTube categories.
#python "$PYTHON_UPLOADER" -c >> "$LOG_FILE" 2>&1
echo "Uploading file: $UPLOAD_FILE." >> "$LOG_FILE"
python "$PYTHON_UPLOADER" $UPLOADER_OPTS "$LOGIN_EMAIL" "$LOGIN_PASS" "$UPLOAD_DIR/$UPLOAD_FILE" "${UPLOAD_FILE%.*}" "$DESC" "$CATEGORY" "$TAGS" >> "$LOG_FILE" 2>&1
echo "Moving file." >> "$LOG_FILE"
mv "$UPLOAD_DIR/$UPLOAD_FILE" "$COMPLETE_DIR/" >> "$LOG_FILE" 2>&1
echo "Finished." >> "$LOG_FILE"
@tokland
Copy link

tokland commented Mar 5, 2011

Good code. Some ideas so it feels more "Unix":

  • if [ $UID != "33" ]; then: 33? this seems pretty hardcoded. I'd say it's not necessary to check which user is doing what, the UNIX philosophy is "let the user make stupid things if he wants to"
  • LOG_FILE: Instead, output to stdout and let the user redirect it. And maybe output to stderr with log() { echo "$@" >&2; }
  • if [ ! -d "$UPLOAD_DIR" ] ; then. I woulnd't check that either, but I guess it's a matter of taste.
  • Use "set -e" so the script exits at error, otherwise you can get nasty results.

@gavingc
Copy link
Author

gavingc commented Mar 6, 2011

Hi tokland,

Thanks for the review.
A benefit from the added overhead of using gist is that the end user can git clone the public url to their install dir and then commit local changes such as the variable settings. Any public improvements in the script can simply be verified and pulled into the installed clone, works like a charm.

The hard coded UID is required in the intended setup, the script interacts with webdav directories. I need to ensure that the www-data user is used for any and all script runs. The end user can disable the code block as suggested in the comments.

I use log this way for scripts that are meant to run unattended.
How would you use the log()? I guess this could provide a central place to change the log type?

I'm not a fan of set -e and agree with this http://mywiki.wooledge.org/BashPitfalls.
Instead I find myself using extra logging and error checking.

@tokland
Copy link

tokland commented Mar 9, 2011

Fair enough, except for the prevention with "set -e":

http://mywiki.wooledge.org/BashPitfalls
"Some people also like to enable set -e to make their scripts abort on any command that returns non-zero, but this can be rather tricky to use correctly (since many common commands may return a non-zero for a warning condition, which you may not want to treat as fatal)."

This seems a pretty weak argument to me. It's like saying "this mechanism will fail on 1% of the cases, so let's not use it at all" :-) yeah, I know, false sense of security is also dangeous, but still...

You can have the best of two worlds: set -e + check the return of commands.

@zillion42
Copy link

I saw that there is no real Google Drive Sync for linux (except proprietary) , I really need to sync a folder. I installed and configured rclone, and I was wondering if I can use inoticoming to trigger sync events.
I already use inoticoming to print (lpr) Pdf files, that my wife needs for her business. She saves them as pdf in a watched folder, and that triggers the print command.
^Nice Code !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment