Skip to content

Commit

Permalink
added functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
williameast committed Jul 6, 2022
1 parent 1cf0de8 commit ce8e065
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 207 deletions.
12 changes: 1 addition & 11 deletions connectionhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def disconnect(self):
def changeWorkingDirectory(self, remotePath):
try:
self.connection.cwd(remotePath)
print(f"changed remote directory to {remotePath}")
# print(f"changed remote directory to {remotePath}")
except Exception as e:
raise Exception(e)

Expand Down Expand Up @@ -156,16 +156,6 @@ def downloadRemoteDir(
overwrite=False,
guess_by_extension=True,
):
"""
Downloads an entire directory tree from an ftp server to the local destination
:param path: the folder on the ftp server to download
:param destination: the local directory to store the copied folder
:param pattern: Python regex pattern, only files that match this pattern will be downloaded.
:param overwrite: set to True to force re-download of all files, even if they appear to exist already
:param guess_by_extension: It takes a while to explicitly check if every item is a directory or a file.
if this flag is set to True, it will assume any file ending with a three character extension ".???" is
a file and not a directory. Set to False if some folders may have a "." in their names -4th position.
"""
path = path.lstrip("/")
original_directory = (
os.getcwd()
Expand Down
108 changes: 4 additions & 104 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def checkMimes(file, allowed_extensions):


def checkIfFolder(path):
"""Check if a path is a folder or a file."""
if os.path.splitext(path)[1] == "":
is_Folder = True
else:
Expand All @@ -26,6 +27,7 @@ def checkIfFolder(path):


def torrentIdentifier(directory):
"""generates a list of torrents - if torrent is file and ends in .torrent, add it to list."""
torrents = []
with os.scandir(directory) as localdir:
for entry in localdir:
Expand All @@ -44,6 +46,7 @@ def getDiffList(a, b):

# TODO: handle file already exists
def moveManager(torrents, torrent_dir):
"""move files (expects posix) from target directory to torrent directory"""
for torrent in torrents:
try:
shutil.move(torrent.path, torrent_dir)
Expand All @@ -52,6 +55,7 @@ def moveManager(torrents, torrent_dir):


def yamlDataExtract(config_file="config.yaml"):
"""Handle the yaml data, load it into the space safely."""
with open("config.yaml", "r") as config:
try:
data = yaml.safe_load(config)
Expand All @@ -72,107 +76,3 @@ def getFileNamefromTorrent(torrent):
"""must be a direntry item. Gets the name of the torrent's finished folder from the .torrent file."""
torrent_info = lt.torrent_info(torrent.path)
return torrent_info.name()


###############################################################################
# Below needs integration. #
###############################################################################


def _is_ftp_dir(ftp_handle, name, guess_by_extension=True):
"""simply determines if an item listed on the ftp server is a valid directory or not"""

# if the name has a "." in the fourth to last position, its probably a file extension
# this is MUCH faster than trying to set every file to a working directory, and will work 99% of time.
if guess_by_extension is True:
if len(name) >= 4:
if name[-4] == ".":
return False

original_cwd = ftp_handle.pwd() # remember the current working directory
try:
ftp_handle.cwd(name) # try to set directory to new name
ftp_handle.cwd(original_cwd) # set it back to what it was
return True

except ftplib.error_perm as e:
print(e)
return False

except Exception as e:
print(e)
return False


def _make_parent_dir(fpath):
"""ensures the parent directory of a filepath exists"""
dirname = os.path.dirname(fpath)
while not os.path.exists(dirname):
try:
os.makedirs(dirname)
print("created {0}".format(dirname))
except OSError as e:
print(e)
_make_parent_dir(dirname)


def _download_ftp_file(ftp_handle, name, dest, overwrite):
"""downloads a single file from an ftp server"""
_make_parent_dir(dest.lstrip("/"))
if not os.path.exists(dest) or overwrite is True:
try:
with open(dest, "wb") as f:
ftp_handle.retrbinary("RETR {0}".format(name), f.write)
print("downloaded: {0}".format(dest))
except FileNotFoundError:
print("FAILED: {0}".format(dest))
else:
print("already exists: {0}".format(dest))


def _file_name_match_patern(pattern, name):
"""returns True if filename matches the pattern"""
if pattern is None:
return True
else:
return bool(re.match(pattern, name))


def _mirror_ftp_dir(ftp_handle, name, overwrite, guess_by_extension, pattern):
"""replicates a directory on an ftp server recursively"""
for item in ftp_handle.nlst(name):
if _is_ftp_dir(ftp_handle, item, guess_by_extension):
_mirror_ftp_dir(ftp_handle, item, overwrite, guess_by_extension, pattern)
else:
if _file_name_match_patern(pattern, name):
_download_ftp_file(ftp_handle, item, item, overwrite)
else:
# quietly skip the file
pass


def download_ftp_tree(
ftp_handle,
path,
destination,
pattern=None,
overwrite=False,
guess_by_extension=True,
):
path = path.lstrip("/")
original_directory = (
os.getcwd()
) # remember working directory before function is executed
os.chdir(destination) # change working directory to ftp mirror directory

_mirror_ftp_dir(
ftp_handle,
path,
pattern=pattern,
overwrite=overwrite,
guess_by_extension=guess_by_extension,
)

os.chdir(
original_directory
) # reset working directory to what it was before function exec
184 changes: 92 additions & 92 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,95 @@
import functions
import connectionhandler

# handle argument parsing
parser = argparse.ArgumentParser(
description="This tool finds .torrent files in your download folder\
and gives the option to move them directly into a shared seedbox via\
FTP. it also allows for download of that file, as well as moving the\
.torrent files somewhere convenient after processing."
)
# only upload files to seedbox.io,
parser.add_argument(
"--upload",
"-u",
action="store_true",
help="upload files to seedbox.io using the\
credentials in the config",
)
parser.add_argument(
"--print",
"-p",
action="store_true",
help="print files in inbound torrent folder",
)
parser.add_argument(
"--move",
"-m",
action="store_true",
help="move files in inbound folder to outbound torrent folder",
) # move torrent files locally
# parser.add_argument(
# "--download",
# "-d",
# action="store_true",
# help="Download torrents from seedbox.io to the specified folder.",
# ) # download torrents
args = parser.parse_args()


# Handle the config file
config = functions.yamlDataExtract()

TORRENT_DIR = config["torrent_dir"]
TARGET_DIR = config["target_dir"]
SEEDBOX_ADDR = config["seedbox_addr"]
SEEDBOX_LOGIN = config["seedbox_login"]
SEEDBOX_PW = config["seedbox_pw"]


# create list of torrent files present in from_torrents, not present in to_torrents
torrents = functions.getTorrentDiffList(TARGET_DIR, TORRENT_DIR)

num_torrents = len(torrents)

if num_torrents == 0:
print(f"Aborting because no torrents were found in {TARGET_DIR}")
sys.exit()

if args.print or args.download or args.upload is True:
for entry in torrents:
print(f"Found torrent file ::: {entry.name}")

if args.upload is True:

sftp = connectionhandler.SeedboxFTP(SEEDBOX_ADDR, SEEDBOX_LOGIN, SEEDBOX_PW)

print(f"attempting to connect to {SEEDBOX_ADDR}")
sftp.connect()

print(f"attempting to upload {num_torrents} to {SEEDBOX_ADDR}.")
# set CWD to Watch folder
sftp.changeWorkingDirectory(remotePath="watch")

# loop through torrent list, and send them to the seedbox
for torrent in torrents:
sftp.testUpload(torrent)
# disconnect
sftp.disconnect()

if args.download is True:

sftp.changeWorkingDirectory(remotePath="/files/Completed Downloads")

for torrent in torrents:
torrentfile = functions.getFileNamefromTorrent(torrent)
print(f"processing {torrentfile}")
sftp.downloadRemoteDir(
torrentfile,
destination=TARGET_DIR,
)
sftp.disconnect()

if args.move is True:
print(f"moving torrents to {TORRENT_DIR}")
functions.moveManager(torrents, TORRENT_DIR)

if __name__ == "__main__":
# handle argument parsing
parser = argparse.ArgumentParser(
description="This tool finds .torrent files in your download folder\
and gives the option to move them directly into a shared seedbox via\
FTP. it also allows for download of that file, as well as moving the\
.torrent files somewhere convenient after processing."
)
# only upload files to seedbox.io,
parser.add_argument(
"--upload",
"-u",
action="store_true",
help="upload files to seedbox.io using the\
credentials in the config",
)
parser.add_argument(
"--print",
"-p",
action="store_true",
help="print files in inbound torrent folder",
)
parser.add_argument(
"--move",
"-m",
action="store_true",
help="move files in inbound folder to outbound torrent folder",
) # move torrent files locally
parser.add_argument(
"--download",
"-d",
action="store_true",
help="Download torrents from seedbox.io to the specified folder.",
) # download torrents
args = parser.parse_args()

# Handle the config file
config = functions.yamlDataExtract()

TORRENT_DIR = config["torrent_dir"]
TARGET_DIR = config["target_dir"]
SEEDBOX_ADDR = config["seedbox_addr"]
SEEDBOX_LOGIN = config["seedbox_login"]
SEEDBOX_PW = config["seedbox_pw"]

# create list of torrent files present in from_torrents, not present in to_torrents
torrents = functions.getTorrentDiffList(TARGET_DIR, TORRENT_DIR)

num_torrents = len(torrents)

if num_torrents == 0:
print(f"Aborting because no torrents were found in {TARGET_DIR}")
sys.exit()

if args.print or args.download or args.upload is True:
for entry in torrents:
print(f"Found torrent file ::: {entry.name}")

if args.upload is True:

sftp = connectionhandler.SeedboxFTP(SEEDBOX_ADDR, SEEDBOX_LOGIN, SEEDBOX_PW)

print(f"attempting to connect to {SEEDBOX_ADDR}")
sftp.connect()

print(f"attempting to upload {num_torrents} to {SEEDBOX_ADDR}.")
# set CWD to Watch folder
sftp.changeWorkingDirectory(remotePath="watch")

# loop through torrent list, and send them to the seedbox
for torrent in torrents:
sftp.testUpload(torrent)
# disconnect
sftp.disconnect()

# if args.download is True:

# sftp.changeWorkingDirectory(remotePath="/files/Completed Downloads")

# for torrent in torrents:
# torrentfile = functions.getFileNamefromTorrent(torrent)
# print(f"processing {torrentfile}")
# sftp.downloadRemoteDir(
# torrentfile,
# destination=TARGET_DIR,
# )
# sftp.disconnect()

if args.move is True:
print(f"moving torrents to {TORRENT_DIR}")
functions.moveManager(torrents, TORRENT_DIR)

0 comments on commit ce8e065

Please sign in to comment.