move get_torrent_info into a new ui common module

This commit is contained in:
Damien Churchill 2008-10-22 15:07:51 +00:00
parent e09a8f79a8
commit f051f050a9
2 changed files with 81 additions and 40 deletions

78
deluge/ui/common.py Normal file
View File

@ -0,0 +1,78 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# deluge/ui/common.py
#
# Copyright (C) Damien Churchill 2008 <damoxc@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor
# Boston, MA 02110-1301, USA.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library.
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.
import os
from sha import sha
from deluge import bencode
from deluge.log import LOG as log
def get_torrent_info(filename):
"""
Return the metadata of a torrent file
"""
# Get the torrent data from the torrent file
try:
log.debug("Attempting to open %s.", filename)
metadata = bencode.bdecode(open(filename, "rb").read())
except Exception, e:
log.warning("Unable to open %s: %s", filename, e)
info_hash = sha(bencode.bencode(metadata["info"])).hexdigest()
# 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
})
return {
"filename": filename,
"name": metadata["info"]["name"],
"files": files,
"info_hash": info_hash
}

View File

@ -54,6 +54,7 @@ try:
except ImportError:
from lib.json import write as dumps, read as loads
from deluge.ui import common
from deluge.ui.client import sclient,aclient
from deluge.log import LOG as log
from deluge import component
@ -208,7 +209,7 @@ class json_rpc:
allow the webui to retrieve data about the torrent
input:
url: the url of the torrent to download
filename: the filename of the torrent to gather info about
returns:
{
@ -219,45 +220,7 @@ class json_rpc:
"info_hash" the torrents info_hash
}
"""
import os
import deluge.bencode
# 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)
from sha import sha
info_hash = sha(deluge.bencode.bencode(metadata["info"])).hexdigest()
# 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
})
return {
"filename": filename,
"name": metadata["info"]["name"],
"files": files,
"info_hash": info_hash
}
return common.get_torrent_info(filename)
def add_torrents(self, torrents):
"""