feat: add functions to retrieve IMDb chart lists

These charts are
- the top 250 highest rated movies
- the top 100 most popular movies
- the bottom 100 lowest rated movies
This commit is contained in:
ducklet 2024-05-10 00:12:25 +02:00
parent 4fbdb26d9c
commit 2bf5607183
7 changed files with 176 additions and 10 deletions

BIN
tests/fixtures/bottom_100.html.bz2 vendored Normal file

Binary file not shown.

BIN
tests/fixtures/most_popular_100.html.bz2 vendored Normal file

Binary file not shown.

BIN
tests/fixtures/top250.gql.json.bz2 vendored Normal file

Binary file not shown.

View file

@ -1,7 +1,16 @@
import bz2
from pathlib import Path
from unittest.mock import AsyncMock
import bs4
import pytest
from unwind import imdb
from unwind.imdb import imdb_rating_from_score, score_from_imdb_rating
testsdir = Path(__file__).parent
fixturesdir = testsdir / "fixtures"
@pytest.mark.parametrize("rating", (x / 10 for x in range(10, 101)))
def test_rating_conversion(rating: float):
@ -18,3 +27,41 @@ def test_score_conversion(score: int):
pytest.skip(f"Score cannot be mapped back correctly: {score}")
assert score == score_from_imdb_rating(imdb_rating_from_score(score))
@pytest.mark.asyncio
async def test_load_most_popular_100(monkeypatch):
with bz2.open(fixturesdir / "most_popular_100.html.bz2", "rb") as f:
html = f.read()
soup = bs4.BeautifulSoup(html, "html5lib")
monkeypatch.setattr(imdb, "asoup_from_url", AsyncMock(return_value=soup))
movie_ids = await imdb.load_most_popular_100()
assert len(movie_ids) == 100
assert all(id_.startswith("tt") for id_ in movie_ids)
@pytest.mark.asyncio
async def test_load_bottom_100(monkeypatch):
with bz2.open(fixturesdir / "bottom_100.html.bz2", "rb") as f:
html = f.read()
soup = bs4.BeautifulSoup(html, "html5lib")
monkeypatch.setattr(imdb, "asoup_from_url", AsyncMock(return_value=soup))
movie_ids = await imdb.load_bottom_100()
assert len(movie_ids) == 100
assert all(id_.startswith("tt") for id_ in movie_ids)
@pytest.mark.asyncio
async def test_load_top_250(monkeypatch):
with bz2.open(fixturesdir / "top250.gql.json.bz2", "rb") as f:
jsonstr = f.read()
monkeypatch.setattr(imdb, "adownload", AsyncMock(return_value=jsonstr))
movie_ids = await imdb.load_top_250()
assert len(movie_ids) == 250
assert all(id_.startswith("tt") for id_ in movie_ids)