summaryrefslogtreecommitdiffstats
path: root/BitTorrent/GetTorrent.py
blob: a69651c3511293a445b179a34a3b94b7da97c49e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# The contents of this file are subject to the BitTorrent Open Source License
# Version 1.1 (the License).  You may not copy or use this file, in either
# source code or executable form, except in compliance with the License.  You
# may obtain a copy of the License at http://www.bittorrent.com/license/.
#
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
# for the specific language governing rights and limitations under the
# License.

# GetTorrent -- abstraction which can get a .torrent file from multiple
# sources: local file, url, etc.

# written by Matt Chisholm

import os
import re
import zurllib
from bencode import bdecode
from BitTorrent.platform import get_cache_dir

urlpat = re.compile('^\w+://')
urlpat_torrent = re.compile('^torrent://')
urlpat_bittorrent = re.compile('^bittorrent://')

def get_quietly(arg):
    (data, errors) = get(arg)
    # If there's an error opening a file from the IE cache,
    # act like we simply didn't get a file (because we didn't)
    if errors:
        cache = get_cache_dir()
        if (cache is not None) and (cache in arg):
            errors = []
    return data, errors

def get(arg):
    data = None
    errors = []
    if os.access(arg, os.F_OK):
        data, errors = get_file(arg)
    elif urlpat.match(arg):
        data, errors = get_url(arg)
    else:
        errors.append(_("Could not read %s") % arg)
    return data, errors


def get_url(url):
    data = None
    errors = []
    err_str = _("Could not download or open \n%s\n"
                "Try using a web browser to download the torrent file.") % url
    u = None

    # pending protocol changes, convert:
    #   torrent://http://path.to/file
    # and:
    #   bittorrent://http://path.to/file
    # to:
    #   http://path.to/file
    url = urlpat_torrent.sub('', url)
    url = urlpat_bittorrent.sub('', url)
    
    try:
        u = zurllib.urlopen(url)
        data = u.read()
        u.close()
        b = bdecode(data)
    except Exception, e:
        if u is not None:
            u.close()
        errors.append(err_str + "\n(%s)" % e)
        data = None
    else:
        if u is not None:
            u.close()

    return data, errors
    

def get_file(filename):
    data = None
    errors = []
    f = None
    try:
        f = file(filename, 'rb')
        data = f.read()
        f.close()
    except Exception, e:
        if f is not None:
            f.close()
        errors.append((_("Could not read %s") % filename) + (': %s' % str(e)))

    return data, errors