Add torrent from URL implemented. This is Marcos patch plus some more

error checking and some modifications to make it work properly with 
TorrentManager.
This commit is contained in:
Andrew Resch 2007-09-17 11:07:58 +00:00
parent 6898d8dd17
commit c5156df280
8 changed files with 129 additions and 1 deletions

View File

@ -139,4 +139,24 @@ def ftime(seconds):
return '%dw %dd' % (weeks, days)
return 'unknown'
def is_url(url):
"""A simple regex test to check if the URL is valid."""
import re
return bool(re.search('^(https?|ftp)://', url))
def fetch_url(url):
"""Downloads a torrent file from a given
URL and checks the file's validity."""
import urllib
from deluge.log import LOG as log
try:
filename, headers = urllib.urlretrieve(url)
except IOError:
log.debug("Network error while trying to fetch torrent from %s", url)
else:
if filename.endswith(".torrent") or headers["content-type"] ==\
"application/x-bittorrent":
return filename
else:
log.debug("URL doesn't appear to be a valid torrent file: %s", url)
return None

View File

@ -123,3 +123,4 @@ class Config:
def __setitem__(self, key, value):
self.set(key, value)

View File

@ -163,6 +163,29 @@ class Core(dbus.service.Object):
# Return False because the torrent was not added successfully
return False
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
in_signature="s", out_signature="b")
def add_torrent_url(self, url):
log.info("Attempting to add url %s", url)
# Get the actual filename of the torrent from the url provided.
filename = url.split("/")[-1]
# Get the .torrent file from the url
torrent_file = deluge.common.fetch_url(url)
if torrent_file is None:
return False
# Dump the torrents file contents to a string
try:
filedump = open(torrent_file, "rb").read()
except IOError:
log.warning("Unable to open %s for reading.", torrent_file)
return False
# Add the torrent to session
return self.add_torrent_file(filename, filedump)
@dbus.service.method(dbus_interface="org.deluge_torrent.Deluge",
in_signature="s", out_signature="")
def remove_torrent(self, torrent_id):

View File

@ -92,7 +92,10 @@ class TorrentManager:
# Convert the filedump data array into a string of bytes
if filedump is not None:
filedump = "".join(chr(b) for b in filedump)
# If the filedump is already of type str, then it's already been
# joined.
if type(filedump) is not str:
filedump = "".join(chr(b) for b in filedump)
else:
# Get the data from the file
try:

View File

@ -91,6 +91,14 @@ def add_torrent_file(torrent_files):
# The torrent was not added successfully.
log.warning("Torrent %s was not added successfully.", filename)
def add_torrent_url(torrent_url):
"""Adds torrents to the core via url"""
core = get_core()
result = core.add_torrent_url(torrent_url)
if result is False:
# The torrent url was not added successfully.
log.warning("Torrent %s url was not added successfully.", torrent_url)
def remove_torrent(torrent_ids):
"""Removes torrent_ids from the core.. Expects a list of torrent_ids"""
log.debug("Attempting to removing torrents: %s", torrent_ids)

View File

@ -0,0 +1,70 @@
#
# addtorrenturl.py
#
# Copyright (C) 2007 Marcos Pinto ('markybob') <markybob@gmail.com>
#
# Deluge is free software.
#
# You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# deluge 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 deluge. 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 pygtk
pygtk.require('2.0')
import gtk, gtk.glade
import gettext
from deluge.config import Config
import deluge.common
import pkg_resources
class AddTorrentUrl:
def __init__(self, parent=None):
"""Set up url dialog"""
self.dlg = gtk.Dialog(title=_("Add torrent from URL"), parent=None,
buttons=(gtk.STOCK_CANCEL, 0, gtk.STOCK_OK, 1))
self.dlg.set_icon(deluge.common.get_logo(32))
self.dlg.set_default_response(1)
label = gtk.Label(_("Enter the URL of the .torrent to download"))
self.entry = gtk.Entry()
self.dlg.vbox.pack_start(label)
self.dlg.vbox.pack_start(self.entry)
clip = gtk.clipboard_get(selection='PRIMARY')
text = clip.wait_for_text()
if text:
text = text.strip()
if deluge.common.is_url(text):
self.entry.set_text(text)
def run(self):
"""Show url dialog and add torrent"""
self.dlg.show_all()
self.response = self.dlg.run()
url = self.entry.get_text()
self.dlg.destroy()
if self.response == 1:
return url
else:
return None

View File

@ -41,6 +41,7 @@
<property name="visible">True</property>
<property name="label" translatable="yes">Add _URL</property>
<property name="use_underline">True</property>
<signal name="activate" handler="on_menuitem_addurl_activate"/>
</widget>
</child>
<child>

View File

@ -101,6 +101,8 @@ class MenuBar:
def on_menuitem_addurl_activate(self, data=None):
log.debug("on_menuitem_addurl_activate")
from addtorrenturl import AddTorrentUrl
functions.add_torrent_url(AddTorrentUrl().run())
def on_menuitem_clear_activate(self, data=None):
log.debug("on_menuitem_clear_activate")