def fool_around(self): """ fool the trackers around """ try: self._load_torrents() except Exception: log.exception("exception in fool_around") ptl_exit(2) self.started = time() try: while True: self._fool_around() except Exception: log.exception("exception in fool_around")
def main(argv): interrupt_off() try: opts, args = getopt(argv, "c:DefhM:m:nlp:s:t:vz") except GetoptError: _usage() ptl_exit(2) def parse_int(i): if i.isdigit(): return int(i) return 0 for opt, arg in opts: if opt == "-h": _usage() ptl_exit(0) if opt == "-l": client_list() ptl_exit(0) if opt == "-m": ps.max_up_speed = parse_int(arg) * KILO if ps.max_up_speed <= 0: ptl_error( "max upload bandwidth must be a positive integer, in KB/s") elif opt == "-M": ps.max_down_speed = parse_int(arg) * KILO if ps.max_down_speed <= 0: ptl_error( "max download bandwidth must be a positive integer, in KB/s" ) elif opt == "-s": ps.max_torrent_speed = parse_int(arg) * KILO if ps.max_torrent_speed <= 0: ptl_error( "max torrent speed must be a positive integer, in KB/s") elif opt == "-p": ps.port = parse_int(arg) if ps.port < MIN_PORT or ps.port > MAX_PORT: ptl_error("port number should be within (%s, %s)" % \ (MIN_PORT, MAX_PORT)) elif opt == "-v": ps.logging_level = DEBUG elif opt == "-e": ps.use_ipv6 = True elif opt == "-z": ps.use_zero_rate = True elif opt == "-f": ps.no_sleep = True elif opt == "-n": ps.no_scrape = True elif opt == "-c": if arg not in BT_CLIENTS: ptl_error("client not in supported client-list, see option -l") ps.client_id = arg elif opt == "-t": ps.timer = parse_int(arg) * HOUR if ps.timer < 1: ptl_error("timer must be a positive integer, in hours") print BANNER % {"version": __version__, "date": __date__, "url": __url__} if not ps.no_sleep: sleep(SLEEP_BANNER) ps.fuck_yourself() ts.fuck_yourself() if not ps.no_sleep: sleep(SLEEP_SETTINGS) tm.fool_around()
def _fool_around(self): def run_threads(box): for th in box: th.start() for th in box: th.join() thread_box = [] for torrent in self.torrents.values(): if not torrent.is_ready: continue thread_box.append(Thread(target=torrent.commit)) if len(thread_box) == CONNECTION_PER_BOX: run_threads(thread_box) thread_box = [] if not ps.no_sleep: sleep(SLEEP_THREAD) run_threads(thread_box) # calculate committed values all_torrents = self.torrents.values() uploaded = 0 downloaded = 0 for torrent in all_torrents: uploaded += torrent.uploaded downloaded += torrent.downloaded log.info("time: %s up_speed: %s/s down_speed: %s/s "\ "committed [up: %s down: %s]" % \ (ptime(time() - self.started), psize(ts.up_speed), psize(ts.down_speed), psize(uploaded), psize(downloaded))) if self.done: # say goodbye elapsed = time() - self.started log.info("time elapsed: %s" % ptime(elapsed)) log.info("avg up speed: %s/s" % psize(uploaded / elapsed)) log.info("avg down speed: %s/s" % psize(downloaded / elapsed)) log.debug("<= PTLiar ended") print "Bye~" ptl_exit(0) active_torrents = filter(lambda t: t.status != "error", all_torrents) if len(active_torrents) < 1: ptl_error("no torrents available") # calculate how long should we sleep next_commit = min(map(lambda t: t.next_commit_time, active_torrents)) left = max(0, next_commit - time()) # sleep one more second than needed zzz = min(SLEEP_SCAN, left + 1) log.info("next commit: %s from now, sleep for %s.." % \ (ptime(left), ptime(zzz))) print "press [Ctrl+C] to leave" try: interrupt_on() sleep(zzz) interrupt_off() except (KeyboardInterrupt, SystemExit): # gracefully shutdown interrupt_off() self.done = True if time() >= self.started + ps.timer: # timer self.done = True if self.done: log.info("stopping...") for torrent in active_torrents: torrent.status = "stopped" return # check whether we've got new torrent self._load_torrents()