Beispiel #1
0
    def get(self, tile):
        future = Future()
        url = QUrl(tile.url)
        request = QNetworkRequest(url)
        # Modified by Jean 2020/05/21 to support tianditu.gov.cn
        # request.setRawHeader(b"User-Agent", b"OWMap/1.0")
        request.setRawHeader(b"User-Agent", b"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0")
        
        request.setAttribute(
            QNetworkRequest.CacheLoadControlAttribute,
            QNetworkRequest.PreferCache
        )
        request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True)
        request.setMaximumRedirectsAllowed(5)

        # Future yielding a QNetworkReply when finished.
        reply = self._netmanager.get(request)
        future._reply = reply
        future._tile = tile

        @future.add_done_callback
        def abort_on_cancel(f):
            # abort the network request on future.cancel()
            if f.cancelled() and f._reply is not None:
                f._reply.abort()

        def on_reply_ready(reply, future):
            # type: (QNetworkReply, Future) -> None

            # schedule deferred delete to ensure the reply is closed
            # otherwise we will leak file/socket descriptors
            reply.deleteLater()
            future._reply = None

            with closing(reply):
                if not future.set_running_or_notify_cancel():
                    return

                if reply.error() != QNetworkReply.NoError:
                    # XXX Maybe convert the error into standard http and
                    # urllib exceptions.
                    future.set_exception(Exception(reply.errorString()))
                    return

                try:
                    image = Image.open(BytesIO(reply.readAll()))
                except Exception as e:
                    future.set_exception(e)
                else:
                    tile.disc_cache = reply.attribute(
                        QNetworkRequest.SourceIsFromCacheAttribute)
                    future.set_result(image)

        reply.finished.connect(partial(on_reply_ready, reply, future))
        return future
Beispiel #2
0
    def get(self, tile):
        future = Future()
        url = QUrl(tile.url)
        request = QNetworkRequest(url)
        # Modified by Jean 2020/05/21 to support tianditu.gov.cn
        # request.setRawHeader(b"User-Agent", b"OWMap/1.0")
        # request.setRawHeader(b"User-Agent", b"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0")
        request.setRawHeader(
            b'User-Agent',
            b'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
        )
        # Updated by Jean @ 2022/1/6
        request.setRawHeader(
            b'Accept',
            b'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8'
        )
        request.setRawHeader(b'Accept-Encoding', b'gzip, deflate, br')
        request.setRawHeader(b'Accept-Language', b'zh-CN,zh;q=0.9')
        request.setRawHeader(
            b'sec-ch-ua',
            b'" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"'
        )
        request.setRawHeader(b'sec-ch-ua-mobile', b'?0')
        request.setRawHeader(b'Upgrade-Insecure-Requests', b'1')

        request.setAttribute(QNetworkRequest.CacheLoadControlAttribute,
                             QNetworkRequest.PreferCache)
        request.setAttribute(QNetworkRequest.FollowRedirectsAttribute, True)
        request.setMaximumRedirectsAllowed(5)

        # Future yielding a QNetworkReply when finished.
        reply = self._netmanager.get(request)
        future._reply = reply
        future._tile = tile

        @future.add_done_callback
        def abort_on_cancel(f):
            # abort the network request on future.cancel()
            if f.cancelled() and f._reply is not None:
                f._reply.abort()

        def on_reply_ready(reply, future):
            # type: (QNetworkReply, Future) -> None

            # schedule deferred delete to ensure the reply is closed
            # otherwise we will leak file/socket descriptors
            reply.deleteLater()
            future._reply = None

            with closing(reply):
                if not future.set_running_or_notify_cancel():
                    return

                if reply.error() != QNetworkReply.NoError:
                    # XXX Maybe convert the error into standard http and
                    # urllib exceptions.
                    future.set_exception(Exception(reply.errorString()))
                    return

                try:
                    image = Image.open(BytesIO(reply.readAll()))
                except Exception as e:
                    future.set_exception(e)
                else:
                    tile.disc_cache = reply.attribute(
                        QNetworkRequest.SourceIsFromCacheAttribute)
                    future.set_result(image)

        reply.finished.connect(partial(on_reply_ready, reply, future))
        return future