From b8d69f9bf5a03fd6d8b6a477f3b7ca8f10c27bda Mon Sep 17 00:00:00 2001 From: gabrielgio Date: Tue, 27 Jul 2021 23:15:53 +0200 Subject: feat: Move to gallery-dl As it turns out there is already a project that does what I want but better. --- main.py | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 036aab3..2f34b3a 100644 --- a/main.py +++ b/main.py @@ -7,8 +7,8 @@ import praw from nextcloud import NextCloud from praw.models.util import stream_generator +from gallery_dl.job import DownloadJob -from importer.downloader import Downloader from importer.uploader import upload_file, create_folders levels = { @@ -55,6 +55,33 @@ parser.add_argument('-l', '--log-level', choices=levels.keys(), help=f'it will set log level.') + +def get_list_of_files(dirName): + # create a list of file and sub directories + # names in the given directory + listOfFile = os.listdir(dirName) + allFiles = list() + # Iterate over all the entries + for entry in listOfFile: + # Create full path + fullPath = os.path.join(dirName, entry) + # If entry is a directory then get the list of files in this directory + if os.path.isdir(fullPath): + allFiles = allFiles + get_list_of_files(fullPath) + else: + allFiles.append(fullPath) + + return allFiles + + +def download(url) -> [str]: + d = DownloadJob(url=url) + d.run() + basedir = d.pathfmt.basedirectory + files = get_list_of_files(basedir) + return files + + if __name__ == "__main__": args = parser.parse_args() @@ -81,15 +108,12 @@ if __name__ == "__main__": try: url = post.url create_folders(f"{args.nextcloud_path}/{post.subreddit}/", nxc) - d = Downloader(url=url, reddit=reddit) - with d.provider() as provider: - provider.download() - logging.info(f"{post.id} from {post.subreddit} downloaded") - for path in provider.paths: - if "-mobile" in path: # Remove mobile version - continue - upload_file(path, f"{args.nextcloud_path}/{post.subreddit}/{path}", nxc) - logging.info(f"{path} uploaded") + logging.info(f"{post.id} from {post.subreddit} downloaded") + for path in download(url): + filename = os.path.basename(path) + upload_file(path, f"{args.nextcloud_path}/{post.subreddit}/{filename}", nxc) + os.unlink(path) + logging.info(f"{path} uploaded") except Exception as e: logging.error(e) -- cgit v1.2.3