Add TorrentInfo class and use it in AddTorrentDialog

This commit is contained in:
Andrew Resch 2008-10-23 14:41:51 +00:00
parent 5af5749065
commit 1f0a6eb75e
2 changed files with 54 additions and 39 deletions

View File

@ -36,6 +36,53 @@ from sha import sha
from deluge import bencode
from deluge.log import LOG as log
class TorrentInfo(object):
def __init__(self, filename):
# Get the torrent data from the torrent file
try:
log.debug("Attempting to open %s.", filename)
self.__m_metadata = bencode.bdecode(open(filename, "rb").read())
except Exception, e:
log.warning("Unable to open %s: %s", filename, e)
self.__m_info_hash = sha(bencode.bencode(self.__m_metadata["info"])).hexdigest()
# Get list of files from torrent info
self.__m_files = []
if self.__m_metadata["info"].has_key("files"):
prefix = ""
if len(self.__m_metadata["info"]["files"]) > 1:
prefix = self.__m_metadata["info"]["name"]
for f in self.__m_metadata["info"]["files"]:
self.__m_files.append({
'path': os.path.join(prefix, *f["path"]),
'size': f["length"],
'download': True
})
else:
self.__m_files.append({
"path": self.__m_metadata["info"]["name"],
"size": self.__m_metadata["info"]["length"],
"download": True
})
@property
def name(self):
return self.__m_metadata["info"]["name"]
@property
def info_hash(self):
return self.__m_info_hash
@property
def files(self):
return self.__m_files
@property
def metadata(self):
return self.__m_metadata
def get_torrent_info(filename):
"""
Return the metadata of a torrent file
@ -75,4 +122,4 @@ def get_torrent_info(filename):
"name": metadata["info"]["name"],
"files": files,
"info_hash": info_hash
}
}

View File

@ -45,6 +45,7 @@ import deluge.ui.gtkui.listview as listview
from deluge.configmanager import ConfigManager
from deluge.log import LOG as log
import deluge.common
import deluge.ui.common
class AddTorrentDialog(component.Component):
def __init__(self):
@ -179,51 +180,18 @@ class AddTorrentDialog(component.Component):
break
def add_from_files(self, filenames):
import deluge.bencode
import os.path
new_row = None
for filename in filenames:
# Get the torrent data from the torrent file
try:
log.debug("Attempting to open %s for add.", filename)
metadata = deluge.bencode.bdecode(open(filename, "rb").read())
except Exception, e:
log.warning("Unable to open %s: %s", filename, e)
continue
info = deluge.ui.common.TorrentInfo(filename)
from sha import sha
info_hash = sha(deluge.bencode.bencode(metadata["info"])).hexdigest()
if info_hash in self.infos:
log.debug("Torrent already in list!")
continue
# Get list of files from torrent info
files = []
if metadata["info"].has_key("files"):
prefix = ""
if len(metadata["info"]["files"]) > 1:
prefix = metadata["info"]["name"]
for f in metadata["info"]["files"]:
files.append({
'path': os.path.join(prefix, *f["path"]),
'size': f["length"],
'download': True
})
else:
files.append({
"path": metadata["info"]["name"],
"size": metadata["info"]["length"],
"download": True
})
name = "%s (%s)" % (metadata["info"]["name"], os.path.split(filename)[-1])
name = "%s (%s)" % (info.name, os.path.split(filename)[-1])
new_row = self.torrent_liststore.append(
[info_hash, name, filename])
self.files[info_hash] = files
self.infos[info_hash] = metadata
[info.info_hash, info.name, filename])
self.files[info.info_hash] = info.files
self.infos[info.info_hash] = info.metadata
(model, row) = self.listview_torrents.get_selection().get_selected()
if not row and new_row: