DeaD_EyE icon

wikimedia.org/v2/stream/recentchange niquests with asyncio

DeaD_EyE | PRO | 06/22/25 11:22:33 AM UTC (Edited) | 0 ⭐ | 1963 👁️ | Never ⏰ | []
Python |

1 KB

|

None

|

0 👍

/

0 👎

import asyncio
import json
 
import niquests
 
 
async def get() -> dict:
    async with niquests.AsyncSession() as session:
        response = await session.get(
            "https://stream.wikimedia.org/v2/stream/recentchange", stream=True
        )
        async for chunk in await response.iter_content():
            try:
                data = chunk.decode()
            except UnicodeDecodeError:
                continue
 
            if data.startswith("data:"):
                try:
                    # it may fail here
                    result = json.loads(data.removeprefix("data:"))
                    # if it fails, it does not return
                    # instead iterate
                    return result
                except json.JSONDecodeError:
                    # error
                    pass
 
 
async def main():
    try:
        return await asyncio.wait_for(get(), 60)
    except asyncio.TimeoutError:
        print("Got a timeout")
 
 
asyncio.run(main())

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎