run beets from background script?

468 views
Skip to first unread message

Corinne Hoener

unread,
Apr 28, 2013, 8:36:55 AM4/28/13
to beets...@googlegroups.com
Hey all - 

I'm wondering if what I want to do is theoretically possible. I've been having lots of trouble setting it up. 

I use the transmission-daemon to manage my torrent downloads. Transmission's settings allow you to specify a script to run when a torrent finishes downloading. So when a torrent completes, I'd like to run a script that automatically runs beets on the downloaded directory. 

I'm having all sorts of trouble and I'm thinking maybe it has something to do with the transmission-daemon user not being able to execute the beet command or something, but before I go down that road, I thought I'd stop and ask if it's at least /theoretically/ possible for this to work. Since beets requires user-interaction depending on the tag match strength, I figured maybe it's not even possible to do something like this. (It would be rad if you could though!) 

Any guidance is appreciated - thanks! 

Cheers,
C.

Adrian Sampson

unread,
Apr 28, 2013, 4:28:54 PM4/28/13
to beets...@googlegroups.com
Hi, Corinne,

Great question. Yes, this should definitely be possible -- the key here is the quiet import option (-q on the command line or import.quiet in the config file). This option's raison d'etre is unattended imports; it should suppress all user input and make the import command suitable to run in the background.

You might then want to fiddle with the importer parameters (see the threshold options in the config docs) to make the importer more tolerant since you won't be there to give it advice.

I don't personally run with this setup, but perhaps other users can comment on how they're running "beet import"s in the background.

Good luck!

Adrian


--
You received this message because you are subscribed to the Google Groups "beets" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beets-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Corinne Hoener

unread,
Apr 28, 2013, 5:56:06 PM4/28/13
to beets...@googlegroups.com
Hey Adrian - 

Thanks very much! Perhaps the -q flag is all I was missing; I feel silly for having missed that completely. I'll do some work and report back.

Cheers,
C.

zman...@gmail.com

unread,
Feb 16, 2015, 8:04:58 PM2/16/15
to beets...@googlegroups.com
Replying to old thread, but it is EXACTLY my topic, so hope that's OK!

I also am having this problem, and have done a bit more testing.

I have a script (bash on Linux) which executes the beets command.  If I call this from the command line (or indeed another script) it executes properly, if I call from transmission it fails.

Some further points
  • When called from transmission, the calling script completes, but the beets command is skipped (threading?)
  • I have tried the -q flag - no difference
  • I even just tried "beet list" Same result - works from command line, not from transmission.
  • I use other scripts in the same way from transmission (eg filebot) and they work
The key thing is the silent failure which suggests that beets spawns a separate thread and returns control.

Anybody had success, or any other ideas?

Michael MacLeod

unread,
Feb 17, 2015, 1:41:25 AM2/17/15
to beets...@googlegroups.com
I use a script between transmission and beets. Firstly, transmission is running (on debian) as debian-transmission, which doesn't have write access to either my library file or the directory where I want my music to go. I have some logic in my script that calls whatmp3 if it detects a flac download in order to convert it to mp3 before importing. It emails me whenever it tries to import an album, letting me know if it thinks it failed. I run beets as user installation in my own user account on this particular host, so in sudoers I have this line:

debian-transmission ALL=(mikemacleod) NOPASSWD:/home/mikemacleod/.local/bin/beet

Here's the script I call from transmission:

#!/bin/bash -x
#
# beetsimport.sh - a script to be called by transmission-daemon to attempt to
#                  auto-import newly downloaded torrents into beets
#

IMPORT_USER="mikemacleod"
TMPLOG="/tmp/beetsimport.${TR_TORRENT_ID}.log"
LOG="/var/log/beetsimport.log"

cd $TR_TORRENT_DIR
echo "NEWTORRENT: $TR_TORRENT_NAME" > $TMPLOG
FLAC=`ls -l "$TR_TORRENT_NAME" | grep -i flac | wc -l`
IMPORT_DIR="$TR_TORRENT_NAME"

# Check if the torrent directory exists 
if [ -d "$TR_TORRENT_NAME" ]; then
  # If FLAC then convert to MP3
  if [ "$FLAC" != "0" ]; then
    /usr/local/bin/whatmp3 --V0 --threads=4 "$IMPORT_DIR" > $TMPLOG.V0
    /usr/local/bin/whatmp3 --V2 --threads=4 "$IMPORT_DIR" > $TMPLOG.V2
    /usr/local/bin/whatmp3 --320 --threads=4 "$IMPORT_DIR" > $TMPLOG.320
    IMPORT_DIR=`/bin/cat $TMPLOG.V0 | grep WHAT_MP3_DIR | cut -f4 -d '/'`
    echo "CONVERTEDTORRENT: $IMPORT_DIR" >> $TMPLOG
  fi

  # Import into Beets
  /usr/bin/sudo -u $IMPORT_USER /home/mikemacleod/.local/bin/beet import -q "$IMPORT_DIR" >> $TMPLOG

  # Check for success
  grep -q Skipping $TMPLOG
  IMPORT=$?

  # If we converted copy over the logs
  if [ "$FLAC" != "0" ]; then
    /bin/cat $TMPLOG.V0 >> $TMPLOG
    /bin/cat $TMPLOG.V2 >> $TMPLOG
    /bin/cat $TMPLOG.320 >> $TMPLOG
    /bin/rm $TMPLOG.V0
    /bin/rm $TMPLOG.V2
    /bin/rm $TMPLOG.320
  fi

  # Email the results
  if [ "$IMPORT" == "0" ]; then
    /usr/bin/mail -s "Beets Import Failed" "$EMAIL" < $TMPLOG
  else
    /usr/bin/mail -s "Beets Import Successful" "$EMAIL" < $TMPLOG
  fi

else

  # Email if the directory doesn't exist
  echo "Could not find the torrent \"$TR_TORRENT_NAME\" in the completed torrents folder" >> $TMPLOG
  /usr/bin/mail -s "Beets Could Not Import" "$EMAIL" < $TMPLOG

fi
/bin/cat $TMPLOG >> $LOG
/bin/rm $TMPLOG


That should get you started, at least.

--
You received this message because you are subscribed to the Google Groups "beets" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beets-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

zman...@gmail.com

unread,
Feb 18, 2015, 7:44:09 AM2/18/15
to beets...@googlegroups.com
Hi Mike,

Thanks for this script, your setup looks awesome, I particularly like the way you do the sudo without password - I didn't know of that setting in sudoers.

The crazy thing is - mine has started working and I don't really know why.  I made a few changes, did a bit of testing with printenv and the like.  Somehow it started working, and I don't know what I did.  Good that it's working, bad that I don't know why!  (and not versioned...)

Thanks for posting, main thing is that you gave me the confidence to keep trying as I knew it was possible (and taught me a couple of new things too!)

Bruno Cauet

unread,
Feb 18, 2015, 8:06:52 AM2/18/15
to beets...@googlegroups.com
Hi,
If I may there's a couple of issues common to all scripts that are important to review in case you have a problem:
1. permissions:
    - what are the permissions of the script?
    - what is the user running the script?
    - what are the permissions of the directories on which you're working? And of the directories leading to them?
2. path:
    - what are the paths of the binaries you're trying to invoke?
    - is there a non-standard path among those (e.g. ~/.local/bin)? Is in PATH?
    - (python related) is the installation in a virtualenv? Did you install stuff with pip --user?
In my (short) experience many errors arise from one or several of those points.
Also remember -e and -x modes (set -e; set -x) for printing all lines & stopping on the first error.

Have a nice day & enjoy scripting

--
Reply all
Reply to author
Forward
0 new messages