mirror of
https://github.com/codex-storage/deluge.git
synced 2025-01-12 04:24:27 +00:00
Refactor Torrent _get_pieces_info method
Code is now easier to read and should be a bit faster
This commit is contained in:
parent
48f79dbfca
commit
b4b58380b6
@ -19,6 +19,7 @@ from __future__ import division
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
from urlparse import urlparse
|
from urlparse import urlparse
|
||||||
|
from itertools import izip
|
||||||
|
|
||||||
from twisted.internet.defer import Deferred, DeferredList
|
from twisted.internet.defer import Deferred, DeferredList
|
||||||
from deluge._libtorrent import lt
|
from deluge._libtorrent import lt
|
||||||
@ -1307,40 +1308,20 @@ class Torrent(object):
|
|||||||
|
|
||||||
def _get_pieces_info(self):
|
def _get_pieces_info(self):
|
||||||
"""Get the pieces for this torrent."""
|
"""Get the pieces for this torrent."""
|
||||||
if not self.has_metadata:
|
if not self.has_metadata or self.status.is_seeding:
|
||||||
return None
|
pieces = None
|
||||||
|
else:
|
||||||
|
pieces = []
|
||||||
|
for piece, avail_piece in izip(self.status.pieces, self.handle.piece_availability()):
|
||||||
|
if piece:
|
||||||
|
pieces.append(3) # Completed.
|
||||||
|
elif avail_piece:
|
||||||
|
pieces.append(1) # Available, just not downloaded nor being downloaded.
|
||||||
|
else:
|
||||||
|
pieces.append(0) # Missing, no known peer with piece, or not asked for yet.
|
||||||
|
|
||||||
pieces = {}
|
for peer_info in self.handle.get_peer_info():
|
||||||
# First get the pieces availability.
|
if peer_info.downloading_piece_index >= 0:
|
||||||
availability = self.handle.piece_availability()
|
pieces[peer_info.downloading_piece_index] = 2 # Being downloaded from peer.
|
||||||
# Pieces from connected peers
|
|
||||||
for peer_info in self.handle.get_peer_info():
|
|
||||||
if peer_info.downloading_piece_index < 0:
|
|
||||||
# No piece index, then we're not downloading anything from
|
|
||||||
# this peer
|
|
||||||
continue
|
|
||||||
pieces[peer_info.downloading_piece_index] = 2
|
|
||||||
|
|
||||||
# Now, the rest of the pieces
|
return pieces
|
||||||
for idx, piece in enumerate(self.status.pieces):
|
|
||||||
if idx in pieces:
|
|
||||||
# Piece beeing downloaded, handled above
|
|
||||||
continue
|
|
||||||
elif piece:
|
|
||||||
# Completed Piece
|
|
||||||
pieces[idx] = 3
|
|
||||||
continue
|
|
||||||
elif availability[idx] > 0:
|
|
||||||
# Piece not downloaded nor beeing downloaded but available
|
|
||||||
pieces[idx] = 1
|
|
||||||
continue
|
|
||||||
# If we reached here, it means the piece is missing, ie, there's
|
|
||||||
# no known peer with this piece, or this piece has not been asked
|
|
||||||
# for so far.
|
|
||||||
pieces[idx] = 0
|
|
||||||
|
|
||||||
sorted_indexes = pieces.keys()
|
|
||||||
sorted_indexes.sort()
|
|
||||||
# Return only the piece states, no need for the piece index
|
|
||||||
# Keep the order
|
|
||||||
return [pieces[idx] for idx in sorted_indexes]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user