Skip to content

Commit

Permalink
Merge pull request #26 from pseudo-rnd-thoughts/fix-status
Browse files Browse the repository at this point in the history
Change status check to include 4 (finished) and 5 (seeding)
  • Loading branch information
pseudo-rnd-thoughts authored Jan 13, 2023
2 parents 20a0acb + d6a609f commit e9a9856
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions src/AutoROM.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@
}


status_meaning = {
1: "checking files",
2: "downloading metadata",
3: "download",
4: "finished",
5: "seeding",
6: "error, please report",
7: "checking resumedata"
}


def torrent_tar_to_buffer():

# specify the save path
Expand All @@ -153,32 +164,32 @@ def torrent_tar_to_buffer():
success = False
while not success:
if tries > 2:
raise RuntimeError("Failed to download ROMs from torrent, please try again or report this issue.")

try:
tries += 1
timeit = 0

# libtorrent params
ses = lt.session()
params = lt.parse_magnet_uri(uri)
params.save_path = save_path
handle = ses.add_torrent(params)

# download roms as long as state is not seeding
while handle.status().state != 5:
# some sleep helps
time.sleep(1)
timeit += 1

if timeit >= 20:
raise TimeoutError("Took too long to download and reach seeding.")

success = True

except TimeoutError:
pass

raise RuntimeError("Tried to download ROMs 3 times, which have all failed, please try again or report this issue.")

tries += 1

# libtorrent params
ses = lt.session()
params = lt.parse_magnet_uri(uri)
params.save_path = save_path
handle = ses.add_torrent(params)

# download roms as long as state is not seeding
timeit = 1
while handle.status().state not in {4, 5}:
# some sleep helps
time.sleep(1)
timeit += 1

if timeit == 20:
print("Terminating attempt to download ROMs after 20 seconds, trying again", file=sys.stderr)
break
elif timeit % 5 == 0:
print(f"time={timeit}/20 seconds - Trying to download atari roms, "
f"current status={status_meaning.get(handle.status().state, default='unknown')} ({handle.status().state})",
file=sys.stderr)

success = handle.status().state == 5

# read it as a buffer
with open(save_file, "rb") as fh:
Expand Down

0 comments on commit e9a9856

Please sign in to comment.