From 07d554e235509a1d01b005444ddd25f22b538f53 Mon Sep 17 00:00:00 2001 From: William East Date: Sat, 28 May 2022 17:20:24 +0200 Subject: [PATCH] added local file mgmt and config parser --- functions.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 53 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 functions.py create mode 100644 main.py diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..ed6d968 --- /dev/null +++ b/functions.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +import os +import shutil +import yaml + + +def checkMimes(file, allowed_extensions): + """Check if the file is in the correct extensions list, if not, return False""" + if os.path.splitext(file)[1] in allowed_extensions: + file_valid = True + else: + file_valid = False + return file_valid + + +def handleLocalTorrents(torrent_dir, initial_dir, move_file=False): + """move .torrent files to initial_dir""" + torrents_found = 0 + + with os.scandir(initial_dir) as localdir: + for entry in localdir: + # check if file is correct type + if entry.is_file() and checkMimes(entry, ".torrent"): + print(f"Found torrent file ::: {entry.name}") + torrents_found += 1 + if move_file: + shutil.move(entry, torrent_dir) + print(f"Moved to {torrent_dir}") + if torrents_found == 0: + print(f"No torrent files found to process in {initial_dir}") + else: + print(f"proceesed {os.path(initial_dir)}") + + +def torrentIdentifier(directory): + torrents = [] + with os.scandir(directory) as localdir: + for entry in localdir: + # check if file is correct type + if entry.is_file() and checkMimes(entry, ".torrent"): + # print(f"Found torrent file ::: {entry.name}") + torrents.append(entry) + return torrents + + +def getDiffList(a, b): + """get members of a not present in b, return list of members""" + difflist = [x for x in a if x not in b] + return difflist + + +def moveManager(torrents, torrent_dir): + for torrent in torrents: + shutil.move(torrent, torrent_dir) + + +def yamlDataExtract(config_file="config.yaml"): + + with open("config.yaml", "r") as config: + try: + data = yaml.safe_load(config) + return data + except yaml.YAMLError as exc: + print(exc) diff --git a/main.py b/main.py new file mode 100644 index 0000000..5b7c2a3 --- /dev/null +++ b/main.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import os +import sys +import ftplib # this package allows for ftp login. +import shutil +import functions +import argparse # this package allows for arg parsing + +# TODO first, get name of From folder and Too folder +# argv = sys.argv + +## Directories +# This is where you want the torrents to be managed +# TORRENT_DIR = "~/org/projects/Dev/Python/seed_dl/torrentfiles/" + +# This is the directory where the torrent files are dumped by the browser +# TARGET_DIR = "~/Downloads/" +# with ftplib.FTP(SEEDBOX_ADDR, SEEDBOX_LOGIN, SEEDBOX_PW) as ftp: +# ftp.dir("watch") + +# check local directory for torrent files, and move them to torrentdir +# functions.handleLocalTorrents( +# torrent_dir=TORRENT_DIR, initial_dir=TARGET_DIR, move_file=False +# ) + +config = functions.yamlDataExtract() + +TORRENT_DIR = config["torrent_dir"] +TARGET_DIR = config["target_dir"] + + +# TARGET_DIR, TORRENT_DIR = +from_torrents = functions.torrentIdentifier(TARGET_DIR) +to_torrents = functions.torrentIdentifier(TORRENT_DIR) + +# create list of torrent files present in from_torrents, not present in to_torrents +torrents = functions.getDiffList(from_torrents, to_torrents) + + +if len(torrents) > 0: + print(f"Found {len(torrents)} new torrents") + functions.moveManager(torrents, TORRENT_DIR) +else: + print("No new torrents found.") + + +for entry in torrents: + print(f"Found torrent file ::: {entry.name}") +# TODO diff directories to compare their contents. + + +# TODO check that the files are in the correct format.