Python Forum
cinemagoer library problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: cinemagoer library problem (/thread-40671.html)

Pages: 1 2 3


cinemagoer library problem - lunacy90 - Sep-04-2023

I am trying to make an application that in part prints out the last 10 films released based on actor which means i want to exclude announced films if they haven/t been released yet

def get_data_actor_name(id):
    global movie_name,cg
    search = cg.get_person(id)
    filmography=cg.get_person_filmography(id)
    last_ten_films=filmography['data']['filmography']['actor']


    for movie in last_ten_films:
        if bool(movie['year']):
            movie_name.append(movie['title'], movie['year'])

    messagebox.showinfo(title=f"{search}'s latest film information", message=f"{movie_name}")
    movie_name.clear()
the code above returns a KeyError (year) as you can see below

Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\giorg\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__ return self.func(*args) File "C:\Users\giorg\PycharmProjects\pythonProject2\IMDB\main.py", line 65, in search_for_info check_answer(search) File "C:\Users\giorg\PycharmProjects\pythonProject2\IMDB\main.py", line 38, in check_answer get_data_actor_name(id) File "C:\Users\giorg\PycharmProjects\pythonProject2\IMDB\main.py", line 21, in get_data_actor_name if bool(movie['year']): File "C:\Users\giorg\AppData\Local\Programs\Python\Python39\lib\site-packages\imdb\utils.py", line 1503, in __getitem__ rawData = self.data[key] KeyError: 'year'



RE: cinemagoer library problem - menator01 - Sep-04-2023

Where is the data coming from? Is it coming from a database? I don't really understand why using the global.
Need a little more information please.


RE: cinemagoer library problem - deanhystad - Sep-04-2023

The data comes from Cinemagoer, an IMDB scraper package. Lunacy probably should have posted this question in the original thread instead of starting a new one and losing all the context.

https://python-forum.io/thread-40667.html

'year' must not be a key in movie. You should print out all the keys in movie to find out what they are.

There is no need for the global declaration. You only need to declare a variable name as global if you assign a value to the name. In get_data_actor_name() there are no assignments made to cg, and movie_name is obviously a local variable because it is cleared at the end of the function.

Posted code should be runable. This means including imports and any setup code.


RE: cinemagoer library problem - lunacy90 - Sep-04-2023

(Sep-04-2023, 06:45 PM)deanhystad Wrote: The data comes from Cinemagoer, an IMDB scraper package. Lunacy probably should have posted this question in the original thread instead of starting a new one and losing all the context.

https://python-forum.io/thread-40667.html

'year' must not be a key in movie. You should print out all the keys in movie to find out what they are.

There is no need for the global declaration. You only need to declare a variable name as global if you assign a value to the name. In get_data_actor_name() there are no assignments made to cg, and movie_name is obviously a local variable because it is cleared at the end of the function.

Posted code should be runable. This means including imports and any setup code.

and how can i print all of the keys in "movie"? cause when i tried printing i can only see the name of the feature and no other information


RE: cinemagoer library problem - deanhystad - Sep-04-2023

Maybe that is all there is. Or maybe there is the name of the move and an ID, and you need to look up the move to get the movie information. I always felt it was called web scraping because that is what you have to do. Scrape away layer after layer of information to finally find what you are looking for.


RE: cinemagoer library problem - menator01 - Sep-05-2023

One way to do it
from imdb import Cinemagoer
from pandas import DataFrame as df
from tabulate import tabulate

class Model:
    def __init__(self):
        self.ia = Cinemagoer()

    def search_person(self, name):
        data = []
        people = self.ia.search_person(name.strip())
        person = people[0]
        id = person.personID
        films = self.ia.get_person_biography(id)
        for film in films['titlesRefs'].values():
            try:
                year = film['year']
            except KeyError:
                year = 'No date available'

            data.append((film['title'], year, film.movieID))
        return data
    
    def search_movie(self, title):
        data = []
        movies = self.ia.search_movie(title.strip())
        for movie in movies:
            data.append((movie['title'], movie['year'], movie.movieID))
        return data
  


m = Model()
data = m.search_person('angelina jolie')
data.sort(key=lambda a: a[1])

headers = ['Movie', 'Year', 'Movie ID']

print('All Movies')
print(tabulate(df(data), showindex=False, headers=headers))

print()


# Search < date
alist = []
for movie, year, id in data:
       if isinstance(year, int) and year <= 1998:
            alist.append((movie, year, id))

print('Movies less than or equal to 1998')
print(tabulate(df(alist), showindex=False, headers=headers))

print()
data.reverse()
print('Last five movies')
print(tabulate(df(data[:5]), showindex=False, headers=headers))
output
Output:
All Movies Movie Year Movie ID ------------------------------- ------ ---------- One Flew Over the Cuckoo's Nest 1975 0073486 Hackers 1995 0113243 Foxfire 1996 0116353 True Women 1997 0118499 George Wallace 1997 0119189 Gia 1998 0123865 Girl, Interrupted 1999 0172493 The Bone Collector 1999 0145681 Pushing Tin 1999 0120797 The Matrix 1999 0133093 Lara Croft: Tomb Raider 2001 0146316 Beyond Borders 2003 0294357 Mr. & Mrs. Smith 2005 0356910 Wanted 2008 0493464 Salt 2010 0944835 Maleficent 2014 1587310 Unbroken 2014 1809398 By the Sea 2015 3707106 Eternals 2021 9032400 Movies less than or equal to 1998 Movie Year Movie ID ------------------------------- ------ ---------- One Flew Over the Cuckoo's Nest 1975 0073486 Hackers 1995 0113243 Foxfire 1996 0116353 True Women 1997 0118499 George Wallace 1997 0119189 Gia 1998 0123865 Last five movies Movie Year Movie ID ---------- ------ ---------- Eternals 2021 9032400 By the Sea 2015 3707106 Unbroken 2014 1809398 Maleficent 2014 1587310 Salt 2010 0944835



RE: cinemagoer library problem - deanhystad - Sep-05-2023

This does not agree with IMDB. Tough being cast in a move filmed before you are born. Missing the Kung Fu Pandas movies, one of the Lara Croft movies. Maybe get_person_biography is not the way to get this info?


RE: cinemagoer library problem - menator01 - Sep-05-2023

Could use filmography but, returns a lot of stuff that I don't know if it even involves jolie
I think it's the database. I used harrison ford and got movies that he was not in. The years were 1962 and 1967.
Samething with Jason Statham. The database has input year errors.

Here is the ouput for Statham

Output:
Jason Statham 0005458 +---------------------------------------+------+---------+ | Title | Year | ID | +---------------------------------------+------+---------+ | The Italian Job | 1969 | 0064505 | | Lock, Stock and Two Smoking Barrels | 1998 | 0120735 | | The Bank Job | 2008 | 0200465 | | Snatch | 2000 | 0208092 | | Turn It Up | 2000 | 0216772 | | Ghosts of Mars | 2001 | 0228333 | | The One | 2001 | 0267804 | | Mean Machine | 2001 | 0291341 | | The Transporter | 2002 | 0293662 | | The Italian Job | 2003 | 0317740 | | Death Race | 2008 | 0452608 | | Crank | 2006 | 0479884 | | War | 2007 | 0499556 | | Crank: High Voltage | 2009 | 1121931 | | The Expendables | 2010 | 1320253 | | Fast & Furious 6 | 2013 | 1905041 | | Homefront | 2013 | 2312718 | | Furious 7 | 2015 | 2820852 | | Spy | 2015 | 3079380 | | The Fate of the Furious | 2017 | 4630562 | | Fast & Furious Presents: Hobbs & Shaw | 2019 | 6806448 | +---------------------------------------+------+---------+



RE: cinemagoer library problem - lunacy90 - Sep-05-2023

(Sep-05-2023, 03:24 AM)menator01 Wrote: Could use filmography but, returns a lot of stuff that I don't know if it even involves jolie
I think it's the database. I used harrison ford and got movies that he was not in. The years were 1962 and 1967.
Samething with Jason Statham. The database has input year errors.

Here is the ouput for Statham

Output:
Jason Statham 0005458 +---------------------------------------+------+---------+ | Title | Year | ID | +---------------------------------------+------+---------+ | The Italian Job | 1969 | 0064505 | | Lock, Stock and Two Smoking Barrels | 1998 | 0120735 | | The Bank Job | 2008 | 0200465 | | Snatch | 2000 | 0208092 | | Turn It Up | 2000 | 0216772 | | Ghosts of Mars | 2001 | 0228333 | | The One | 2001 | 0267804 | | Mean Machine | 2001 | 0291341 | | The Transporter | 2002 | 0293662 | | The Italian Job | 2003 | 0317740 | | Death Race | 2008 | 0452608 | | Crank | 2006 | 0479884 | | War | 2007 | 0499556 | | Crank: High Voltage | 2009 | 1121931 | | The Expendables | 2010 | 1320253 | | Fast & Furious 6 | 2013 | 1905041 | | Homefront | 2013 | 2312718 | | Furious 7 | 2015 | 2820852 | | Spy | 2015 | 3079380 | | The Fate of the Furious | 2017 | 4630562 | | Fast & Furious Presents: Hobbs & Shaw | 2019 | 6806448 | +---------------------------------------+------+---------+
thanks a lot for your help guys. Ill add your usernames in my github page as collaborators


RE: cinemagoer library problem - lunacy90 - Sep-05-2023

(Sep-05-2023, 03:24 AM)menator01 Wrote: Could use filmography but, returns a lot of stuff that I don't know if it even involves jolie
I think it's the database. I used harrison ford and got movies that he was not in. The years were 1962 and 1967.
Samething with Jason Statham. The database has input year errors.

Here is the ouput for Statham

Output:
Jason Statham 0005458 +---------------------------------------+------+---------+ | Title | Year | ID | +---------------------------------------+------+---------+ | The Italian Job | 1969 | 0064505 | | Lock, Stock and Two Smoking Barrels | 1998 | 0120735 | | The Bank Job | 2008 | 0200465 | | Snatch | 2000 | 0208092 | | Turn It Up | 2000 | 0216772 | | Ghosts of Mars | 2001 | 0228333 | | The One | 2001 | 0267804 | | Mean Machine | 2001 | 0291341 | | The Transporter | 2002 | 0293662 | | The Italian Job | 2003 | 0317740 | | Death Race | 2008 | 0452608 | | Crank | 2006 | 0479884 | | War | 2007 | 0499556 | | Crank: High Voltage | 2009 | 1121931 | | The Expendables | 2010 | 1320253 | | Fast & Furious 6 | 2013 | 1905041 | | Homefront | 2013 | 2312718 | | Furious 7 | 2015 | 2820852 | | Spy | 2015 | 3079380 | | The Fate of the Furious | 2017 | 4630562 | | Fast & Furious Presents: Hobbs & Shaw | 2019 | 6806448 | +---------------------------------------+------+---------+
one more question, hopefully it will be the last. I try to search via company name with the search_company function. I then pass the definitive name to the get_company function which requests an ID for the parameter. How can i get the ID of the company straight from the search_company function? I tried printing it but i only get the name of the company printed. ive tried a generic movie_id or movieId or id option but i get a KeyError

my code follows

this is the function that gets all the results based by the name to find the correct company name which the user is looking for.


        for i in range(len(search)):
            if messagebox.askokcancel(title=f"Company Name",message=f"Is {search[i]}  the company you are looking for?"):
                id=search[i]
                print(id)
                get_data_company(id)
the above function loads another function that will follow below


def get_data_company(id):
    print(id)
company=cg.get_company(str(id))
    print(company["name"])
the elements i have yet to add to this function are the name retrievals from the films and i want to create a new list of the 10 most recent films made by the company that the user is looking for


EDIT: i set up the search by actor name function. It works ok but there is a small problem. I splice the actors filmography to get the first 10 films sorted by release year but it doesnt actually contain the newest ones. For example i searched for Keanu Reeves and his newest film in the list was Matrix Revolutions not John Wick Chapter 4 as it is in real life. My code follows below

def get_data_actor_name(id):
    data=[]
    global cg
    search = cg.search_person(id)
    person=search[0]
    filmography=cg.get_person_biography(id)
    for film in filmography["titlesRefs"].values():
        try:
            year=film["year"]
        except KeyError:
            year="No date available"
        data.append((film["title"],year))
    data.sort(key=lambda a: a[1])
    data.reverse()
    headers=["Movie","Year"]
    filmography=data[:10]
    messagebox.showinfo(title=f"{search}'s latest film information", message=f"{headers}\n"
                                                                            f"{filmography[0]}\n"
                                                                             f"{filmography[1]}\n"
                                                                             f"{filmography[2]}\n"
                                                                             f"{filmography[3]}\n"
                                                                             f"{filmography[4]}\n"
                                                                             f"{filmography[5]}\n"
                                                                             f"{filmography[6]}\n"
                                                                             f"{filmography[7]}\n"
                                                                             f"{filmography[8]}\n"
                                                                             f"{filmography[9]}\n"
                                                                             f"")