Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: utf-8 -*- 

# 

# Copyright (C) 2008-2009 Ido Abramovich <ido.deluge@gmail.com> 

# Copyright (C) 2009 Andrew Resch <andrewresch@gmail.com> 

# Copyright (C) 2011 Nick Lanham <nick@afternight.org> 

# 

# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with 

# the additional special exception to link portions of this program with the OpenSSL library. 

# See LICENSE for more details. 

# 

 

import base64 

import glob 

import logging 

import os 

 

import deluge.common 

from deluge.ui.client import client 

from deluge.ui.common import TorrentInfo 

 

log = logging.getLogger(__name__) 

 

 

def _bracket_fixup(path): 

    if (path.find("[") == -1 and path.find("]") == -1): 

        return path 

    sentinal = 256 

    while (path.find(unichr(sentinal)) != -1): 

        sentinal += 1 

        if sentinal > 65535: 

            log.error("Can't fix brackets in path, path contains all possible sentinal characters") 

            return path 

    newpath = path.replace("]", unichr(sentinal)) 

    newpath = newpath.replace("[", "[[]") 

    newpath = newpath.replace(unichr(sentinal), "[]]") 

    return newpath 

 

 

def add_torrent(t_file, options, success_cb, fail_cb, ress): 

    t_options = {} 

    if options["path"]: 

        t_options["download_location"] = os.path.expanduser(options["path"]) 

    t_options["add_paused"] = options["add_paused"] 

 

    is_url = (not (options["path_type"] == 1)) and (deluge.common.is_url(t_file) or options["path_type"] == 2) 

    is_magnet = not(is_url) and (not (options["path_type"] == 1)) and deluge.common.is_magnet(t_file) 

 

    if is_url or is_magnet: 

        files = [t_file] 

    else: 

        files = glob.glob(_bracket_fixup(t_file)) 

    num_files = len(files) 

    ress["total"] = num_files 

 

    if num_files <= 0: 

        fail_cb("Doesn't exist", t_file, ress) 

 

    for f in files: 

        if is_url: 

            client.core.add_torrent_url(f, t_options).addCallback(success_cb, f, ress).addErrback(fail_cb, f, ress) 

        elif is_magnet: 

            client.core.add_torrent_magnet(f, t_options).addCallback(success_cb, f, ress).addErrback(fail_cb, f, ress) 

        else: 

            if not os.path.exists(f): 

                fail_cb("Doesn't exist", f, ress) 

                continue 

            if not os.path.isfile(f): 

                fail_cb("Is a directory", f, ress) 

                continue 

 

            try: 

                TorrentInfo(f) 

            except Exception as e: 

                fail_cb(e.message, f, ress) 

                continue 

 

            filename = os.path.split(f)[-1] 

            filedump = base64.encodestring(open(f).read()) 

 

            client.core.add_torrent_file(filename, filedump, t_options 

                                         ).addCallback(success_cb, f, ress).addErrback(fail_cb, f, ress)