def setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the qBittorrent sensors."""

    try:
        client = Client(config[CONF_URL])
        client.login(config[CONF_USERNAME], config[CONF_PASSWORD])
    except LoginRequired:
        _LOGGER.error("Invalid authentication")
        return
    except RequestException as err:
        _LOGGER.error("Connection failed")
        raise PlatformNotReady from err

    name = config.get(CONF_NAME)

    entities = [
        QBittorrentSensor(description, client, name, LoginRequired)
        for description in SENSOR_TYPES
    ]

    add_entities(entities, True)
Beispiel #2
0
async def async_setup_platform(hass,
                               config,
                               async_add_entities,
                               discovery_info=None):
    """Set up the qBittorrent sensors."""
    from qbittorrent.client import Client, LoginRequired

    try:
        client = Client(config[CONF_URL])
        client.login(config[CONF_USERNAME], config[CONF_PASSWORD])
    except LoginRequired:
        _LOGGER.error("Invalid authentication")
        return
    except RequestException:
        _LOGGER.error("Connection failed")
        raise PlatformNotReady

    name = config.get(CONF_NAME)

    dev = []
    for sensor_type in SENSOR_TYPES:
        sensor = QBittorrentSensor(sensor_type, client, name, LoginRequired)
        dev.append(sensor)

    async_add_entities(dev, True)
Beispiel #3
0
    def connect(self):
        if self.qb is not None:
            self.qb.logout()

        url = clean_host(self.conf('host'), protocol=True, ssl=False)

        if self.conf('username') and self.conf('password'):
            self.qb = Client(url)
            self.qb.login(username=self.conf('username'),
                          password=self.conf('password'))
        else:
            self.qb = Client(url)

        return self.qb._is_authenticated
Beispiel #4
0
    def __init__(self, hass, config):

        try:
            self.client = Client(config[CONF_URL])
            self.client.login(config[CONF_USERNAME], config[CONF_PASSWORD])
        except LoginRequired:
            _LOGGER.error("Invalid authentication")
            return
        except RequestException:
            _LOGGER.error("Connection failed")
            raise PlatformNotReady

        self.sensors = []
        self.name = config.get(CONF_NAME)
        for sensor_type in SENSOR_TYPES:
            sensor = QBittorrentSensor(sensor_type, self.client, self.name,
                                       LoginRequired)
            self.sensors.append(sensor)
Beispiel #5
0
async def async_setup_platform(
        hass, config, async_add_entities, discovery_info=None):
    """Set up the qBittorrent sensors."""
    from qbittorrent.client import Client, LoginRequired

    try:
        client = Client(config[CONF_URL])
        client.login(config[CONF_USERNAME], config[CONF_PASSWORD])
    except LoginRequired:
        _LOGGER.error("Invalid authentication")
        return
    except RequestException:
        _LOGGER.error("Connection failed")
        raise PlatformNotReady

    name = config.get(CONF_NAME)

    dev = []
    for sensor_type in SENSOR_TYPES:
        sensor = QBittorrentSensor(sensor_type, client, name, LoginRequired)
        dev.append(sensor)

    async_add_entities(dev, True)
Beispiel #6
0
  for folder in folders:
    li = xbmcgui.ListItem(folder)

    url = sys.argv[0] + "?mode=%s" % folder

    xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=url, listitem=li, isFolder=True)

  xbmcplugin.endOfDirectory(handle=int(sys.argv[1]), cacheToDisc=False)
  


username = __settings__.getSetting('username')
password = __settings__.getSetting('password')
url = __settings__.getSetting('url')

qb = Client(url=url)
qb.login(username, password)

for arg in sys.argv:
  xbmc.log('arg: %s' % arg)

params = get_params()

for param in params:
  xbmc.log('param: %s' % param)

mode = 0

try:
  mode = params['mode']
except:
Beispiel #7
0
        url = sys.argv[0] + "?mode=%s" % folder

        xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),
                                    url=url,
                                    listitem=li,
                                    isFolder=True)

    xbmcplugin.endOfDirectory(handle=int(sys.argv[1]), cacheToDisc=False)


username = __settings__.getSetting('username')
password = __settings__.getSetting('password')
url = __settings__.getSetting('url')

qb = Client(url=url)
qb.login(username, password)

for arg in sys.argv:
    xbmc.log('arg: %s' % arg)

params = get_params()

for param in params:
    xbmc.log('param: %s' % param)

mode = 0

try:
    mode = params['mode']
except:
Beispiel #8
0
class qBittorrent(DownloaderBase):

    protocol = ['torrent', 'torrent_magnet']
    qb = None

    def __init__(self):
        super(qBittorrent, self).__init__()

    def connect(self):
        if self.qb is not None:
            self.qb.logout()

        url = clean_host(self.conf('host'), protocol=True, ssl=False)

        if self.conf('username') and self.conf('password'):
            self.qb = Client(url)
            self.qb.login(username=self.conf('username'),
                          password=self.conf('password'))
        else:
            self.qb = Client(url)

        return self.qb._is_authenticated

    def test(self):
        """ Check if connection works
        :return: bool
        """
        return self.connect()

    def download(self, data=None, media=None, filedata=None):
        """ Send a torrent/nzb file to the downloader

        :param data: dict returned from provider
            Contains the release information
        :param media: media dict with information
            Used for creating the filename when possible
        :param filedata: downloaded torrent/nzb filedata
            The file gets downloaded in the searcher and send to this function
            This is done to have failed checking before using the downloader, so the downloader
            doesn't need to worry about that
        :return: boolean
            One faile returns false, but the downloaded should log his own errors
        """

        if not media: media = {}
        if not data: data = {}

        log.debug('Sending "%s" to qBittorrent.', (data.get('name')))

        if not self.connect():
            return False

        if not filedata and data.get('protocol') == 'torrent':
            log.error('Failed sending torrent, no data')
            return False

        if data.get('protocol') == 'torrent_magnet':
            # Send request to qBittorrent directly as a magnet
            try:
                self.qb.download_from_link(data.get('url'),
                                           label=self.conf('label'))
                torrent_hash = re.findall('urn:btih:([\w]{32,40})',
                                          data.get('url'))[0].upper()
                log.info('Torrent [magnet] sent to QBittorrent successfully.')
                return self.download_return_id(torrent_hash)

            except Exception as e:
                log.error('Failed to send torrent to qBittorrent: %s', e)
                return False

        if data.get('protocol') == 'torrent':
            info = bdecode(filedata)["info"]
            torrent_hash = sha1(bencode(info)).hexdigest()

            # Convert base 32 to hex
            if len(torrent_hash) == 32:
                torrent_hash = b16encode(b32decode(torrent_hash))

            # Send request to qBittorrent
            try:
                self.qb.download_from_file(filedata, label=self.conf('label'))
                log.info('Torrent [file] sent to QBittorrent successfully.')
                return self.download_return_id(torrent_hash)
            except Exception as e:
                log.error('Failed to send torrent to qBittorrent: %s', e)
                return False

    def get_torrent_status(self, torrent):

        if torrent['state'] in ('uploading', 'queuedUP', 'stalledUP'):
            return 'seeding'

        if torrent['progress'] == 1:
            return 'completed'

        return 'busy'

    def get_all_download_status(self, ids):
        """ Get status of all active downloads

        :param ids: list of (mixed) downloader ids
            Used to match the releases for this downloader as there could be
            other downloaders active that it should ignore
        :return: list of releases
        """

        log.debug('Checking qBittorrent download status.')

        if not self.connect():
            return []

        try:
            torrents = self.qb.torrents(status='all', label=self.conf('label'))

            release_downloads = ReleaseDownloadList(self)

            for torrent in torrents:
                if torrent['hash'] in ids:
                    torrent_filelist = self.qb.get_torrent_files(
                        torrent['hash'])

                    torrent_files = []
                    torrent_dir = os.path.join(torrent['save_path'],
                                               torrent['name'])

                    if os.path.isdir(torrent_dir):
                        torrent['save_path'] = torrent_dir

                    if len(torrent_filelist) > 1 and os.path.isdir(
                            torrent_dir
                    ):  # multi file torrent, path.isdir check makes sure we're not in the root download folder
                        for root, _, files in os.walk(torrent['save_path']):
                            for f in files:
                                torrent_files.append(sp(os.path.join(root, f)))

                    else:  # multi or single file placed directly in torrent.save_path
                        for f in torrent_filelist:
                            file_path = os.path.join(torrent['save_path'],
                                                     f['name'])
                            if os.path.isfile(file_path):
                                torrent_files.append(sp(file_path))

                    release_downloads.append({
                        'id':
                        torrent['hash'],
                        'name':
                        torrent['name'],
                        'status':
                        self.get_torrent_status(torrent),
                        'seed_ratio':
                        torrent['ratio'],
                        'original_status':
                        torrent['state'],
                        'timeleft':
                        str(timedelta(seconds=torrent['eta'])),
                        'folder':
                        sp(torrent['save_path']),
                        'files':
                        torrent_files
                    })

            return release_downloads

        except Exception as e:
            log.error('Failed to get status from qBittorrent: %s', e)
            return []

    def pause(self, release_download, pause=True):
        if not self.connect():
            return False

        torrent = self.qb.get_torrent(release_download['id'])
        if torrent is None:
            return False

        if pause:
            return self.qb.pause(release_download['id'])
        return self.qb.resume(release_download['id'])

    def remove_failed(self, release_download):
        log.info('%s failed downloading, deleting...',
                 release_download['name'])
        return self.process_complete(release_download, delete_files=True)

    def process_complete(self, release_download, delete_files):
        log.debug(
            'Requesting qBittorrent to remove the torrent %s%s.',
            (release_download['name'],
             ' and cleanup the downloaded files' if delete_files else ''))

        if not self.connect():
            return False

        torrent = self.qb.get_torrent(release_download['id'])

        if torrent is None:
            return False

        if delete_files:
            self.qb.delete_permanently(
                release_download['id'])  # deletes torrent with data
        else:
            self.qb.delete(release_download['id']
                           )  # just removes the torrent, doesn't delete data

        return True