Skip to content

Commit 4492b22

Browse files
committed
fix bug
1 parent 274ec8b commit 4492b22

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

fetchMetadata.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ def recv_piece(s, timeout=5):
104104
return pkg
105105

106106

107+
def get_length(metadata):
108+
length = 0
109+
if metadata.get(b'files'):
110+
for f in metadata[b'files']:
111+
length += x[b'length']
112+
else:
113+
length += metadata[b'length']
114+
115+
if length < 1024:
116+
return str(length) + "B"
117+
elif length < 1024 ** 2:
118+
return str(int(length / 1024)) + "KB"
119+
else:
120+
return str(int(length / 1024 / 1024)) + "MB"
121+
107122
def fetch_metadata(nid, infohash, address, timeout=5):
108123
try:
109124
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -132,13 +147,13 @@ def fetch_metadata(nid, infohash, address, timeout=5):
132147
piece = recv_piece(s, timeout)
133148
metadata.append(piece)
134149

135-
metadata = b"".join(metadata)
136-
print(bencodepy.decode(metadata)[b"name"].decode(), "size", len(metadata))
137-
150+
metadata = bencodepy.decode(b"".join(metadata))
151+
print(metadata[b"name"].decode(), "size", get_length(metadata))
138152
except socket.timeout:
139153
pass
140154
except Exception as e:
141155
#print(e)
142156
pass
143157
finally:
144-
s.close()
158+
if s:
159+
s.close()

0 commit comments

Comments
 (0)