def _get_auth(self):
        if self.session.cookies and self.auth:
            return self.auth

        params = {
            'api': 'SYNO.API.Auth',
            'version': 2,
            'method': 'login',
            'account': self.username,
            'passwd': self.password,
            'session': 'DownloadStation',
            'format': 'cookie',
        }

        try:
            self.response = self.session.get(self.urls['login'],
                                             params=params,
                                             verify=False)
            self.response.raise_for_status()
        except RequestException as error:
            handle_requests_exception(error)
            self.session.cookies.clear()
            self.auth = False
            return self.auth
        else:
            return self._check_response()
Example #2
0
    def _get_auth(self):
        if self.session.cookies and self.auth:
            return self.auth

        params = {
            'api': 'SYNO.API.Auth',
            'version': 2,
            'method': 'login',
            'account': self.username,
            'passwd': self.password,
            'session': 'DownloadStation',
            'format': 'cookie',
        }

        try:
            self.response = self.session.get(self.urls['login'], params=params, verify=False)
            self.response.raise_for_status()
        except RequestException as error:
            handle_requests_exception(error)
            self.session.cookies.clear()
            self.auth = False
            return self.auth
        else:
            return self._check_response()
    def _check_destination(self):
        """Validate and set torrent destination."""
        torrent_path = app.TORRENT_PATH

        if not (self.auth or self._get_auth()):
            return False

        if self.checked_destination and self.destination == torrent_path:
            return True

        params = {
            'api': 'SYNO.DSM.Info',
            'version': 2,
            'method': 'getinfo',
            'session': 'DownloadStation',
        }

        try:
            self.response = self.session.get(self.urls['dsminfo'], params=params, verify=False, timeout=120)
            self.response.raise_for_status()
        except RequestException as error:
            handle_requests_exception(error)
            self.session.cookies.clear()
            self.auth = False
            return False

        destination = ''
        if self._check_response():
            jdata = self.response.json()
            version_string = jdata.get('data', {}).get('version_string')
            if not version_string:
                log.warning('Could not get the version string from DSM:'
                            ' {response}', {'response': jdata})
                return False

            if version_string.startswith('DSM 6'):
                #  This is DSM6, lets make sure the location is relative
                if torrent_path and os.path.isabs(torrent_path):
                    torrent_path = re.sub(r'^/volume\d/', '', torrent_path).lstrip('/')
                else:
                    #  Since they didn't specify the location in the settings,
                    #  lets make sure the default is relative,
                    #  or forcefully set the location setting
                    params.update({
                        'method': 'getconfig',
                        'version': 2,
                    })

                    try:
                        self.response = self.session.get(self.urls['info'], params=params, verify=False, timeout=120)
                        self.response.raise_for_status()
                    except RequestException as error:
                        handle_requests_exception(error)
                        self.session.cookies.clear()
                        self.auth = False
                        return False

                    if self._check_response():
                        jdata = self.response.json()
                        destination = jdata.get('data', {}).get('default_destination')
                        if destination and os.path.isabs(destination):
                            torrent_path = re.sub(r'^/volume\d/', '', destination).lstrip('/')
                        else:
                            log.info('Default destination could not be'
                                     ' determined for DSM6: {response}',
                                     {'response': jdata})

                            return False

        if destination or torrent_path:
            log.info('Destination is now {path}',
                     {'path': torrent_path or destination})

        self.checked_destination = True
        self.destination = torrent_path
        return True
Example #4
0
    def _check_destination(self):
        """Validate and set torrent destination."""
        torrent_path = app.TORRENT_PATH

        if not (self.auth or self._get_auth()):
            return False

        if self.checked_destination and self.destination == torrent_path:
            return True

        params = {
            'api': 'SYNO.DownloadStation.Info',
            'version': 2,
            'method': 'getinfo',
            'session': 'DownloadStation',
        }

        try:
            self.response = self.session.get(self.urls['info'], params=params, verify=False, timeout=120)
            self.response.raise_for_status()
        except RequestException as error:
            handle_requests_exception(error)
            self.session.cookies.clear()
            self.auth = False
            return False

        destination = ''
        if self._check_response():
            jdata = self.response.json()
            version_string = jdata.get('data', {}).get('version_string')
            if not version_string:
                log.warning('Could not get the version string from DSM:'
                            ' {response}', {'response': jdata})
                return False

            if version_string.startswith('DSM 6'):
                #  This is DSM6, lets make sure the location is relative
                if torrent_path and os.path.isabs(torrent_path):
                    torrent_path = re.sub(r'^/volume\d/', '', torrent_path).lstrip('/')
                else:
                    #  Since they didn't specify the location in the settings,
                    #  lets make sure the default is relative,
                    #  or forcefully set the location setting
                    params.update({
                        'method': 'getconfig',
                        'version': 2,
                    })

                    try:
                        self.response = self.session.get(self.urls['info'], params=params, verify=False, timeout=120)
                        self.response.raise_for_status()
                    except RequestException as error:
                        handle_requests_exception(error)
                        self.session.cookies.clear()
                        self.auth = False
                        return False

                    if self._check_response():
                        jdata = self.response.json()
                        destination = jdata.get('data', {}).get('default_destination')
                        if destination and os.path.isabs(destination):
                            torrent_path = re.sub(r'^/volume\d/', '', destination).lstrip('/')
                        else:
                            log.info('Default destination could not be'
                                     ' determined for DSM6: {response}',
                                     {'response': jdata})

                            return False

        if destination or torrent_path:
            log.info('Destination is now {path}',
                     {'path': torrent_path or destination})

        self.checked_destination = True
        self.destination = torrent_path
        return True