Пример #1
0
async def _fetch_chunks_pixelzone(chunks: Iterable[ChunkPz]):
    socket_url = "{0}://pixelzone.io/socket.io/?EIO=3&transport={1}"
    sid = None
    chunks = [x for x in chunks if x.is_in_bounds()]
    if len(chunks) == 0:
        return
    async with aiohttp.ClientSession() as session:
        attempts = 0
        while attempts < 3:
            try:
                async with session.get(
                        socket_url.format("http", "polling"),
                        headers={'User-Agent':
                                 'Python/3.6 aiohttp/3.2.0'}) as r:
                    sid = json.loads(str((await r.read())[4:-4],
                                         "utf-8"))['sid']
                    break
            except aiohttp.client_exceptions.ClientOSError:
                attempts += 1
    if not sid:
        raise errors.HttpCanvasError('pixelzone')
    async with websockets.connect(
            socket_url.format("ws", "websocket&sid=") + sid,
            extra_headers={'User-Agent': 'Python/3.6 aiohttp/3.2.0'}) as ws:
        try:
            await ws.send("2probe")
            await ws.recv()
            await ws.send("5")
            await ws.recv()
            await ws.send("42['useAPI', '{}'".format(cfg.pz_api_key))
            for ch in chunks:
                data = {}
                await ws.send(ch.url)
                async for msg in ws:
                    d = json.loads(msg[msg.find('['):])
                    if type(d) == int:
                        continue
                    if d[0] == "c":
                        data = d[1]
                        break
                ch = next((x for x in chunks
                           if x.x == data['cx'] and x.y == data['cy']))
                ch.load(data['data'])
        except websockets.ConnectionClosed as e:
            raise errors.HttpCanvasError('pixelzone')
Пример #2
0
async def fetch_online_pixelzone():
    socket_url = "{0}://pixelzone.io/socket.io/?EIO=3&transport={1}"
    sid = None
    async with aiohttp.ClientSession() as session:
        attempts = 0
        while attempts < 3:
            try:
                async with session.get(
                        socket_url.format("http", "polling"),
                        headers={'User-Agent':
                                 'Python/3.6 aiohttp/3.2.0'}) as r:
                    sid = json.loads(str((await r.read())[4:-4],
                                         "utf-8"))['sid']
                    break
            except aiohttp.client_exceptions.ClientOSError:
                attempts += 1
    if not sid:
        raise errors.HttpCanvasError('pixelzone')
    async with websockets.connect(
            socket_url.format("ws", "websocket&sid=") + sid,
            extra_headers={'User-Agent': 'Python/3.6 aiohttp/3.2.0'}) as ws:
        await ws.send("2probe")
        await ws.recv()
        await ws.send("5")
        await ws.recv()
        await ws.send("42['useAPI', '{}'".format(cfg.pz_api_key))
        try:
            async for msg in ws:
                d = json.loads(msg[msg.find('['):])
                if type(d) == int:
                    continue
                if d[0] == "g":
                    data = d[1]
                    break
        except websockets.ConnectionClosed as e:
            raise errors.HttpCanvasError('pixelzone')
    return data
Пример #3
0
async def _fetch_chunks_pixelcanvas(bigchunks: Iterable[BigChunk]):
    async with aiohttp.ClientSession() as session:
        for bc in bigchunks:
            await asyncio.sleep(0)
            if not bc.is_in_bounds():
                continue
            data = None
            attempts = 0
            while attempts < 3:
                try:
                    async with session.get(bc.url) as resp:
                        data = await resp.read()
                        if len(data) == 460800:
                            break
                except aiohttp.ClientPayloadError:
                    pass
                data = None
                attempts += 1
            if not data:
                raise errors.HttpCanvasError('pixelcanvas')
            bc.load(data)