def addDownload(self, download):
        if not self._client:
            self._client = TransmissionClient(self.c.host, self.c.username,
                                              self.c.password)

        # if headers, have to download then transmit content.
        if 'headers' in download.extra_data:
            headers = download.extra_data[
                'headers'] if 'headers' in download.extra_data else {}
            response = requests.get(download.url, headers=headers)
            if response.status_code == 200:
                result = self._client.addNewTorrentData(response.content, True)
            else:
                log.info(
                    "Download torrent file for transmission failed with url %s"
                    % download.url)
                return False
        else:
            result = self._client.addNewTorrentLink(download.url, True)

        if result['success']:
            download.extra_data['hash'] = result['hash']
            log.info("Download sent to rutorrent as %s." % result['hash'])
            return True
        else:
            return False
 def _testConnection(self, host, username, password):
     try:
         client = TransmissionClient(host, username, password)
         log.info("Connected to transmission.")
     except Exception as e:
         return (False, {}, 'Connection failed (%s), check settings.' % e)
     return (True, {}, 'Connection Established!')
    def getDownloadPercentage(self, element):
        if not self._client:
            self._client = TransmissionClient(self.c.host, self.c.username,
                                              self.c.password)
        if not self._torrents:
            self._torrents = self._client.getTorrents()['torrents']
        for item in self._torrents:
            download = self._findDownload(item['hash'])
            if download == None:
                continue

            if download.element.id != element.id:
                continue

            percentage = item['percentDone'] * 100
            return percentage
        return 0
    def getElementStaus(self, element):
        if not self._client:
            self._client = TransmissionClient(self.c.host, self.c.username,
                                              self.c.password)

        download = Download()
        download.status = common.UNKNOWN
        if not self._torrents:
            self._torrents = self._client.getTorrents()['torrents']
        for item in self._torrents:
            download = self._findDownload(item['hash'])
            if download == None:
                continue

            if download.element.id != element.id:
                continue

            if item['leftUntilDone'] == 0:
                return (common.DOWNLOADED, download, item['storage'])
            elif item['leftUntilDone'] > 0:
                return (common.DOWNLOADING, download, '')
            else:
                return (common.FAILED, download, '')
        return (common.UNKNOWN, download, '')