Browse Source

Pass magnets directly to rTorrent

pull/7038/head
Maciej Urbaniak 9 years ago
parent
commit
0590ac21db
  1. 47
      couchpotato/core/downloaders/rtorrent_.py
  2. 36
      libs/rtorrent/__init__.py

47
couchpotato/core/downloaders/rtorrent_.py

@ -3,6 +3,7 @@ from datetime import timedelta
from hashlib import sha1
from urlparse import urlparse
import os
import re
from couchpotato.core._base.downloader.main import DownloaderBase, ReleaseDownloadList
from couchpotato.core.event import addEvent
@ -146,6 +147,7 @@ class rTorrent(DownloaderBase):
if not self.connect():
return False
torrent_hash = 0
torrent_params = {}
if self.conf('label'):
torrent_params['label'] = self.conf('label')
@ -156,29 +158,42 @@ class rTorrent(DownloaderBase):
# Try download magnet torrents
if data.get('protocol') == 'torrent_magnet':
filedata = self.magnetToTorrent(data.get('url'))
if filedata is False:
# Send magnet to rTorrent
torrent_hash = re.findall('urn:btih:([\w]{32,40})', data.get('url'))[0].upper()
# Send request to rTorrent
try:
torrent = self.rt.load_magnet(data.get('url'), torrent_hash)
if not torrent:
log.error('Unable to find the torrent, did it fail to load?')
return False
except Exception as err:
log.error('Failed to send magnet to rTorrent: %s', err)
return False
data['protocol'] = 'torrent'
if data.get('protocol') == 'torrent':
info = bdecode(filedata)["info"]
torrent_hash = sha1(bencode(info)).hexdigest().upper()
info = bdecode(filedata)["info"]
torrent_hash = sha1(bencode(info)).hexdigest().upper()
# Convert base 32 to hex
if len(torrent_hash) == 32:
torrent_hash = b16encode(b32decode(torrent_hash))
# Convert base 32 to hex
if len(torrent_hash) == 32:
torrent_hash = b16encode(b32decode(torrent_hash))
# Send request to rTorrent
try:
# Send torrent to rTorrent
torrent = self.rt.load_torrent(filedata, verify_retries=10)
# Send request to rTorrent
try:
# Send torrent to rTorrent
torrent = self.rt.load_torrent(filedata, verify_retries=10)
if not torrent:
log.error('Unable to find the torrent, did it fail to load?')
return False
if not torrent:
log.error('Unable to find the torrent, did it fail to load?')
except Exception as err:
log.error('Failed to send torrent to rTorrent: %s', err)
return False
try:
# Set label
if self.conf('label'):
torrent.set_custom(1, self.conf('label'))
@ -191,10 +206,12 @@ class rTorrent(DownloaderBase):
torrent.start()
return self.downloadReturnId(torrent_hash)
except Exception as err:
log.error('Failed to send torrent to rTorrent: %s', err)
return False
def getTorrentStatus(self, torrent):
if not torrent.complete:
return 'busy'

36
libs/rtorrent/__init__.py

@ -129,6 +129,42 @@ class RTorrent:
return(func_name)
def load_magnet(self, magneturl, info_hash, start=False, verbose=False, verify_load=True, verify_retries=3):
p = self._get_conn()
info_hash = info_hash.upper()
func_name = self._get_load_function("url", start, verbose)
# load magnet
getattr(p, func_name)(magneturl)
if verify_load:
i = 0
while i < verify_retries:
for torrent in self.get_torrents():
if torrent.info_hash != info_hash:
continue
time.sleep(1)
i += 1
# Resolve magnet to torrent
torrent.start()
assert info_hash in [t.info_hash for t in self.torrents],\
"Adding magnet was unsuccessful."
i = 0
while i < verify_retries:
for torrent in self.get_torrents():
if torrent.info_hash == info_hash:
if str(info_hash) not in str(torrent.name):
time.sleep(1)
i += 1
return(torrent)
def load_torrent(self, torrent, start=False, verbose=False, verify_load=True, verify_retries=3):
"""
Loads torrent into rTorrent (with various enhancements)

Loading…
Cancel
Save