Posts: 26
Threads: 5
Joined: Aug 2023
hello again
ok so im trying to generate the top 10 best movies from imdb. i have the code below that i found in a tutorial but as you can see under the code the variable is empty. I tried both Cinemagoer class and IMDb class but they both just end up having the same problem
import imdb
cg=imdb.Cinemagoer()
top=cg.get_top250_movies()
for i in range(10):
print(top[i]["title"]) Error: Traceback (most recent call last):
File "C:\Users\giorg\PycharmProjects\pythonProject2\get_250_filns_imdb\main.py", line 7, in <module>
print(top[i]["title"])
IndexError: list index out of range
when i try printing the "top" variable it appears as an empty list which is why im getting the indexerror you see above
Posts: 6,798
Threads: 20
Joined: Feb 2020
Because there are fewer that 10 items? My guess is there is 1, but that is only a guess. Have you looked at the return value for get_top250_movies()?
And I don't think get_person_biography(id) is the correct way to get the filmography. It is convenient, but it does not match what is on the IMDB website.
Posts: 26
Threads: 5
Joined: Aug 2023
(Sep-05-2023, 12:24 PM)deanhystad Wrote: Because there are fewer that 10 items? My guess is there is 1, but that is only a guess. Have you looked at the return value for get_top250_movies()?
And I don't think get_person_biography(id) is the correct way to get the filmography. It is convenient, but it does not match what is on the IMDB website.
well originally i had the filmography function but it wouldn't work which is why i took menator's suggestion to use the biography function instead.
About the list, when i print it i get no values whatsoever. In the tutorial where i found the top250 function it said to assign it to a variable and then access the first index of that variable but when i do that (like i said before) i get an IndexError because there is nothing in that variable
Posts: 1,145
Threads: 114
Joined: Sep 2019
Ok this is what I came up with. Note, that some entries do not have a date
from imdb import Cinemagoer
from pandas import DataFrame
from tabulate import tabulate
ia = Cinemagoer()
people = ia.search_person('angelina jolie')
person = people[0]
person = ia.get_person(person.personID)
jobs = [job for job in person['filmography'].keys()]
movies = [movie for movie in person['filmography'][jobs[0]]]
data = []
for stuff in movies:
data.append((stuff.get('title'), str(stuff.get('year')), stuff.movieID))
headers = ['Movie', 'Release', 'Movie ID']
show = tabulate(DataFrame(data), headers=headers, tablefmt='pretty', showindex=False, colalign=('left', 'left', 'left'))
print(show) output
Output: +----------------------------------------------+---------+----------+
| Movie | Release | Movie ID |
+----------------------------------------------+---------+----------+
| Every Note Played | None | 11351272 |
| Maleficent 3 | None | 15410560 |
| Maria | None | 22893404 |
| Kung Fu Panda 4 | 2024 | 21692408 |
| Eternals | 2021 | 9032400 |
| Those Who Wish Me Dead | 2021 | 3215824 |
| The One and Only Ivan | 2020 | 3661394 |
| Come Away | 2020 | 5714470 |
| Maleficent: Mistress of Evil | 2019 | 4777008 |
| Mon Guerlain: Notes of a Woman | 2017 | 9110568 |
| Kung Fu Panda 3 | 2016 | 2267968 |
| By the Sea | 2015 | 3707106 |
| Maleficent | 2014 | 1587310 |
| Kung Fu Panda Po's Winter Wonderland | 2012 | 21360350 |
| Kung Fu Panda: Secrets of the Masters | 2011 | 1980162 |
| Kung Fu Panda 2 | 2011 | 1302011 |
| The Tourist | None | 1243957 |
| Kung Fu Panda Holiday | 2010 | 1702433 |
| Salt | 2010 | 0944835 |
| Wanted | 2008 | 0493464 |
| Changeling | 2008 | 0824747 |
| Kung Fu Panda | 2008 | 0441773 |
| Beowulf | 2007 | 0442933 |
| Entourage | 2007 | 0387199 |
| A Mighty Heart | 2007 | 0829459 |
| The Good Shepherd | 2006 | 0343737 |
| Stars in Their Eyes Kids | 2006 | 0363377 |
| Mr. & Mrs. Smith | 2005 | 0356910 |
| Alexander | 2004 | 0346491 |
| The Fever | 2004 | 0368725 |
| Shark Tale | 2004 | 0307453 |
| Sky Captain and the World of Tomorrow | 2004 | 0346156 |
| Taking Lives | 2004 | 0364045 |
| Beyond Borders | 2003 | 0294357 |
| Korn: Did My Time | 2003 | 8661174 |
| Lara Croft: Tomb Raider - The Cradle of Life | 2003 | 0325703 |
| Life or Something Like It | 2002 | 0282687 |
| Billy Bob Thornton: Angelina | 2001 | 13381238 |
| Original Sin | 2001 | 0218922 |
| Lara Croft: Tomb Raider | 2001 | 0146316 |
| Gone in 60 Seconds | 2000 | 0187078 |
| Girl, Interrupted | 1999 | 0172493 |
| The Bone Collector | 1999 | 0145681 |
| Pushing Tin | 1999 | 0120797 |
| Playing by Heart | 1998 | 0145734 |
| Hell's Kitchen | 1998 | 0129136 |
| Gia | 1998 | 0123865 |
| Playing God | 1997 | 0119906 |
| The Rolling Stones: Anybody Seen My Baby? | 1997 | 6762286 |
| George Wallace | 1997 | 0119189 |
| True Women | 1997 | 0118499 |
| Mojave Moon | 1996 | 0117070 |
| Foxfire | 1996 | 0116353 |
| Love Is All There Is | 1996 | 0116928 |
| Without Evidence | 1995 | 0176326 |
| Hackers | 1995 | 0113243 |
| Meat Loaf: Bat Out of Hell II - Picture Show | 1994 | 0364980 |
| Meat Loaf: Rock and Roll Dreams Come Through | 1994 | 7023152 |
| Alice & Viril | 1993 | 0356342 |
| Angela & Viril | 1993 | 0356360 |
| The Lemonheads: It's About Time | 1993 | 9145946 |
| Widespread Panic: Wonderin' | 1993 | 13381208 |
| Cyborg 2: Glass Shadow | 1993 | 0106639 |
| Lenny Kravitz: Stand by My Woman | 1991 | 9145838 |
| Antonello Venditti: Alta marea | 1990 | 7307196 |
| Lookin' to Get Out | 1982 | 0084268 |
+----------------------------------------------+---------+----------+
Posts: 6,798
Threads: 20
Joined: Feb 2020
Sep-05-2023, 10:21 PM
(This post was last modified: Sep-05-2023, 10:21 PM by deanhystad.)
You probably need to look up the movies to get all the movie information. Just like the biography is a glance into the actor's full film catalog, the movie information in the filmography is a glance into the movie information.
Still wondering why "One Flew Over the Cuckoo's Nest" shows up on Angelina's bio. Favorite film?
Posts: 1,145
Threads: 114
Joined: Sep 2019
Sep-05-2023, 11:50 PM
(This post was last modified: Sep-05-2023, 11:50 PM by menator01.)
Ok, this is another try I came up with. Three of the movies have not been released or production has not started
With all the queries, it takes a minute to get results
First it test for a year in the original call, if year is None, it test from a movie call using the movieID. If there is a KeyError, set a default value. I checked the production date of the three with no year. They are not in production or may not be produced.
from imdb import Cinemagoer
from pandas import DataFrame
from tabulate import tabulate
ia = Cinemagoer()
arg = 'angelina jolie'
people = ia.search_person(arg)
person = people[0]
person = ia.get_person(person.personID)
jobs = [job for job in person['filmography'].keys()]
movies = [movie for movie in person['filmography'][jobs[0]]]
data = []
for stuff in movies:
if stuff.get('year') != None:
year = stuff.get('year')
else:
if ia.get_movie(stuff.movieID).get('year'):
year = ia.get_movie(stuff.movieID).get('year')
else:
year = 'No Release Date Available'
data.append((stuff.get('title'), str(year), stuff.movieID))
data.sort(key=lambda date: date[1])
headers = ['Movie', 'Release', 'Movie ID']
show = tabulate(DataFrame(data), headers=headers, tablefmt='pretty', showindex=False, colalign=('left', 'left', 'left'))
print(show) output
Output: +----------------------------------------------+---------------------------+----------+
| Movie | Release | Movie ID |
+----------------------------------------------+---------------------------+----------+
| Lookin' to Get Out | 1982 | 0084268 |
| Antonello Venditti: Alta marea | 1990 | 7307196 |
| Lenny Kravitz: Stand by My Woman | 1991 | 9145838 |
| Alice & Viril | 1993 | 0356342 |
| Angela & Viril | 1993 | 0356360 |
| The Lemonheads: It's About Time | 1993 | 9145946 |
| Widespread Panic: Wonderin' | 1993 | 13381208 |
| Cyborg 2: Glass Shadow | 1993 | 0106639 |
| Meat Loaf: Bat Out of Hell II - Picture Show | 1994 | 0364980 |
| Meat Loaf: Rock and Roll Dreams Come Through | 1994 | 7023152 |
| Without Evidence | 1995 | 0176326 |
| Hackers | 1995 | 0113243 |
| Mojave Moon | 1996 | 0117070 |
| Foxfire | 1996 | 0116353 |
| Love Is All There Is | 1996 | 0116928 |
| Playing God | 1997 | 0119906 |
| The Rolling Stones: Anybody Seen My Baby? | 1997 | 6762286 |
| George Wallace | 1997 | 0119189 |
| True Women | 1997 | 0118499 |
| Playing by Heart | 1998 | 0145734 |
| Hell's Kitchen | 1998 | 0129136 |
| Gia | 1998 | 0123865 |
| Girl, Interrupted | 1999 | 0172493 |
| The Bone Collector | 1999 | 0145681 |
| Pushing Tin | 1999 | 0120797 |
| Gone in 60 Seconds | 2000 | 0187078 |
| Billy Bob Thornton: Angelina | 2001 | 13381238 |
| Original Sin | 2001 | 0218922 |
| Lara Croft: Tomb Raider | 2001 | 0146316 |
| Life or Something Like It | 2002 | 0282687 |
| Beyond Borders | 2003 | 0294357 |
| Korn: Did My Time | 2003 | 8661174 |
| Lara Croft: Tomb Raider - The Cradle of Life | 2003 | 0325703 |
| Alexander | 2004 | 0346491 |
| The Fever | 2004 | 0368725 |
| Shark Tale | 2004 | 0307453 |
| Sky Captain and the World of Tomorrow | 2004 | 0346156 |
| Taking Lives | 2004 | 0364045 |
| Mr. & Mrs. Smith | 2005 | 0356910 |
| The Good Shepherd | 2006 | 0343737 |
| Stars in Their Eyes Kids | 2006 | 0363377 |
| Beowulf | 2007 | 0442933 |
| Entourage | 2007 | 0387199 |
| A Mighty Heart | 2007 | 0829459 |
| Wanted | 2008 | 0493464 |
| Changeling | 2008 | 0824747 |
| Kung Fu Panda | 2008 | 0441773 |
| The Tourist | 2010 | 1243957 |
| Kung Fu Panda Holiday | 2010 | 1702433 |
| Salt | 2010 | 0944835 |
| Kung Fu Panda: Secrets of the Masters | 2011 | 1980162 |
| Kung Fu Panda 2 | 2011 | 1302011 |
| Kung Fu Panda Po's Winter Wonderland | 2012 | 21360350 |
| Maleficent | 2014 | 1587310 |
| By the Sea | 2015 | 3707106 |
| Kung Fu Panda 3 | 2016 | 2267968 |
| Mon Guerlain: Notes of a Woman | 2017 | 9110568 |
| Maleficent: Mistress of Evil | 2019 | 4777008 |
| The One and Only Ivan | 2020 | 3661394 |
| Come Away | 2020 | 5714470 |
| Eternals | 2021 | 9032400 |
| Those Who Wish Me Dead | 2021 | 3215824 |
| Kung Fu Panda 4 | 2024 | 21692408 |
| Every Note Played | No Release Date Available | 11351272 |
| Maleficent 3 | No Release Date Available | 15410560 |
| Maria | No Release Date Available | 22893404 |
Posts: 1,145
Threads: 114
Joined: Sep 2019
Sep-06-2023, 08:06 AM
(This post was last modified: Sep-06-2023, 08:36 AM by menator01.)
Here is a tkinter version. The movie search is not finished but, can search by person.
As before, it takes a minute as there are several queries being done.
# Do the imports
from imdb import Cinemagoer
import tkinter as tk
from tkinter import ttk
from urllib.request import urlopen
class Data:
'''
The Data class retrieves all information from user input
Searches person and movies returns all relevant information
'''
def __init__(self):
self.action = Cinemagoer()
def search_person(self, name):
person = self.action.search_person(name.strip())[0]
return person.get('name'), person.personID
def get_person(self, id):
person = self.action.get_person(id)
jobs = [job for job in person.get('filmography').keys()]
movies = [movie for movie in person['filmography'][jobs[0]]]
return person.get('name'), movies
def search_movies(self, title):
return self.action.search_movie(title)
def get_movie(self, movie_id):
data = []
movie = self.action.get_movie(movie_id)
return movie
class Window:
'''
Window class is for visual and has fields and buttons
for the Controller class to use
'''
def __init__(self, parent):
parent.columnconfigure(0, weight=1)
parent.rowconfigure(0, weight=1)
parent.minsize(800,600)
parent['padx'] = 5
parent['pady'] = 3
parent['bg'] = '#c0c0c0'
parent.title('Movie Search')
container = tk.Frame(parent, bg='#333333')
container.grid(column=0, row=0, sticky='news')
container.grid_columnconfigure(0, weight=3)
container.grid_rowconfigure(3, weight=3)
header = tk.Label(container, text='Movie Search', fg='#ffffff', bg='#333333')
header['font'] = 'tk.HEADER 28 bold'
header.grid(column=0, row=0, sticky='new', padx=2, pady=2)
self.msg = tk.Label(container, text='Messages: ', bg='#555555', fg='#FFFFFF', anchor='w', padx=10)
self.msg['font'] = 'tk.MENU 13 normal'
self.msg.grid(column=0, row=1, sticky='new', padx=2, pady=2)
row1 = tk.Frame(container, bg='#555555')
row1.grid(column=0, row=2, sticky='new', padx=2, pady=2)
for i in range(5):
weight = 4 if i == 3 else 3
row1.grid_columnconfigure(i, weight=weight, uniform='cols')
row2 = tk.Frame(container, bg='#555555')
row2.grid(column=0, row=3, sticky='news', padx=2, pady=2)
row2.grid_columnconfigure(0, weight=3, uniform='tree')
row2.grid_columnconfigure(2, weight=3, uniform='tree')
row2.grid_rowconfigure(0, weight=3)
row3 = tk.Frame(container, bg='#555555')
row3.grid(column=0, row=4, sticky='new', padx=2, pady=2)
row3.grid_columnconfigure(0, weight=3)
label = tk.Label(row1, text='Search by:', bg='#555555', fg='#ffffff')
label['font'] = 'None 12 normal'
label.grid(column=0, row=0, sticky='new', padx=2, pady=4)
options = ['Actor/Actress', 'Movie Title']
self.picked = tk.StringVar()
self.picked.set(options[0])
self.searchby = tk.OptionMenu(row1, self.picked, *options)
self.searchby['font'] = 'None 10 normal'
self.searchby.grid(column=1, row=0, sticky='new', padx=2, pady=4)
self.searchby.config(border=0)
label = tk.Label(row1, text='Search For:', bg='#555555', fg='#ffffff')
label['font'] = 'None 12 normal'
label.grid(column=2, row=0, sticky='new', padx=2, pady=4)
self.entry = tk.Entry(row1)
self.entry['font'] = 'None 12 normal'
self.entry.grid(column=3, row=0, sticky='new', padx=2, pady=4)
self.button = tk.Button(row1, text='Search')
self.button.grid(column=4, row=0, sticky='new', padx=2)
columns = ('Title', 'Released', 'Movie ID')
self.left_tree = ttk.Treeview(row2, columns=columns, show='headings', selectmode='browse')
for column in columns:
if column == columns[0]:
self.left_tree.column(column, minwidth=0, width=400, stretch='yes')
else:
self.left_tree.column(column, minwidth=0, width=100)
self.left_tree.heading(column, text=column.title())
self.left_tree.grid(column=0, row=0, sticky='news', padx=2)
left_scroll = tk.Scrollbar(row2, orient='vertical', command=self.left_tree.yview)
self.left_tree.config(yscrollcommand=left_scroll.set)
left_scroll.grid(column=1, row=0, sticky='ns', padx=2)
self.right_tree = ttk.Treeview(row2)
self.right_tree.grid(column=2, row=0, sticky='news', padx=2)
right_scroll = tk.Scrollbar(row2, orient='vertical', command=self.right_tree.yview)
self.right_tree.config(yscrollcommand=right_scroll.set)
right_scroll.grid(column=3, row=0, sticky='ns', padx=2)
sig = tk.Label(row3, text='my-python.org', bg='#333333', fg='#ffffff')
sig['font'] = 'None 10 normal'
sig.grid(column=0, row=0, sticky='new')
class Controller:
'''
Controller class handles all communications between the Data and Window class
'''
def __init__(self, data, window):
self.data = data
self.window = window
# Button Commands
self.window.button['command'] = self.search
def search(self):
'''
Search method will lookup either actor or movie depending on the
selection in the Window class. At the momment the search only works
for finding actors and actresses
'''
# Clear the treevies
self.window.left_tree.delete(*self.window.left_tree.get_children())
# Get the param for either person search or movie search
picked = self.window.picked.get()
# Get the name of the search. person or movie
name = self.window.entry.get().strip()
# Set alternation colors for the treeview
self.window.left_tree.tag_configure('oddrow', background='#ccceee')
self.window.left_tree.tag_configure('evenrow', background='white')
# Person was selected for search
if picked == 'Actor/Actress':
self.window.msg['text'] = f'Messages: Search results for {name.title()}'
# Clear the entry field
self.window.entry.delete(0, 'end')
# Call the Data class search_person method
person = self.data.search_person(name.strip())
# Get relevant data using the person[1] which is set to the person id
data = self.data.get_person(person[1])
# Create empty list to hold the data wanted
movie_list = []
# Loop through the data getting relevant information
# Also checking that there is a year for the movie release
# This helps in not getting any movies not yet released
for movie in data[1]:
if movie.get('year'):
movie_list.append((movie.get('title'), movie.get('year'), movie.movieID))
elif movie.get('year') == None:
year = self.data.get_movie(movie.movieID).get('year')
if year:
movie_list.append((movie.get('title'), year, movie.movieID))
else:
pass
# Loop through the movie_list and insert into the treeview with alternating colors
for index, movie in enumerate(movie_list):
if index % 2 == 0:
self.window.left_tree.insert('', index, values=(movie[0], movie[1], movie[2]), tags=('oddrow',))
else:
self.window.left_tree.insert('', index, values=(movie[0], movie[1], movie[2]), tags=('evenrow',))
else:
self.window.msg['text'] = 'Message: This function is not yet complete.'
self.window.entry.delete(0, 'end')
if __name__ == '__main__':
root = tk.Tk()
img = urlopen('https://my-python.org/images/code-forum/light/light_on.png')
data = img.read()
image = tk.PhotoImage(data=data)
root.wm_iconphoto(True, image)
controller = Controller(Data(), Window(root))
root.mainloop()
Posts: 1,145
Threads: 114
Joined: Sep 2019
Sep-07-2023, 04:47 PM
(This post was last modified: Sep-07-2023, 04:47 PM by menator01.)
One way to access the company id
AS best I can tell it's set to be dicts within a list. Can be accessed by such. When it comes to getting id's ,it's something like
arg.companyID or arg.personID. Arg being the variable assigned to loop through the list. See examples below.
from imdb import Cinemagoer
ia = Cinemagoer()
companies = ia.search_company('hbo')
print(f"Company: {companies[0].get('name')} | ID: {companies[0].companyID}") Output: Company: HBO Max | ID: 0754095
from imdb import Cinemagoer
ia = Cinemagoer()
companies = ia.search_company('hbo')
for company in companies:
print(f'Company Name: {company.get("name")} Company ID: {company.companyID}') Output: Company Name: HBO Max Company ID: 0754095
Company Name: Home Box Office (HBO) Company ID: 0008693
Company Name: HBO Documentary Company ID: 0037614
Company Name: HBO Documentary Films Company ID: 0139821
Company Name: HBO Sports Company ID: 0033562
Company Name: HBO Films Company ID: 0005861
Company Name: HBO Entertainment Company ID: 0284741
Company Name: HBO Pacific Partners of Netherlands Antilles Company ID: 0840644
Company Name: HBO Europe Company ID: 0391378
Company Name: HBO Animation Company ID: 0051804
Company Name: HBO Zone Company ID: 0167627
Company Name: HBO Studio Productions Company ID: 0080413
Company Name: HBO Video Company ID: 0148466
Company Name: HBO / Cinemax Company ID: 0368145
Company Name: HBO Go Company ID: 1010750
Company Name: HBO Brasil Company ID: 0891644
Company Name: HBO Europe Company ID: 0736914
Company Name: HBO Go Company ID: 0777335
Company Name: HBO Communications Center Company ID: 0010519
Company Name: HBO Europe Company ID: 0561930
Posts: 6,798
Threads: 20
Joined: Feb 2020
Sep-07-2023, 06:37 PM
(This post was last modified: Sep-07-2023, 06:37 PM by deanhystad.)
Quote:AS best I can tell it's set to be dicts within a list. Can be accessed by such. When it comes to getting id's ,it's something like
That is what it is supposed to look like. The "dicts" are instances of custom classes that subclass _Controller. When you index one of those _Controller "dicts" it executes code instead of doing a lookup. I wouldn't be surprised if some of these _Controller subclasses do requests and scrape more information.
Posts: 1,145
Threads: 114
Joined: Sep 2019
Here is the finished script (needs optimized) in the code share part of the forum
https://python-forum.io/thread-40687-pos...#pid172655
|