From f051f050a95d4f03d0aaa4cf6aa0aa1be56e7ed9 Mon Sep 17 00:00:00 2001 From: Damien Churchill Date: Wed, 22 Oct 2008 15:07:51 +0000 Subject: [PATCH] move get_torrent_info into a new ui common module --- deluge/ui/common.py | 78 +++++++++++++++++++++++++++++++++++++ deluge/ui/webui/json_api.py | 43 ++------------------ 2 files changed, 81 insertions(+), 40 deletions(-) create mode 100644 deluge/ui/common.py diff --git a/deluge/ui/common.py b/deluge/ui/common.py new file mode 100644 index 000000000..8f3d4ad78 --- /dev/null +++ b/deluge/ui/common.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# deluge/ui/common.py +# +# Copyright (C) Damien Churchill 2008 +# +# 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 + } \ No newline at end of file diff --git a/deluge/ui/webui/json_api.py b/deluge/ui/webui/json_api.py index 74fb5e159..6ad654c8d 100644 --- a/deluge/ui/webui/json_api.py +++ b/deluge/ui/webui/json_api.py @@ -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): """