17334 Script to move downloaded files to the corresponding folder - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Development > Userland Programming & Scripting

Userland Programming & Scripting C, Shell, Perl, Sed & Awk

Reply
 
Thread Tools Display Modes
  #1  
Old April 20th, 2012, 20:26
Kveras Kveras is offline
Junior Member
 
Join Date: Apr 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default Script to move downloaded files to the corresponding folder

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

Last edited by DutchDaemon; April 21st, 2012 at 00:57. Reason: Proper formatting: http://forums.freebsd.org/showthread.php?t=8816
Reply With Quote
  #2  
Old April 20th, 2012, 22:52
bbzz bbzz is offline
Member
 
Join Date: Nov 2010
Location: random
Posts: 826
Thanks: 77
Thanked 119 Times in 79 Posts
Default

transmission-remote

-w switch.
Reply With Quote
  #3  
Old April 21st, 2012, 00:03
Kveras Kveras is offline
Junior Member
 
Join Date: Apr 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by bbzz View Post
transmission-remote

-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 /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.

Last edited by DutchDaemon; April 21st, 2012 at 00:57.
Reply With Quote
  #4  
Old April 21st, 2012, 00:04
Kveras Kveras is offline
Junior Member
 
Join Date: Apr 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #5  
Old April 21st, 2012, 10:13
jalla's Avatar
jalla jalla is offline
Member
 
Join Date: Aug 2009
Location: Bergen, Norway
Posts: 334
Thanks: 11
Thanked 67 Times in 58 Posts
Default

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?
Reply With Quote
The Following User Says Thank You to jalla For This Useful Post:
Kveras (April 22nd, 2012)
  #6  
Old April 21st, 2012, 17:24
roddierod's Avatar
roddierod roddierod is offline
Member
 
Join Date: Nov 2008
Location: On my Slingerlands!
Posts: 638
Thanks: 29
Thanked 72 Times in 62 Posts
Default

Quote:
Originally Posted by Kveras View Post
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
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.
Reply With Quote
  #7  
Old April 21st, 2012, 17:58
aa aa is offline
Junior Member
 
Join Date: Mar 2012
Posts: 47
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Use magic: file filename.
Reply With Quote
  #8  
Old April 21st, 2012, 18:02
roddierod's Avatar
roddierod roddierod is offline
Member
 
Join Date: Nov 2008
Location: On my Slingerlands!
Posts: 638
Thanks: 29
Thanked 72 Times in 62 Posts
Default

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?
Reply With Quote
  #9  
Old April 21st, 2012, 18:32
roddierod's Avatar
roddierod roddierod is offline
Member
 
Join Date: Nov 2008
Location: On my Slingerlands!
Posts: 638
Thanks: 29
Thanked 72 Times in 62 Posts
Default

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()
Reply With Quote
The Following User Says Thank You to roddierod For This Useful Post:
Kveras (April 22nd, 2012)
  #10  
Old April 21st, 2012, 18:42
aa aa is offline
Junior Member
 
Join Date: Mar 2012
Posts: 47
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Quote:
Originally Posted by roddierod View Post
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
Reply With Quote
  #11  
Old April 22nd, 2012, 01:18
Kveras Kveras is offline
Junior Member
 
Join Date: Apr 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by roddierod View Post
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()
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'
Reply With Quote
  #12  
Old April 22nd, 2012, 04:07
roddierod's Avatar
roddierod roddierod is offline
Member
 
Join Date: Nov 2008
Location: On my Slingerlands!
Posts: 638
Thanks: 29
Thanked 72 Times in 62 Posts
Default

Change this line

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

Last edited by DutchDaemon; April 22nd, 2012 at 04:16.
Reply With Quote
  #13  
Old May 13th, 2012, 09:51
rednoah rednoah is offline
Junior Member
 
Join Date: May 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default FileBot

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/forum...p?f=4&t=5#p802

Here's another tutorial on monitoring completed downloads:
http://www.torrent-invites.com/audio...t-copying.html

Last edited by DutchDaemon; May 13th, 2012 at 23:03.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Solved] find files older than 10 min - script/cmd problem da1 Userland Programming & Scripting 3 August 5th, 2011 14:54
[Solved] I want move big files from FreeBSD box to Linux Box with USB flash mfaridi General 4 January 4th, 2011 15:07
tftp server - downloaded files log & server_args ? jenaniston Networking 2 January 20th, 2010 15:28
cd to sub-folder on Flash using Script iic2 Userland Programming & Scripting 8 April 7th, 2009 12:49
[Solved] mutt move msg to folder graudeejs Installation and Maintenance of FreeBSD Ports or Packages 2 March 5th, 2009 21:46


All times are GMT +1. The time now is 21:27.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0