Exemple #1
0
def test_download_on_server_side_error(session, caplog):
    responses.add(responses.GET, URI, body=BODY, status=500)

    result = http.download(session, URI)

    assert result is None
    assert 'Problem downloading' in caplog.text
Exemple #2
0
def test_download_on_server_side_error(session, caplog):
    responses.add(responses.GET, URI, body=BODY, status=500)

    result = http.download(session, URI)

    assert result is None
    assert 'Problem downloading' in caplog.text()
Exemple #3
0
def test_download_times_out_if_connection_times_out(session_mock, caplog):
    session_mock.get.side_effect = requests.exceptions.Timeout

    result = http.download(session_mock, URI, timeout=1.0)

    session_mock.get.assert_called_once_with(URI, timeout=1.0, stream=True)
    assert result is None
    assert ('Download of %r failed due to connection timeout after 1.000s' %
            URI in caplog.text())
Exemple #4
0
def _unwrap_stream(uri, timeout, scanner, requests_session):
    """
    Get a stream URI from a playlist URI, ``uri``.

    Unwraps nested playlists until something that's not a playlist is found or
    the ``timeout`` is reached.
    """

    original_uri = uri
    seen_uris = set()
    deadline = time.time() + timeout

    while time.time() < deadline:
        if uri in seen_uris:
            logger.info("Unwrapping stream from URI (%s) failed: " "playlist referenced itself", uri)
            return None, None
        else:
            seen_uris.add(uri)

        logger.debug("Unwrapping stream from URI: %s", uri)

        try:
            scan_timeout = deadline - time.time()
            if scan_timeout < 0:
                logger.info("Unwrapping stream from URI (%s) failed: " "timed out in %sms", uri, timeout)
                return None, None
            scan_result = scanner.scan(uri, timeout=scan_timeout)
        except exceptions.ScannerError as exc:
            logger.debug("GStreamer failed scanning URI (%s): %s", uri, exc)
            scan_result = None

        if scan_result is not None:
            if scan_result.playable or (
                not scan_result.mime.startswith("text/") and not scan_result.mime.startswith("application/")
            ):
                logger.debug("Unwrapped potential %s stream: %s", scan_result.mime, uri)
                return uri, scan_result

        download_timeout = deadline - time.time()
        if download_timeout < 0:
            logger.info("Unwrapping stream from URI (%s) failed: timed out in %sms", uri, timeout)
            return None, None
        content = http.download(requests_session, uri, timeout=download_timeout / 1000)

        if content is None:
            logger.info("Unwrapping stream from URI (%s) failed: " "error downloading URI %s", original_uri, uri)
            return None, None

        uris = playlists.parse(content)
        if not uris:
            logger.debug("Failed parsing URI (%s) as playlist; found potential stream.", uri)
            return uri, None

        # TODO Test streams and return first that seems to be playable
        logger.debug("Parsed playlist (%s) and found new URI: %s", uri, uris[0])
        uri = uris[0]
Exemple #5
0
def test_download_times_out_if_connection_times_out(session_mock, caplog):
    session_mock.get.side_effect = requests.exceptions.Timeout

    result = http.download(session_mock, URI, timeout=1.0)

    session_mock.get.assert_called_once_with(URI, timeout=1.0, stream=True)
    assert result is None
    assert (
        'Download of %r failed due to connection timeout after 1.000s' % URI
        in caplog.text)
Exemple #6
0
def test_download_times_out_if_download_is_slow(session, caplog):
    responses.add(responses.GET, URI, body=BODY, content_type='text/plain')

    with mock.patch.object(http, 'time') as time_mock:
        time_mock.time.side_effect = [0, TIMEOUT + 1]

        result = http.download(session, URI)

    assert result is None
    assert ('Download of %r failed due to download taking more than 1.000s' %
            URI in caplog.text())
Exemple #7
0
def test_download_times_out_if_download_is_slow(session, caplog):
    responses.add(responses.GET, URI, body=BODY, content_type='text/plain')

    with mock.patch.object(http, 'time') as time_mock:
        time_mock.time.side_effect = [0, TIMEOUT + 1]

        result = http.download(session, URI)

    assert result is None
    assert (
        'Download of %r failed due to download taking more than 1.000s' % URI
        in caplog.text)
Exemple #8
0
    def _parse_playlist(self, uri):
        """Fetch and parse playlist from uri."""
        content = http.download(self.backend.session,
                                uri,
                                timeout=self.backend.timeout / 1000)

        if content is None:
            logger.warning(
                "Error downloading URI %s while unwrapping playlist",
                uri,
            )
            return []

        uris = playlists.parse(content)
        if not uris:
            logger.warning("Failed playlist from URI %s.", uri)
            return []

        return uris
Exemple #9
0
def _unwrap_stream(uri, timeout, scanner, requests_session):
    """
    Get a stream URI from a playlist URI, ``uri``.

    Unwraps nested playlists until something that's not a playlist is found or
    the ``timeout`` is reached.
    """

    original_uri = uri
    seen_uris = set()
    deadline = time.time() + timeout

    while time.time() < deadline:
        if uri in seen_uris:
            logger.info(
                'Unwrapping stream from URI (%s) failed: '
                'playlist referenced itself', uri)
            return None
        else:
            seen_uris.add(uri)

        logger.debug('Unwrapping stream from URI: %s', uri)

        try:
            scan_timeout = deadline - time.time()
            if scan_timeout < 0:
                logger.info(
                    'Unwrapping stream from URI (%s) failed: '
                    'timed out in %sms', uri, timeout)
                return None
            scan_result = scanner.scan(uri, timeout=scan_timeout)
        except exceptions.ScannerError as exc:
            logger.debug('GStreamer failed scanning URI (%s): %s', uri, exc)
            scan_result = None

        if scan_result is not None:
            if scan_result.playable or (
                    not scan_result.mime.startswith('text/')
                    and not scan_result.mime.startswith('application/')):
                logger.debug('Unwrapped potential %s stream: %s',
                             scan_result.mime, uri)
                return uri

        download_timeout = deadline - time.time()
        if download_timeout < 0:
            logger.info(
                'Unwrapping stream from URI (%s) failed: timed out in %sms',
                uri, timeout)
            return None
        content = http.download(requests_session,
                                uri,
                                timeout=download_timeout)

        if content is None:
            logger.info(
                'Unwrapping stream from URI (%s) failed: '
                'error downloading URI %s', original_uri, uri)
            return None

        uris = playlists.parse(content)
        if not uris:
            logger.debug(
                'Failed parsing URI (%s) as playlist; found potential stream.',
                uri)
            return uri

        # TODO Test streams and return first that seems to be playable
        logger.debug('Parsed playlist (%s) and found new URI: %s', uri,
                     uris[0])
        uri = uris[0]
Exemple #10
0
def _unwrap_stream(uri, timeout, scanner, requests_session):
    """
    Get a stream URI from a playlist URI, ``uri``.

    Unwraps nested playlists until something that's not a playlist is found or
    the ``timeout`` is reached.
    """

    original_uri = uri
    seen_uris = set()
    deadline = time.time() + timeout

    while time.time() < deadline:
        if uri in seen_uris:
            logger.info(
                "Unwrapping stream from URI (%s) failed: "
                "playlist referenced itself",
                uri,
            )
            return None, None
        else:
            seen_uris.add(uri)

        logger.debug("Unwrapping stream from URI: %s", uri)

        try:
            scan_timeout = deadline - time.time()
            if scan_timeout < 0:
                logger.info(
                    "Unwrapping stream from URI (%s) failed: "
                    "timed out in %sms",
                    uri,
                    timeout,
                )
                return None, None
            scan_result = scanner.scan(uri, timeout=scan_timeout)
        except exceptions.ScannerError as exc:
            logger.debug("GStreamer failed scanning URI (%s): %s", uri, exc)
            scan_result = None

        if scan_result is not None:
            has_interesting_mime = (
                scan_result.mime is not None
                and not scan_result.mime.startswith("text/")
                and not scan_result.mime.startswith("application/"))
            if scan_result.playable or has_interesting_mime:
                logger.debug("Unwrapped potential %s stream: %s",
                             scan_result.mime, uri)
                return uri, scan_result

        download_timeout = deadline - time.time()
        if download_timeout < 0:
            logger.info(
                "Unwrapping stream from URI (%s) failed: timed out in %sms",
                uri,
                timeout,
            )
            return None, None
        content = http.download(requests_session,
                                uri,
                                timeout=download_timeout / 1000)

        if content is None:
            logger.info(
                "Unwrapping stream from URI (%s) failed: "
                "error downloading URI %s",
                original_uri,
                uri,
            )
            return None, None

        uris = playlists.parse(content)
        if not uris:
            logger.debug(
                "Failed parsing URI (%s) as playlist; found potential stream.",
                uri,
            )
            return uri, None

        # TODO Test streams and return first that seems to be playable
        new_uri = uris[0]
        logger.debug("Parsed playlist (%s) and found new URI: %s", uri,
                     new_uri)
        uri = urllib.parse.urljoin(uri, new_uri)
Exemple #11
0
def _unwrap_stream(uri, timeout, scanner, requests_session):
    """
    Get a stream URI from a playlist URI, ``uri``.

    Unwraps nested playlists until something that's not a playlist is found or
    the ``timeout`` is reached.
    """

    original_uri = uri
    seen_uris = set()
    deadline = time.time() + timeout

    while time.time() < deadline:
        if uri in seen_uris:
            logger.info(
                f"Unwrapping stream from URI ({uri!r}) failed: "
                "playlist referenced itself", )
            return None, None
        else:
            seen_uris.add(uri)

        logger.debug(f"Unwrapping stream from URI: {uri!r}")

        try:
            scan_timeout = deadline - time.time()
            if scan_timeout < 0:
                logger.info(
                    f"Unwrapping stream from URI ({uri!r}) failed: "
                    f"timed out in {timeout}ms", )
                return None, None
            scan_result = scanner.scan(uri, timeout=scan_timeout)
        except exceptions.ScannerError as exc:
            logger.debug(f"GStreamer failed scanning URI ({uri!r}): {exc}")
            scan_result = None

        if scan_result is not None:
            if scan_result.playable or (
                    not scan_result.mime.startswith("text/")
                    and not scan_result.mime.startswith("application/")):
                logger.debug(
                    f"Unwrapped potential {scan_result.mime} stream: {uri!r}")
                return uri, scan_result

        download_timeout = deadline - time.time()
        if download_timeout < 0:
            logger.info(
                f"Unwrapping stream from URI ({uri!r}) failed: timed out in {timeout}ms"
            )
            return None, None
        content = http.download(requests_session,
                                uri,
                                timeout=download_timeout / 1000)

        if content is None:
            logger.info(
                f"Unwrapping stream from URI ({original_uri!r}) failed: "
                f"error downloading URI {uri!r}", )
            return None, None

        uris = playlists.parse(content)
        if not uris:
            logger.debug(
                f"Failed parsing URI ({uri!r}) as playlist; "
                "found potential stream.", )
            return uri, None

        # TODO Test streams and return first that seems to be playable
        logger.debug(
            f"Parsed playlist ({uri!r}) and found new URI: {uris[0]!r}")
        uri = uris[0]