from code.decoding_bencoded import bencoding from code.torrentclientfactory import Transmission406 from code.pretty import pretty_data, pretty_GET from hashlib import sha1 from urllib.parse import quote_plus import requests import logging import random from tqdm import tqdm from time import sleep from struct import unpack import os 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): 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) 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'] 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') hash_factory = sha1() hash_factory.update(raw_info) hashed = hash_factory.hexdigest() sha = bytearray.fromhex(hashed) return str(quote_plus(sha)) def send_request(self, params, headers): url = self.metainfo['announce'] print(pretty_GET(url, headers, params)) while True: try: r = requests.get(url, params=params, headers=headers) except requests.exceptions.ConnectionError as e: sleep(1) continue break return r.content def tracker_start_request(self): tc = self.torrentclient headers = tc.get_headers() params = tc.get_query(uploaded=0, downloaded=0, event='started') 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) raw_peers = b_enc.get_dict('peers') i = 0 peers = [] while i