Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Then configure your `config.json` file.

```js
{
"torrent": "<Torrent file path>",
"torrent": "<Torrent folder path>",
"upload": "<Upload speed (kB/s)>"
}
```
Expand Down
80 changes: 38 additions & 42 deletions code/process_torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
from time import sleep

from struct import unpack
import os

logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(level=logging.INFO)

class process_torrent():

def __init__(self, configuration):
self.configuration = configuration
self.open_torrent()
self.timer = 0
self.torrentclient = Transmission406(self.tracker_info_hash())

def open_torrent(self):
Expand All @@ -28,11 +29,14 @@ def open_torrent(self):
self.b_enc = bencoding()
self.metainfo = self.b_enc.bdecode(data)
self.info = self.metainfo['info']
self.files = []
if 'length' not in self.info:
self.info['length'] = 0
for file in self.info['files']:
self.info['length'] += file['length']
print(pretty_data(self.info['files']))
self.files.append(file['path'])
# print(pretty_data(self.info['files']))
# print(self.files)

def tracker_info_hash(self):
raw_info = self.b_enc.get_dict('info')
Expand Down Expand Up @@ -60,16 +64,12 @@ def tracker_start_request(self):
params = tc.get_query(uploaded=0,
downloaded=0,
event='started')

print('----------- First Command to Tracker --------')
content = self.send_request(params, headers)
self.tracker_response_parser(content)

def tracker_response_parser(self, tr_response):
b_enc = bencoding()
response = b_enc.bdecode(tr_response)
print('----------- Received Tracker Response --------')
print(pretty_data(response))
raw_peers = b_enc.get_dict('peers')
i = 0
peers = []
Expand All @@ -83,38 +83,34 @@ def tracker_response_parser(self, tr_response):
peers.append((ip, port))
self.interval = response['interval']

def wait(self):
random_badtime = random.randint(10,15)*60 # interval to send request betwen 10min and 15min
self.interval = random_badtime
pbar = tqdm(total=self.interval)
print('sleep: {}'.format(self.interval))
t = 0
while t < (self.interval):
t += 1
pbar.update(1)
sleep(1)
pbar.close()

def tracker_process(self):
while True:
self.tracker_start_request()

print('----------- Sending Command to Tracker --------')

# get upload
min_up = self.interval-(self.interval*0.1)
max_up = self.interval
randomize_upload = random.randint(min_up, max_up)
uploaded = int(self.configuration['upload'])*1000*randomize_upload

# get download
downloaded = 0

tc = self.torrentclient
headers = tc.get_headers()
params = tc.get_query(uploaded=uploaded,
downloaded=downloaded,
event='stopped')
content = self.send_request(params, headers)
self.tracker_response_parser(content)
self.wait()
def seedqueue(queue, time):
while time > 0:
waitingqueue = ""
for torrent in queue:
if torrent.timer <= 0:
torrent.tracker_start_request()

min_up = torrent.interval-(torrent.interval*0.1)
max_up = torrent.interval
randomize_upload = random.randint(min_up, max_up)
uploaded = int(torrent.configuration['upload'])*1000*randomize_upload

downloaded = 0

tc = torrent.torrentclient
headers = tc.get_headers()
params = tc.get_query(uploaded=uploaded,
downloaded=downloaded,
event='stopped')
content = torrent.send_request(params, headers)
torrent.tracker_response_parser(content)

torrent.timer = random.randint(10,15)*60 # interval to send request betwen 10min and 15min
torrent.interval = torrent.timer
else:
torrent.timer -= 1
waitingqueue += f"Waiting {torrent.timer} seconds for {torrent.configuration['torrent']}" + "\n"
os.system('cls' if os.name == 'nt' else 'clear')
print(waitingqueue)
sleep(1)
time -= 1
2 changes: 1 addition & 1 deletion code/torrentclientfactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ def generate_peer_id(self):
def generate_key(self):
chars = 'ABCDEF' + string.digits
key = self.id_generator(chars, 8)
return key
return key
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"torrent": "./file.torrent",
"upload":"350"
"torrents": "torrentsfolder",
"upload": "350"
}
48 changes: 38 additions & 10 deletions ratio.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,62 @@
from code.process_torrent import process_torrent
from code.process_torrent import process_torrent, seedqueue
import argparse
import json
import sys
import os

def parse_args():
"""Create the arguments"""
parser = argparse.ArgumentParser('\nratio.py -c <configuration-file.json>')
parser = argparse.ArgumentParser(description="Fake ratio")
parser.add_argument("-c", "--configuration", help="Configuration file")
parser.add_argument("-t", "--time", help="Time to seed", type=str, default="1d")
# parser.add_argument("-s", "--speed", help="Speed to seed", type=str, default="350")
return parser.parse_args()

def load_configuration(configuration_file):
with open(configuration_file) as f:
configuration = json.load(f)

if 'torrent' not in configuration:
if 'torrents' not in configuration:
return None

return configuration

def get_time(timestring):
days = hours = minutes = seconds = 0
if 'd' in timestring:
days, timestring = timestring.split('d')
days = int(days)
if 'h' in timestring:
hours, timestring = timestring.split('h')
hours = int(hours)
if 'm' in timestring:
minutes, timestring = timestring.split('m')
minutes = int(minutes)
if 's' in timestring:
seconds = int(timestring.split('s')[0])
total_seconds = days * 86400 + hours * 3600 + minutes * 60 + seconds
return total_seconds

if __name__ == "__main__":
queue = []
args = parse_args()
if args.configuration:
configuration = load_configuration(args.configuration)
else:
sys.exit()

if not configuration:
sys.exit()

to = process_torrent(configuration)
to.tracker_process()

folder_path = configuration['torrents']
torrents_path = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(".torrent"):
torrents_path.append(os.path.join(root, file))
for torrent_file in torrents_path:
config = {
"torrent": torrent_file,
"upload": configuration['upload']
}
torrent = process_torrent(config)
queue.append(torrent)
print(f'Got {len(queue)} torrents')
time = get_time(args.time)
seedqueue(queue, time)