Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DownLoadOrganizer code is tightly coupled #4

Open
KRVPerera opened this issue Jul 9, 2020 · 0 comments
Open

DownLoadOrganizer code is tightly coupled #4

KRVPerera opened this issue Jul 9, 2020 · 0 comments

Comments

@KRVPerera
Copy link
Owner

On MacOS you can decouple and run python scripts by Automator folder actions. Some python libraries are missing and code needs to be simple and mostly native.

Code must be organized to decouple to a standalone native program which can be extended to a FileSystemWatcher.

Can start here.

import os
import time

def createDir(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)


class FolderOrganizer():
    trackingFolder = "/Users/rukshanp/Downloads"
    jpgFolder = "/Users/rukshanp/Pictures/downloaded"
    jarFolder = "/Users/rukshanp/Downloads/jars"
    setupFolder = "/Users/rukshanp/Downloads/SDKs"
    videosFolder = "/Users/rukshanp/Movies/downloaded"

    def __init__(self, trackingFolder):
        self.trackingFolder = trackingFolder
        createDir(self.jpgFolder)
        createDir(self.jarFolder)
        createDir(self.setupFolder)
        createDir(self.videosFolder)
        print("Created")


    def on_modified(self):
        for file in os.listdir(self.trackingFolder):
            if self.isImageFile(file):
                srcPath = self.trackingFolder + "/" + file
                newDestination = self.jpgFolder + "/" + file
                i = 0
                while os.path.isfile(newDestination):
                    newDestination = self.jpgFolder + \
                        "/new_file_" + str(i) + "__" + file
                    i = i + 1

                print("Moving {} to {}".format(srcPath, newDestination))

            elif self.isJarFile(file):
                srcPath = self.trackingFolder + "/" + file
                newDestination = self.jarFolder + "/" + file
                i = 0
                while os.path.isfile(newDestination):
                    newDestination = self.jarFolder + \
                        "/new_file_" + str(i) + "__" + file
                    i = i + 1

                print("Moving {} to {}".format(srcPath, newDestination))

            elif self.isSetupFile(file):
                srcPath = self.trackingFolder + "/" + file
                newDestination = self.setupFolder + "/" + file
                i = 0
                while os.path.isfile(newDestination):
                    newDestination = self.setupFolder + \
                        "/new_file_" + str(i) + "__" + file
                    i = i + 1

                print("Moving {} to {}".format(srcPath, newDestination))

            elif self.isVideoFile(file):
                srcPath = self.trackingFolder + "/" + file
                newDestination = self.videosFolder + "/" + file
                i = 0
                while os.path.isfile(newDestination):
                    newDestination = self.videosFolder + \
                        "/new_file_" + str(i) + "__" + file
                    i = i + 1

                print("Moving {} to {}".format(srcPath, newDestination))
            else:
                continue

            os.rename(srcPath, newDestination)

    def isImageFile(self, fileName):
        fileName = fileName.lower()
        if fileName.endswith(".jpg"):
            return True
        if fileName.endswith(".png"):
            return True
        return False

    def isJarFile(self, fileName):
        if fileName.endswith(".jar"):
            return True
        return False

    def isSetupFile(self, fileName):
        if fileName.endswith(".dmg"):
            return True
        if fileName.endswith(".iso"):
            return True
        if fileName.endswith(".app"):
            return True
        return False

    def isVideoFile(self, fileName):
        if fileName.endswith(".mp4"):
            return True
        return False


def main():
    downLoadsFolder = "/Users/rukshanp/Downloads"
    folderEventHandler = FolderOrganizer(downLoadsFolder)
    folderEventHandler.on_modified()


if __name__ == "__main__":
    main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant