Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
cinemagoer library problem
#21
(Sep-06-2023, 08:06 AM)menator01 Wrote: 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()

ok so this is the code i currently have for the search by company function


def get_data_company(id):
    global cg
    company=cg.get_company(str(id))
    messagebox.showinfo(title=f"{company['name'].title()}",message=f"Last 10 films: {company}")
the result is that it prints the company name only. How could i get the titles of the films made from the assigned company? Furthermore i would have to slice the resulting list to get the 10 newest films made from the company. i only need to finish this part and im done with this application
Reply
#22
As best I can tell, the movies the company has is not in the database. The only keys I've seen is name and long imdb name.
The docs is where I've learned what I have. I think they are a little outdated. Some of the things do not work
https://imdbpy.readthedocs.io/en/latest/...start.html

ia = Cinemagoer()

company = ia.search_company('pixar')

for dept in company:
    print(dept.get('name'), dept.companyID, dept.keys())
    

data = ia.get_company(str(company[0].companyID))
print(data.get('long imdb name'))
output
Output:
Pixar Animation Studios 0017902 ['name', 'long imdb name'] Pixar 0348691 ['name', 'long imdb name'] Pixar Archives 0894998 ['name', 'long imdb name'] Pixar Canada 0514251 ['name', 'long imdb name'] Pixar Studio Tools Development 0087269 ['name', 'long imdb name'] Pixar Information Systems 0087268 ['name', 'long imdb name'] Pixar Drone 0545181 ['name', 'long imdb name'] Pixar Post Production 0239068 ['name', 'long imdb name'] Pixarvision 0593882 ['name', 'long imdb name'] Pixar Systems Group 0754045 ['name', 'long imdb name'] Fox-Pixar Media 0892370 ['name', 'long imdb name'] Pixar Talking Pictures 0791909 ['name', 'long imdb name'] PixArt 0974120 ['name', 'long imdb name'] Pixar Animation Studios
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with importing python-telegram library into the project gandonio 1 1,515 Nov-01-2022, 02:19 AM
Last Post: deanhystad
  Problem with using Crypto library reks2004 2 2,374 Mar-27-2020, 05:38 AM
Last Post: buran
  Problem installing library thunderspeed 2 2,280 Mar-22-2020, 11:04 PM
Last Post: thunderspeed
  Problem importing resource library ChrisM 8 3,866 Oct-23-2019, 01:40 PM
Last Post: ChrisM
  Problem Using Python Library abir99 8 5,296 Nov-21-2017, 03:12 PM
Last Post: sparkz_alot
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 4,743 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020