0

`I'm building a discord bot in python. Every time a user types a command such as "fantasy 10" the bot will give the user a list of 10 books from that genre.

import discord
import requests
import random
from discord.ext import commands


base_url = "https://www.googleapis.com/books/v1/volumes"




client = commands.Bot(command_prefix="=",intents=discord.Intents.all())

books_list = []



@client.command()
async def recommend(cxt,subject:str, num_books:int):
    
        
        create_book_list(subject)
       
        

      

        randomizer = random.randint(0,len(books_list) - num_books)

        for i in range(num_books):
           
            await cxt.channel.send(books_list[i + randomizer]["volumeInfo"]["title"])
        
   
   

def create_book_list(subject):
        

        start_index = 0

        num_loops = 15

        
        
        for i in range(num_loops):

            params = {
            "q": f"subject:{subject}",  
            "maxResults": 14,  
            "startIndex": start_index,
            "printType": "books",  
        }
            start_index = start_index + 14        
            response = requests.get(base_url,params=params)

            if response.status_code == 200:

                books = response.json()["items"]
        
                for book in books:
                    books_list.append(book);

        



client.run("")

I tried many ways to loop through all the books in a genre as you can see but basically I can't access index past 200. if startingIndex is more than 200 code breaks

`I'm just wondering why I can't access indexes past 200 as there should be more than 200 books in a specified genre.

I'm trying to access the books past index 200 so I can give the users more diversity when recommending books.

Filip Bog
  • 19
  • 2

0 Answers0