DEV Community

Cover image for Simple Movie rec sys
Lukas
Lukas

Posted on

Simple Movie rec sys

I want to improve my Algorithm and Data structure Knowledge.
In the following blog post, I will guide you through a sample project you can create for your portfolio or as an Idea of how to use Data structures like a Tree, Hashmap, etc...
The key intention behind this blog post is to create simple projects that can be used in your portfolio to highlight your skills. 
The main focus in this post is to use:

  1. API for data 
  2. Data structures like TreeNodes
  3. Command prompt

The project uses themoviedb API to fetch the movie data of 20 movies belonging to a certain genre. The API key can get generated after you created an account under themoviedb.
In the next step I use the json and requests library.
genres = requests.get(f"https://api.themoviedb.org/3/genre/movie/list?api_key={api_key}&language=en-US")
genre_data = genres.json()

The api data looks like the following:
{'genres': [{'id': 28, 'name': 'Action'},
{'id': 12, 'name': 'Adventure'},
{'id': 16, 'name': 'Animation'},
{'id': 35, 'name': 'Comedy'},
{'id': 80, 'name': 'Crime'},
{'id': 99, 'name': 'Documentary'},
{'id': 18, 'name': 'Drama'},
{'id': 10751, 'name': 'Family'},
{'id': 14, 'name': 'Fantasy'},
{'id': 36, 'name': 'History'},
{'id': 27, 'name': 'Horror'},
{'id': 10402, 'name': 'Music'},
{'id': 9648, 'name': 'Mystery'},
{'id': 10749, 'name': 'Romance'},
{'id': 878, 'name': 'Science Fiction'},
{'id': 10770, 'name': 'TV Movie'},
{'id': 53, 'name': 'Thriller'},
{'id': 10752, 'name': 'War'},
{'id': 37, 'name': 'Western'}]}

Through the API we got all available genres in the next step we use the genres to collect 20 movies from each genre.

data = []
for genre in genre_name:
    r = requests.get(f"https://api.themoviedb.org/3/discover/movie?api_key={api_key}&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres={genre}&with_watch_monetization_types=flatrate")
    movie_data = r.json()

    for i in movie_data['results']:
        movie_temp = []
        movie = [i['title'],i['vote_average'],i['release_date'],i['popularity'],i['overview'], genre]
        movie_temp.append(movie)
        data += movie_temp 
Enter fullscreen mode Exit fullscreen mode

genre_name is a list containing all genres. We iterate over each genre and change the API key according to that. In the next step, we iterate over the API result and place it in a list of lists that get appended to data. The resulting list of list looks like the following:

[['Spider-Man: No Way Home', 8.6, '2021-12-15', 20686.826, 'Peter Parker is unmasked and no longer able to separate...', 'Action'],
['Venom: Let There Be Carnage', 7.2, '2021-09-30', 7992.617, 'After finding a host body in investigative reporter Eddie Brock,...', 'Action']]

After getting the data and saving it in video_data.py we create a simple TreeNode class that will be used as a data structure for the project. The tree is simple and contains a start node and a node for each genre. Each genre node contains 20 movies which add up to 380 Nodes from the 19 genres.

The next step is to get the user input and the information of what genre he wants to watch. The user has several methods to select the movie. The first way is using a number from 1-19 or writing the genre into the command line.
The user gets asked after that if he wants to see movies from the selected genre. After that, he gets prompted again to get the amount of recommended movies. In the end, the user can get another recommendation or quit the recommendation sys.

The following is a gif that highlights the working of the rec sys.

Working recsys

Thank you very much for your time and have fun exploring my github repository and feedback of any sort is welcomed.

cover image from unsplash

Top comments (0)