Skip to content

Commit

Permalink
added local file mgmt and config parser
Browse files Browse the repository at this point in the history
  • Loading branch information
williameast committed May 28, 2022
1 parent 25af5a5 commit 07d554e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
65 changes: 65 additions & 0 deletions functions.py
Original file line number Diff line number Diff line change
@@ -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)
53 changes: 53 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 07d554e

Please sign in to comment.