# Script to move downloaded files to the corresponding folder



## Kveras (Apr 20, 2012)

Greetings fellow *Free*BSD-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 *Free*BSD 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


----------



## bbzz (Apr 20, 2012)

transmission-remote

*-w* switch.


----------



## Kveras (Apr 21, 2012)

bbzz said:
			
		

> 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:

Read /srv/download every hour for newly added files or folders.
Determine what kind of data it is, music, movie, tv-series or other.
Copy file, or extract if compressed, to the corresponding directory.
Rename the newly created folder to comply with the naming scheme.
Problem is, I have no idea where to start.


----------



## Kveras (Apr 21, 2012)

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.


----------



## jalla (Apr 21, 2012)

Perhaps filebot?
In addition to a gui it has a cmdline interface that could be suitable for scripting.


----------



## roddierod (Apr 21, 2012)

Kveras said:
			
		

> Greetings fellow *Free*BSD-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.
> 
> ...



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.


----------



## aa (Apr 21, 2012)

Use magic(): file() filename.


----------



## roddierod (Apr 21, 2012)

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?


----------



## roddierod (Apr 21, 2012)

Something like this, run from a cron job.

*WARNING:* Quick, dirty and untested.


```
#!/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()
```


----------



## aa (Apr 21, 2012)

roddierod said:
			
		

> 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


----------



## Kveras (Apr 22, 2012)

roddierod said:
			
		

> Something like this, run from a cron job.
> 
> *WARNING:* Quick, dirty and untested.
> 
> ...



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:


```
-> 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'
```


----------



## roddierod (Apr 22, 2012)

Change this line


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

```
fn, ext = filename.lower().split('.',1)
```


----------



## rednoah (May 13, 2012)

*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/forums/viewtopic.php?f=4&t=5#p802

Here's another tutorial on monitoring completed downloads:
http://www.torrent-invites.com/audi...-shows-while-continuing-seed-not-copying.html


----------

