forked from Flexget/Flexget
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Flexget#25 from crawln45/master2
Added rtorrent_magnet and tests
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters