
import requests
import sys

API_KEY = "d4470516"
#SEARCH_TERM = "rambo 2"
SEARCH_TERM = str(sys.argv[1])
all_movies = []
page = 1

while True:
    # Use 's' for search and 'page' for pagination
    url = f"http://omdbapi.com?apikey={API_KEY}&s={SEARCH_TERM}&page={page}"
    response = requests.get(url).json()

    # Check if the API returned valid search results
    if response.get("Response") == "True":
        search_results = response.get("Search", [])
        all_movies.extend(search_results)
        
#        print(f"Title: {response['Title']}")
#        print(f"IMDb ID: {response['imdbID']}")  # Output: tt1375666
        # Stop looping if we have collected all available results
        total_results = int(response.get("totalResults", 0))
        if len(all_movies) >= total_results:
            break
            
        page += 1  # Move to the next page
    else:
        print(f"Error or end of results: {response.get('Error')}")
        break


#print(f"Fetched {len(all_movies)} movies total.")
print(f"{all_movies}")

