Skip to content

Commit

Permalink
Merge pull request Flexget#25 from crawln45/master2
Browse files Browse the repository at this point in the history
Added rtorrent_magnet and tests
  • Loading branch information
gazpachoking committed Jan 31, 2013
2 parents cc59f6d + 4263d09 commit b400d1a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
82 changes: 82 additions & 0 deletions flexget/plugins/output/rtorrent_magnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import logging
import re
import os
import sys

from flexget import plugin
from flexget import validator

log = logging.getLogger('rtorrent_magnet')
pat = re.compile('xt=urn:btih:([^&/]+)')


class PluginRtorrentMagnet(object):
"""
Process Magnet URI's into rtorrent compatible torrent files
Magnet URI's will look somethign like this:
magnet:?xt=urn:btih:190F1ABAED7AE7252735A811149753AA83E34309&dn=URL+Escaped+Torrent+Name
rTorrent would expect to see something like meta-URL_Escaped_Torrent_Name.torrent and the torrent file must contain the text:
d10:magnet-uri88:xt=urn:btih:190F1ABAED7AE7252735A811149753AA83E34309&dn=URL+Escaped+Torrent+Namee
This plugin will check if a download URL is a magnet link, and then create the appropriate torrent file.
Example:
rtorrent_magnet: ~/torrents/
"""

def write_torrent_file(self, task, entry):
path = os.path.join(entry['path'], 'meta-%s.torrent' % entry['title'].encode(sys.getfilesystemencoding(), 'replace'))
log.info('Writing rTorrent Magnet File: %s', path)

if task.manager.options.test:
log.info('Would write: d10:magnet-uri%d:%se' % (entry['url'].__len__(), entry['url']))
else:
with open(path, 'w') as f:
f.write('d10:magnet-uri%d:%se' % (entry['url'].__len__(), entry['url']))
f.closed

entry['output'] = path

def validator(self):
root = validator.factory()
root.accept('path', allow_replacement=True)
return root

@plugin.priority(0)
def on_task_download(self, task, config):
if isinstance(config, basestring) == False:
return

for entry in task.accepted:
if 'output' in entry:
log.debug('Ignoring, %s already has an output file: %s' % (entry['title'], entry['output']))
continue

if entry.get('urls'):
urls = entry.get('urls')
else:
urls = [entry['url']]

for url in urls:
if url.startswith('magnet:'):
log.debug('Magnet URI detected for url %s (%s)' % (url, entry['title']))

m = pat.search(url)
if m:
entry['url'] = url
entry['path'] = entry.get('path', config)
entry['hash'] = m.groups()[0]

log.debug('Magnet Hash Detected: %s' % entry['hash'])

self.write_torrent_file(task, entry)

break
else:
log.warning('Unrecognized Magnet URI Format: %s', url)

plugin.register_plugin(PluginRtorrentMagnet, 'rtorrent_magnet', api_ver=2)
21 changes: 21 additions & 0 deletions tests/test_torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,24 @@ def test_torrent_alive_pass(self):
self.execute_task('test_torrent_alive_pass')
assert self.task.accepted
assert self.task._rerun_count == 0, 'Torrent should have been accepted without rerun.'


class TestRtorrentMagnet(FlexGetBase):
__tmp__ = True
__yaml__ = """
tasks:
test:
mock:
- title: 'test'
url: 'magnet:?xt=urn:btih:HASH&dn=title&tr=http://torrent.ubuntu.com:6969/announce'
rtorrent_magnet: __tmp__
accept_all: yes
"""


def test_rtorrent_magnet(self):
self.execute_task('test')
filename = 'meta-test.torrent'
fullpath = os.path.join(self.__tmp__, filename)
assert os.path.isfile(fullpath)
assert open(fullpath).read() == 'd10:magnet-uri76:magnet:?xt=urn:btih:HASH&dn=title&tr=http://torrent.ubuntu.com:6969/announcee'

0 comments on commit b400d1a

Please sign in to comment.