nmcassa/letterboxdpy

KeyError in `movie_rating` function when 'aggregateRating' is missing for a movie

Closed this issue · 4 comments

Traceback (most recent call last):
  File "C:\Users\user\react\letterboxdwatchlist\backend\api.py", line 17, in <module>
    movie_instance = movie.Movie("eline-vere")
                     ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\letterboxdpy\movie.py", line 27, in __init__
    self.movie_rating(dom, script)
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\letterboxdpy\movie.py", line 56, in movie_rating
    script['aggregateRating']['ratingValue'] if script else None
    ~~~~~~^^^^^^^^^^^^^^^^^^^
KeyError: 'aggregateRating'

Movie link: https://letterboxd.com/film/eline-vere/
I am on latest git version
pip install git+https://github.com/nmcassa/letterboxdpy.git
To reproduce

from letterboxdpy import movie
movie_instance = movie.Movie("eline-vere")
print(movie_instance)

I think there should be a check whether a movie has a rating or not.

Indeed, I had been considering alternative approaches for these use cases. For now, I've set the default to None.

    # letterboxd.com/film/?
    def movie_rating(self, dom, script: dict=None) -> float:
        elem = dom.find('span', attrs={'class': 'average-rating'})
        rating = elem.text if elem else None
        try:
            rating = rating if rating else (
                script['aggregateRating']['ratingValue'] if script else None
                )
            self.rating = float(rating)
        except KeyError:
            self.rating = None

In the future, I'll try to calculate something over the rating-histogram-bars and see if the ratios are consistent.

image

Thanks for the report!

Thanks that problem is solved after the change you posted.

But, I found another edge case with this movie. https://letterboxd.com/film/romeo-julie-a-tma/

  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\letterboxdpy\movie.py", line 123, in movie_popular_reviews
    data = data.find_all("div", {"class": ["film-detail-content"], })
           ^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'find_all'
# letterboxd.com/film/?
def movie_popular_reviews(self, dom) -> dict:
    data = dom.find("ul", {"class": ["film-popular-review"]})
    items = data.find_all("div", {"class": ["film-detail-content"]}) if data else []

    self.popular_reviews = []
    for item in items:
        curr = {}

        owner = item.find("strong", {"class": ["name"], })
        rating = item.find("span", {"class": ['rating'], })
        review = item.find("div", {"class": ['body-text'], }).p

        curr['reviewer'] = owner.text if owner else None
        curr['rating'] = rating.text if rating else None
        curr['review'] = review.text if review else None

        self.popular_reviews.append(curr)

fastfingertips@50449f4#diff-f94f83537966af10a02e071769a592be1cfe5e23722786dfc203325f5b4cad63R101-R117

For faster responses, you can test using my repository(#26).

pip install git+https://github.com/fastfingertips/fork.letterboxdpy.git --force