Search code examples
pythonpython-3.xpaginationdiscord.pyaiohttp

How can I read all pages of an API response using aiohttp with discord.py?


I'm using the discord.py rewrite and aiohttp. There is very little documentation for this API, and from what I've seen, I haven't seen a "next_page" link anywhere in the response.

How do I make it so that all pages of the json response are considered when the query is executed instead of just the default first page?

Here's my current relevant code:

async def command(ctx, *, name):
    if not name:
        return await ctx.channel.send('Uhhhh. How am I supposed to show you info if you don\'t enter a name?')
    async with ctx.channel.typing():
        async with aiohttp.ClientSession() as cs:
            async with cs.get("https://website.items.json") as r:
                data = await r.json()
                listings = data["items"]
                for k in listings:
                    if name.lower() == k["name"].lower():
                        await ctx.channel.send("message with results and player info as ascertained in the above code")```

Solution

  • You simply take the max number of pages and iterate it. The final url would be like this.

    https://theshownation.com/mlb20/apis/items.json?page=2
    

    Here is how you set up the loop

    for page_number in range(1,total_pages):
        url = f'https://theshownation.com/mlb20/apis/items.json?page={page_number}'
    

    EDIT:

    The full implementation is like this.

    @bot.command()
    async def card(ctx, *, player_name):
        if not player_name:
            return await ctx.channel.send('Uhhhh. How am I supposed to show you player info if you don\'t enter a player name?')
    
        async with ctx.channel.typing():
            async with aiohttp.ClientSession() as cs:
                for page_number in range(1, 20):
                    async with cs.get(f"https://theshownation.com/mlb20/apis/items.json?page={page_number}") as r:
                        data = await r.json()
                        print(data["page"])
                        listings = data["items"]
                        for k in listings:
                            if player_name.lower() == k["name"].lower():
                                return await ctx.channel.send("message with results and player info as ascertained in the above code")
                                
    
        await ctx.send('Sorry could not find him')