From 8316712c7525ccb901681ebc000e356a45963cf6 Mon Sep 17 00:00:00 2001 From: MisterDaneel Date: Sat, 16 Feb 2019 18:00:51 +0100 Subject: [PATCH] Add configuration file --- code/torrent.py | 6 ++++-- main.py | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/code/torrent.py b/code/torrent.py index a3abf34..7f6025d 100644 --- a/code/torrent.py +++ b/code/torrent.py @@ -16,8 +16,10 @@ logging.basicConfig(level=logging.DEBUG) class torrent(): - def __init__(self, torrent_tile): - with open(torrent_tile, 'rb') as tf: + def __init__(self, configuration): + self.configuration = configuration + torrent_file = self.configuration['torrent:'] + with open(torrent_file, 'rb') as tf: data = tf.read() self.b_enc = bencoding() self.metainfo = self.b_enc.bdecode(data) diff --git a/main.py b/main.py index 7400d68..b99e70f 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,35 @@ from code.torrent import torrent +import argparse +import json +import sys -to = torrent('test.torrent') -rep = to.tracker_start_request() -to.tracker_process() +def parse_args(): + """Create the arguments""" + parser = argparse.ArgumentParser('\nratio.py -c ') + parser.add_argument("-c", "--configuration", help="Configuration file") + return parser.parse_args() + +def load_configuration(configuration_file): + with open(configuration_file) as f: + configuration = json.load(f) + + if 'torrent' not in configuration: + return None + + return configuration + + +if __name__ == "__main__": + args = parse_args() + if args.configuration: + configuration = load_configuration(args.configuration) + else: + sys.exit() + + if not configuration: + sys.exit() + + to = torrent('test.torrent') + rep = to.tracker_start_request() + to.tracker_process()