~jderose/dmedia/filestore-ui

« back to all changes in this revision

Viewing changes to dmedia/backends/torrent.py

  • Committer: Jason Gerard DeRose
  • Date: 2012-04-05 10:22:53 UTC
  • Revision ID: jderose@novacut.com-20120405102253-9yegbmwnsqqsg5hd
Removed a bunch of cruft from before the refactor, re-enabled auto-discovery of all modules to test

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Authors:
2
 
#   Jason Gerard DeRose <jderose@novacut.com>
3
 
#
4
 
# dmedia: distributed media library
5
 
# Copyright (C) 2011 Jason Gerard DeRose <jderose@novacut.com>
6
 
#
7
 
# This file is part of `dmedia`.
8
 
#
9
 
# `dmedia` is free software: you can redistribute it and/or modify it under the
10
 
# terms of the GNU Affero General Public License as published by the Free
11
 
# Software Foundation, either version 3 of the License, or (at your option) any
12
 
# later version.
13
 
#
14
 
# `dmedia` is distributed in the hope that it will be useful, but WITHOUT ANY
15
 
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16
 
# A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17
 
# details.
18
 
#
19
 
# You should have received a copy of the GNU Affero General Public License along
20
 
# with `dmedia`.  If not, see <http://www.gnu.org/licenses/>.
21
 
 
22
 
"""
23
 
Download with BitTorrent.
24
 
"""
25
 
 
26
 
import logging
27
 
import time
28
 
from os import path
29
 
 
30
 
import libtorrent
31
 
 
32
 
from dmedia.constants import LEAF_SIZE
33
 
from dmedia.transfers import HTTPBaseBackend, register_downloader, http_conn
34
 
 
35
 
 
36
 
log = logging.getLogger()
37
 
 
38
 
 
39
 
class TorrentBackend(HTTPBaseBackend):
40
 
    """
41
 
    Backend for BitTorrent downloads using `libtorrent`.
42
 
    """
43
 
 
44
 
    def download(self, doc, leaves, fs):
45
 
        chash = doc['_id']
46
 
        ext = doc.get('ext')
47
 
        url = self.basepath + self.key(chash, ext) + '?torrent'
48
 
        data = self.get(url)
49
 
 
50
 
        tmp = fs.tmp(chash, ext, create=True)
51
 
        session = libtorrent.session()
52
 
        session.listen_on(6881, 6891)
53
 
 
54
 
        info = libtorrent.torrent_info(libtorrent.bdecode(data))
55
 
 
56
 
        torrent = session.add_torrent({
57
 
            'ti': info,
58
 
            'save_path': path.dirname(tmp),
59
 
        })
60
 
 
61
 
        while not torrent.is_seed():
62
 
            s = torrent.status()
63
 
            self.progress(s.total_payload_download)
64
 
            time.sleep(2)
65
 
 
66
 
        session.remove_torrent(torrent)
67
 
        time.sleep(2)
68
 
 
69
 
 
70
 
register_downloader('torrent', TorrentBackend)