Script to move downloaded files to the corresponding folder

C, C++, Python, Perl, Shell, etc.

Script to move downloaded files to the corresponding folder

Postby Kveras » 20 Apr 2012, 19:26

Greetings fellow FreeBSD-users.

I have a problem that I need a solution to, and I think a simple script might do the work; however I am not skilled in scripting and thus I come asking for help.

The scenario:

I run FreeBSD 9 on a small home server, using to ZFS disks in a single pool. I use [FILE]transmission-daemon[/FILE] to handle all my downloads (by just saving the torrent to my watch-folder) and all my downloads are going in one folder ([FILE]/srv/download[/FILE]). I then manually copy all the downloaded material to the corresponding folder, every once per week or so. Music to [FILE]/srv/music[/FILE], movies to [FILE]/srv/movies[/FILE], tv-series to [FILE]/srv/tv-series[/FILE] and misc to [FILE]/srv/misc[/FILE].

I prefer to name my folders manually, for example [FILE]/srv/movies/movie_title_2012[/FILE]. Tv-series goes to [FILE]/srv/tv-series/title/season_0X/[/FILE].

The problem:

I find this repetitive task somewhat boring, and in the UNIX world I think a script could be able to do the legwork for me. I have tried a couple of small programs previously, while running Linux, but they always messed up the names, or placed the wrong content in the wrong folder. The naming in [FILE]/srv/music[/FILE] does not matter, since the music client mostly handles the identification fairly well.

Does anyone have a suggestion on how to do this? If more information is needed, please let me know.

Regards,
Johan
Kveras
Junior Member
 
Posts: 4
Joined: 20 Apr 2012, 19:02

Postby bbzz » 20 Apr 2012, 21:52

[FILE]transmission-remote[/FILE]

-w switch.
bbzz
Member
 
Posts: 858
Joined: 04 Nov 2010, 01:07
Location: random

Postby Kveras » 20 Apr 2012, 23:03

bbzz wrote:[FILE]transmission-remote[/FILE]

-w switch.


Thank you for responding. But this does not solve the issue with naming etc.

I would think that a cronjob that did the following would be best:
  1. Read [FILE]/srv/download[/FILE] every hour for newly added files or folders.
  2. Determine what kind of data it is, music, movie, tv-series or other.
  3. Copy file, or extract if compressed, to the corresponding directory.
  4. Rename the newly created folder to comply with the naming scheme.
Problem is, I have no idea where to start.
Kveras
Junior Member
 
Posts: 4
Joined: 20 Apr 2012, 19:02

Postby Kveras » 20 Apr 2012, 23:04

I might add that I have a "flexget" cronjob to download things I subscribe to. So I am not adding the torrents myself half of the time.
Kveras
Junior Member
 
Posts: 4
Joined: 20 Apr 2012, 19:02

Postby jalla » 21 Apr 2012, 09:13

Perhaps filebot?
In addition to a gui it has a cmdline interface that could be suitable for scripting.
Practical latin
Amicule, deliciae, num is sum qui mentiar tibi?
But dear, could I ever lie to you?
User avatar
jalla
Member
 
Posts: 369
Joined: 06 Aug 2009, 12:41
Location: Bergen, Norway

Postby roddierod » 21 Apr 2012, 16:24

Kveras wrote:Greetings fellow FreeBSD-users.

I have a problem that I need a solution to, and I think a simple script might do the work; however I am not skilled in scripting and thus I come asking for help.

The scenario:

I run FreeBSD 9 on a small home server, using to ZFS disks in a single pool. I use [FILE]transmission-daemon[/FILE] to handle all my downloads (by just saving the torrent to my watch-folder) and all my downloads are going in one folder ([FILE]/srv/download[/FILE]). I then manually copy all the downloaded material to the corresponding folder, every once per week or so. Music to [FILE]/srv/music[/FILE], movies to [FILE]/srv/movies[/FILE], tv-series to [FILE]/srv/tv-series[/FILE] and misc to [FILE]/srv/misc[/FILE].

I prefer to name my folders manually, for example [FILE]/srv/movies/movie_title_2012[/FILE]. Tv-series goes to [FILE]/srv/tv-series/title/season_0X/[/FILE].

The problem:

I find this repetitive task somewhat boring, and in the UNIX world I think a script could be able to do the legwork for me. I have tried a couple of small programs previously, while running Linux, but they always messed up the names, or placed the wrong content in the wrong folder. The naming in [FILE]/srv/music[/FILE] does not matter, since the music client mostly handles the identification fairly well.

Does anyone have a suggestion on how to do this? If more information is needed, please let me know.

Regards,
Johan


This could easily be scripted, I prefer python myself but I'm sure it could be whipped up as a bash script or anything else. My one question would be...how is the script suppose to know if the download file is a movie or tv show? Music, video and application file can be seperated by file extension easily, but how do you divide the video? Music you could divide by reading the id3 tag - assuming all download had valid one.
User avatar
roddierod
Member
 
Posts: 697
Joined: 17 Nov 2008, 12:36
Location: On my Slingerlands!

Postby aa » 21 Apr 2012, 16:58

Use [man]magic[/man]: [man]file[/man] [file]filename[/file].
aa
Junior Member
 
Posts: 47
Joined: 24 Mar 2012, 09:42

Postby roddierod » 21 Apr 2012, 17:02

So exactly how will that tell me that x-files.avi is one of the feature length movies or if it one of the tv episodes?
User avatar
roddierod
Member
 
Posts: 697
Joined: 17 Nov 2008, 12:36
Location: On my Slingerlands!

Postby roddierod » 21 Apr 2012, 17:32

Something like this, run from a cron job.

WARNING: Quick, dirty and untested.

Code: Select all
#!/usr/local/bin/python

import os
import sys
import subprocess

VIDEO_FILE_TYPES = ['avi','mpg','mkv','divx','vob','mp4','m4v']
AUDIO_FILE_TYPES = ['mp3','ogg','flac','wav']

DOWNLOAD_DIR = '/srv/download'
DESTINATION_FILE = '/srv/{TYPE}/{FILENAME}'
MOVE_COMMAND = 'mv {SRC} {DEST}'

def main():
  for root, dir, files in os.walk(DOWNLOAD_DIR):
    for filename in files:
      try:
        fn, ext = filename.split('.',1).lower()     
     
        if ext in VIDEO_FILE_TYPES:
          file_type='movie'
        elif ext in VIDEO_FILE_TYPES:
          file_type='music'
        else:
          file_type='misc'
         
        source_file = os.path.join(root, filename)
        DESTINATION_FILE.format(TYPE=file_type, FILENAME=filename)
        MOVE_COMMAND.format(SRC=source_file, DEST=DESTINATION_FILE)
         
        rtn  = subprocess.call(MOVE_COMMAND, shell=True)
      except IndexError:
        print 'File: %s - has no extension' % (file)

if __name__ == '__main__':
  main()

User avatar
roddierod
Member
 
Posts: 697
Joined: 17 Nov 2008, 12:36
Location: On my Slingerlands!

Postby aa » 21 Apr 2012, 17:42

roddierod wrote:So exactly how will that tell me that x-files.avi is one of the feature length movies or if it one of the tv episodes?

Sorry, I'm referring to the thread subject
aa
Junior Member
 
Posts: 47
Joined: 24 Mar 2012, 09:42

Postby Kveras » 22 Apr 2012, 00:18

roddierod wrote:Something like this, run from a cron job.

WARNING: Quick, dirty and untested.

Code: Select all
#!/usr/local/bin/python

import os
import sys
import subprocess

VIDEO_FILE_TYPES = ['avi','mpg','mkv','divx','vob','mp4','m4v']
AUDIO_FILE_TYPES = ['mp3','ogg','flac','wav']

DOWNLOAD_DIR = '/srv/download'
DESTINATION_FILE = '/srv/{TYPE}/{FILENAME}'
MOVE_COMMAND = 'mv {SRC} {DEST}'

def main():
  for root, dir, files in os.walk(DOWNLOAD_DIR):
    for filename in files:
      try:
        fn, ext = filename.split('.',1).lower()     
     
        if ext in VIDEO_FILE_TYPES:
          file_type='movie'
        elif ext in VIDEO_FILE_TYPES:
          file_type='music'
        else:
          file_type='misc'
         
        source_file = os.path.join(root, filename)
        DESTINATION_FILE.format(TYPE=file_type, FILENAME=filename)
        MOVE_COMMAND.format(SRC=source_file, DEST=DESTINATION_FILE)
         
        rtn  = subprocess.call(MOVE_COMMAND, shell=True)
      except IndexError:
        print 'File: %s - has no extension' % (file)

if __name__ == '__main__':
  main()



I could not express how impressed I am that someone would take their time to help me out in this way! Thank you!

However, I get the following:

Code: Select all
 -> python script.py
Traceback (most recent call last):
  File "script.py", line 36, in <module>
    main()
  File "script.py", line 18, in main
    fn, ext = filename.split('.',1).lower()
AttributeError: 'list' object has no attribute 'lower'
Kveras
Junior Member
 
Posts: 4
Joined: 20 Apr 2012, 19:02

Postby roddierod » 22 Apr 2012, 03:07

Change this line

Code: Select all
fn, ext = filename.split('.',1).lower()

to
Code: Select all
fn, ext = filename.lower().split('.',1)
User avatar
roddierod
Member
 
Posts: 697
Joined: 17 Nov 2008, 12:36
Location: On my Slingerlands!

FileBot

Postby rednoah » 13 May 2012, 08:51

With FileBot scripting feature you can take care of everything you need quite easily. Check out the example scripts.

There's an example on how do something similar with µTorrent. Should be straight-forward to adjust things for transmission:
http://filebot.sourceforge.net/forums/viewtopic.php?f=4&t=5#p802

Here's another tutorial on monitoring completed downloads:
http://www.torrent-invites.com/audio-video/193980-automatically-sorting-dled-movies-tv-shows-while-continuing-seed-not-copying.html
rednoah
Junior Member
 
Posts: 2
Joined: 13 May 2012, 08:44


Return to Userland Programming & Scripting

Who is online

Users browsing this forum: No registered users and 0 guests