Exemplo n.º 1
0
    def add_torrent(self, torrent_path, download_path=None):
        if not self.connected:
            ret = ClientRet(ret_type=-2)
            return ret

        options = {
        }  # Ref: https://github.com/deluge-torrent/deluge/blob/1.3-stable/deluge/core/torrent.py
        options['add_paused'] = False
        if download_path is not None:
            options['download_location'] = str(Path(download_path).resolve())

        abs_torrent_path = str(Path(torrent_path).resolve())
        torrent_content = open(abs_torrent_path, 'rb').read()
        torrent_base64 = base64.b64encode(torrent_content)

        torrent_idx = self.client.core.add_torrent_file(
            filename=abs_torrent_path,
            filedump=torrent_base64,
            options=options)

        if torrent_idx is not None:
            ret = ClientRet(ret_type=3, ret_value=torrent_idx.decode())
        else:
            ret = ClientRet(ret_type=-3)

        return ret
Exemplo n.º 2
0
    def connect(self):
        self.client.connect()
        self.connected = self.client.connected

        if self.connected:
            ret = ClientRet(ret_type=2)
        else:
            ret = ClientRet(ret_type=-2)
        return ret
Exemplo n.º 3
0
    def connect(self):
        login_ret = self.client.login(username=self.username,
                                      password=self.password)

        if login_ret is None:
            self.connected = True
            ret = ClientRet(ret_type=2)
        else:
            ret = ClientRet(ret_type=-2)

        return ret
Exemplo n.º 4
0
 def connect(self):
     try:
         self.client = Client(host=self.rpc_address,
                              port=self.rpc_port,
                              username=self.username,
                              password=self.password,
                              path=self.path)
         self.connected = True
         ret = ClientRet(ret_type=2)
     except:
         ret = ClientRet(ret_type=-2)
     finally:
         return ret
Exemplo n.º 5
0
    def del_torrent(self, idx, remove_data=True):
        if not self.connected:
            return ClientRet(ret_type=-2)

        try:
            if remove_data:
                self.client.delete_permanently(idx)
            else:
                self.client.delete(idx)
            ret = ClientRet(ret_type=5)
        except:
            ret = ClientRet(ret_type=-5)
        finally:
            return ret
Exemplo n.º 6
0
    def get_torrent_status(self, idx):
        if not self.connected:
            return ClientRet(ret_type=-2)

        tlist = self.client.torrents()
        for torrent in tlist:
            if idx == torrent[
                    'hash']:  # No progress info in get_torrent() method, really...
                is_finished = math.isclose(torrent['progress'], 1)
                torrent_status = TorrentStatus(torrent_id=torrent['hash'],
                                               is_finished=is_finished,
                                               name=torrent['name'])

                ret = ClientRet(ret_type=6, ret_value=torrent_status)
                return ret

        return ClientRet(ret_type=-6)
Exemplo n.º 7
0
    def get_torrent_status(self, idx):
        if not self.connected:
            ret = ClientRet(ret_type=-2)
            return ret

        try:
            torrent_obj = self.client.get_torrent(idx)
            is_finished = math.isclose(torrent_obj.progress, 100)
            torrent_status = TorrentStatus(torrent_id=torrent_obj.hashString,
                                           is_finished=is_finished,
                                           name=torrent_obj.name)

            ret = ClientRet(ret_type=6, ret_value=torrent_status)
        except:
            ret = ClientRet(ret_type=-6)
        finally:
            return ret
Exemplo n.º 8
0
    def list_torrents(self):
        if not self.connected:
            return ClientRet(ret_type=-2)

        torrent_list = self.client.torrents()
        session_status = {}
        for torrent in torrent_list:
            is_finished = math.isclose(torrent['progress'], 1)
            torrent_status = TorrentStatus(torrent_id=torrent['hash'],
                                           is_finished=is_finished,
                                           name=torrent['name'])

            session_status[torrent['hash']] = torrent_status

        ret = ClientRet(ret_type=4, ret_value=session_status)

        return ret
Exemplo n.º 9
0
    def list_torrents(self):
        if not self.connected:
            ret = ClientRet(ret_type=-2)
            return ret

        torrent_id_list = self.client.core.get_session_state()
        session_status = {}
        for idx in torrent_id_list:
            torrent_status_raw = self.client.core.get_torrent_status(
                torrent_id=idx, keys=bt_client.client_base.torrent_status_key)
            torrent_status = TorrentStatus(
                torrent_id=idx.decode(),
                is_finished=torrent_status_raw['is_finished'.encode()],
                name=torrent_status_raw['name'.encode()].decode())
            session_status[torrent_status.torrent_id] = torrent_status

        ret = ClientRet(ret_type=4, ret_value=session_status)

        return ret
Exemplo n.º 10
0
    def get_torrent_status(
        self, idx
    ):  # All the value inputted and returned back should be string, not bytearray
        if not self.connected:
            ret = ClientRet(ret_type=-2)
            return ret

        idx = idx.encode()  # Convert to byte array for Deluge
        torrent_status_raw = self.client.core.get_torrent_status(
            torrent_id=idx, keys=bt_client.client_base.torrent_status_key)
        torrent_status = TorrentStatus(
            torrent_id=idx.decode(),
            is_finished=torrent_status_raw[
                'is_finished'.encode()],  # Decode bytearray to string
            name=torrent_status_raw[
                'name'.encode()].decode())  # Decode bytearray to string

        ret = ClientRet(ret_type=6, ret_value=torrent_status)
        return ret
Exemplo n.º 11
0
    def del_torrent(self, idx, remove_data=True):
        if not self.connected:
            ret = ClientRet(ret_type=-2)
            return ret

        idx_byte = str(idx).encode()
        torrent_id_list = self.client.core.get_session_state()
        if idx_byte in torrent_id_list:
            self.client.core.remove_torrent(torrent_id=idx_byte,
                                            remove_data=remove_data)
            tlist = self.client.core.get_session_state()
            if idx not in tlist:
                ret = ClientRet(ret_type=5)
                return ret
            else:
                ret = ClientRet(ret_type=-5)
                return ret
        else:
            ret = ClientRet(ret_type=-5)
            return ret
Exemplo n.º 12
0
    def add_torrent(self, torrent_path, download_path=None):
        if not self.connected:
            ret = ClientRet(ret_type=-2)
            return ret

        abs_torrent_path = str(Path(torrent_path).resolve())
        try:
            if download_path is None:
                torrent_obj = self.client.add_torrent(abs_torrent_path,
                                                      paused=False)
            else:
                download_path = str(Path(download_path).resolve())
                torrent_obj = self.client.add_torrent(
                    abs_torrent_path, download_dir=download_path,
                    paused=False)  # Must be absolute path
            if torrent_obj is not None:
                ret = ClientRet(ret_type=3, ret_value=torrent_obj.hashString)
        except:
            ret = ClientRet(ret_type=-3)
        finally:
            return ret
Exemplo n.º 13
0
    def list_torrents(self):
        if not self.connected:
            ret = ClientRet(ret_type=-2)
            return ret

        try:
            torrent_obj_list = self.client.get_torrents()
            session_status = {}
            for torrent_obj in torrent_obj_list:
                is_finished = math.isclose(torrent_obj.progress, 100)
                torrent_status = TorrentStatus(
                    torrent_id=torrent_obj.hashString,
                    is_finished=is_finished,
                    name=torrent_obj.name)

                session_status[torrent_obj.hashString] = torrent_status

            ret = ClientRet(ret_type=4, ret_value=session_status)
        except:
            ret = ClientRet(ret_type=-4)
        finally:
            return ret
Exemplo n.º 14
0
    def del_torrent(self, idx, remove_data=True):
        if not self.connected:
            ret = ClientRet(ret_type=-2)
            return ret

        try:
            torrent_obj = self.client.get_torrent(idx)
            torrent_exist = True
        except:
            torrent_exist = False

        if torrent_exist:
            try:
                self.client.remove_torrent(idx, delete_data=remove_data)
                ret = ClientRet(ret_type=5)
            except:
                ret = ClientRet(ret_type=-5)
            finally:
                return ret
        else:
            ret = ClientRet(ret_type=-5)
            return ret
Exemplo n.º 15
0
    def add_torrent(self, torrent_path, download_path=None):
        if not self.connected:
            return ClientRet(ret_type=-2)

        abs_torrent_path = str(Path(torrent_path).resolve())
        buf = open(abs_torrent_path, 'rb')

        if download_path is None:
            try:
                api_ret = self.client.download_from_file(buf)
                if 'Ok.' in api_ret:
                    buf.close()
                    info_hash = torf.Torrent.read(abs_torrent_path).infohash

                    ret = ClientRet(ret_type=3, ret_value=info_hash)
                else:
                    ret = ClientRet(ret_type=-3)
            except:
                ret = ClientRet(ret_type=-3)
            finally:
                return ret
        else:
            try:
                abs_download_path = str(Path(download_path).resolve())
                api_ret = self.client.download_from_file(
                    buf, save_path=abs_download_path)
                if 'Ok.' in api_ret:
                    buf.close()
                    info_hash = torf.Torrent.read(abs_torrent_path).infohash

                    ret = ClientRet(ret_type=3, ret_value=info_hash)
                else:
                    ret = ClientRet(ret_type=-3)
            except:
                ret = ClientRet(ret_type=-3)
            finally:
                return ret
Exemplo n.º 16
0
 def disconnect(self):
     self.connected = False
     self.client = None
     ret = ClientRet(ret_type=0)
     return ret
Exemplo n.º 17
0
 def disconnect(self):
     if self.connected:
         self.client.logout()
         self.connected = False
     ret = ClientRet(ret_type=0)
     return ret