zvodd
9/20/2016 - 11:12 PM

Compile multiple torrent videos into one folder.

Compile multiple torrent videos into one folder.

import os
import re
import shutil

# Finds files in 1-d down subfolder with name match from CHECKFOLDER. If file matches extensions, move it to destination folder.


CHECKFOLDER = "D:\Downloads\Torrent"
DESTFOLDER = "D:\Downloads\Videos\AdventureTime S7"

MATCH_FOLDER_NAME = "Adventure.?Time.*"
MATCH_EXTS = ['mkv', 'mp4', 'avi']


CHECKFOLDER = os.path.abspath(CHECKFOLDER)
DESTFOLDER = os.path.abspath(DESTFOLDER)
MATCH_FOLDER_NAME = re.compile("MATCH_FOLDER_NAME")

def lazyprop(fn):
    attr_name = '_lazy_' + fn.__name__
    @property
    def _lazyprop(self):
        if not hasattr(self, attr_name):
            setattr(self, attr_name, fn(self))
        return getattr(self, attr_name)
    return _lazyprop

class PathInst(object):
    def __init__( self, name, folder):
        self.name = name
        self.parent = os.path.normpath(folder)
        self.fullpath = os.path.join(self.parent, name)

    @lazyprop
    def exists(self):
        return os.path.exists(self.fullpath)

    @lazyprop
    def isfile(self):
        return os.path.isfile(self.fullpath)

    @lazyprop
    def stat(self):
        return os.stat(self.fullpath)

    def __repr__(self):
        return "<PathInst: \"{}\">".format(self.fullpath)
        
    def list_children(self):
        if self.isfile:
            raise StopIteration
        for f in os.listdir(self.fullpath):
            yield PathInst(f, self.fullpath)


def move_file (src, dst):
    print ("'{}' --> '{}'".format(src.fullpath, dst.fullpath))
    # TODO OVERWRITE CHECK
    shutil.move(src.fullpath, dst.fullpath)


def get_paths(check_path):
    for c_name in os.listdir(check_path):
        if MATCH_FOLDER_NAME.match(c_name):
            c_path = PathInst(c_name, check_path)
            if not c_path.isfile:
                    yield c_path


def main():
    for folder in get_paths(CHECKFOLDER):
        for potential_main in folder.list_children():
            if list(filter(lambda x : potential_main.name.endswith(x), MATCH_EXTS)):
                dest = PathInst(potential_main.name, DESTFOLDER)
                move_file(potential_main, dest)


if __name__ == '__main__':
    main()