Skip to content

Commit

Permalink
Add IMDb rating
Browse files Browse the repository at this point in the history
IMDb.py
  • Loading branch information
crazyuploader committed Sep 18, 2024
1 parent 13df405 commit 310cebf
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions IMDb.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ def fetch_top_50_movies() -> list[dict]:
.get("titleListItems")
)
for movie in json_data:
movie_data.append({
"name": movie["originalTitleText"],
"link": IMDB_BASE_URL + "/title/" + movie["titleId"] + "/"
})
movie_data.append(
{
"name": movie["originalTitleText"],
"link": IMDB_BASE_URL + "/title/" + movie["titleId"] + "/",
"rating": movie["ratingSummary"]["aggregateRating"],
}
)
return movie_data


Expand Down Expand Up @@ -91,8 +94,11 @@ def fetch_top_250_movies():
for movie in json_data["itemListElement"]:
movie_rank = i
movie_name = movie["item"]["name"]
movie_rating = movie["item"]["aggregateRating"]["ratingValue"]
movie_link = movie["item"]["url"]
file.write(f'"{movie_rank}", "{movie_name}", "{movie_link}"\n')
file.write(
f'"{movie_rank}", "{movie_name}", "{movie_rating}", "{movie_link}"\n'
)
i += 1
file.close

Expand Down Expand Up @@ -121,10 +127,13 @@ def fetch_top_50_shows() -> list[dict]:
.get("titleListItems")
)
for show in json_data:
show_data.append({
"name": show["originalTitleText"],
"link": IMDB_BASE_URL + "/title/" + show["titleId"] + "/"
})
show_data.append(
{
"name": show["originalTitleText"],
"link": IMDB_BASE_URL + "/title/" + show["titleId"] + "/",
"rating": show["ratingSummary"]["aggregateRating"],
}
)
return show_data


Expand All @@ -133,7 +142,7 @@ def print_top_50_movies(movies_data):
Print the Top 50 Movie names.
Args:
movies_data (list of dict): A list where each dictionary contains movie information,
movies_data (list of dict): A list where each dictionary contains movie information,
such as the Movie's name and link.
"""

Expand Down Expand Up @@ -169,18 +178,18 @@ def save_to_csv(fetched_data, file_path, content_type):
file_path (str): The file path where the data should be saved.
"""
ensure_path_directory(file_path)
if content_type == 'movies':
header = "Rank, Movie Name, Link"
if content_type == "movies":
header = "Rank, Movie Name, IMDb Rating, Link"
else:
header = "Rank, Show Name, Link"
header = "Rank, Show Name, IMDb Rating, Link"

file = open(file_path, "w")
file.write(header + "\n\n")
file.close()

file = open(file_path, "a")
for i, item in enumerate(fetched_data, 1):
file.write(f'"{i}", "{item["name"]}", "{item["link"]}"\n')
file.write(f'"{i}", "{item["name"]}", "{item["rating"]}", "{item["link"]}"\n')
file.close()


Expand All @@ -201,9 +210,15 @@ def save_to_md(fetched_data):
file.write(f"\n**Top IMDb Movies as of:** {datetime.now().date()}\n\n")
file.write(f"**IMDb Top 50 Movies Page:** [Link]({IMDB_MOVIES_SEARCH_URL})\n\n")
file.write(f"**IMDb Top 250 Movies Page:** [Link]({IMDB_TOP_250_MOVIES_URL})\n\n")
file.write("**Top 50 Movies:** [CSV File](/data/top50/movies.csv), [JSON File](/data/top50/movies.json)\n\n")
file.write("**Top 250 Movies:** [CSV File](/data/top250/movies.csv), [JSON File](/data/top250/movies.json)\n\n")
file.write("**Top 50 TV Shows:** [CSV File](/data/top50/shows.csv), [JSON File](/data/top50/shows.json)\n\n")
file.write(
"**Top 50 Movies:** [CSV File](/data/top50/movies.csv), [JSON File](/data/top50/movies.json)\n\n"
)
file.write(
"**Top 250 Movies:** [CSV File](/data/top250/movies.csv), [JSON File](/data/top250/movies.json)\n\n"
)
file.write(
"**Top 50 TV Shows:** [CSV File](/data/top50/shows.csv), [JSON File](/data/top50/shows.json)\n\n"
)
file.write("---\n\n")
file.write("## IMDb Top 50 Movies List\n\n")

Expand Down

0 comments on commit 310cebf

Please sign in to comment.