0

I am writing a bot using gspread and IMDbPy. The script right now takes input(a movie title), it then grabs the movie ID, finds the movie's rating on IMDB.com, then posts the rating onto a spreadsheet into a specific cell.

There is a function named "update_cell" that updates the the specific cell based off the given row and column parameters. Once the bot is complete, I don't want to have to keep going into the code to update the row cell parameter. I want it to update by 1 each time the bot executes.

Is there a way to do this? I'll post the code below:

ia = imdb.IMDb()


def take_input():
    fd = open('movielist.txt',"w")
    print("Input your movie please: \n")
    inp = input()
    fd.write(inp)
    fd.close()

take_input()

# Wed 8/28/19 - movie_list is a list object. Must set it equal to our ia.search_movies
# Need to find out where to put movie_list = ia.search_movies in the code, and what to 
# remove or keep.


a = int(52)
b = int(18)



def Main():
    c = """Python Movie Rating Scraper by Nickydimebags"""
    print(c)
    time.sleep(2)
    f1 = open('movielist.txt')
    movie_list = []

    for i in f1.readlines():
        movie_list.append(i)
        movie_list = ia.search_movie(i)
        movie_id = movie_list[0].movieID
        print(movie_id)
        m = ia.get_movie(movie_id)
        print(m)
        rating = m['rating']
        print(rating)
        scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',                    "https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
        creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
        client = gspread.authorize(creds)
        sheet = client.open("Movie Fridays").sheet1
        sheet.update_cell(a, b, rating) #updates specific cell

Main()

^ The a variable is what I need to update by 1 everytime the bot runs

nick_rinaldi
  • 641
  • 6
  • 17

2 Answers2

1

I am guessing the a variable tracks the row index. You could get the index of the next empty row cell in the column you are adding the values to.

def next_available_row(worksheet, col):
    return len(worksheet.col_values(col)) + 1


sheet = client.open("Movie Fridays").sheet1
sheet.update_cell(next_available_row(sheet, b), b, rating)
0

You are going to need to save the current or next value of your a variable somewhere and update it every time the script runs.

You could abuse a cell in the spreadsheet for this, or write it out to a file.

Powertieke
  • 2,368
  • 1
  • 14
  • 21