1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Script to move downloaded files to the corresponding folder

Discussion in 'Userland Programming and Scripting' started by Kveras, Apr 20, 2012.

  1. Kveras

    Kveras New Member

    Messages:
    4
    Likes Received:
    0
    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 transmission-daemon to handle all my downloads (by just saving the torrent to my watch-folder) and all my downloads are going in one folder (/srv/download). I then manually copy all the downloaded material to the corresponding folder, every once per week or so. Music to /srv/music, movies to /srv/movies, tv-series to /srv/tv-series and misc to /srv/misc.

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

    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 /srv/music 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
     
  2. bbzz

    bbzz New Member

    Messages:
    867
    Likes Received:
    0
    transmission-remote

    -w switch.
     
  3. Kveras

    Kveras New Member

    Messages:
    4
    Likes Received:
    0
    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 /srv/download 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.
     
  4. Kveras

    Kveras New Member

    Messages:
    4
    Likes Received:
    0
    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.
     
  5. jalla

    jalla New Member

    Messages:
    374
    Likes Received:
    0
    Perhaps filebot?
    In addition to a gui it has a cmdline interface that could be suitable for scripting.
     
  6. roddierod

    roddierod Member

    Messages:
    701
    Likes Received:
    0
    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.
     
  7. aa

    aa New Member

    Messages:
    48
    Likes Received:
    0
  8. roddierod

    roddierod Member

    Messages:
    701
    Likes Received:
    0
    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?
     
  9. roddierod

    roddierod Member

    Messages:
    701
    Likes Received:
    0
    Something like this, run from a cron job.

    WARNING: Quick, dirty and untested.

    Code:
    #!/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()
    
    
     
  10. aa

    aa New Member

    Messages:
    48
    Likes Received:
    0
    Sorry, I'm referring to the thread subject
     
  11. Kveras

    Kveras New Member

    Messages:
    4
    Likes Received:
    0
    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:
     -> 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'
    
     
  12. roddierod

    roddierod Member

    Messages:
    701
    Likes Received:
    0
    Change this line

    Code:
    fn, ext = filename.split('.',1).lower()
    
    to
    Code:
    fn, ext = filename.lower().split('.',1)
    
     
  13. rednoah

    rednoah New Member

    Messages:
    2
    Likes Received:
    0