Skip to content

Commit

Permalink
Merge pull request #26 from dbatten5/imdb-id
Browse files Browse the repository at this point in the history
add `imdb_id` support during `Phylm` instantiation
  • Loading branch information
dbatten5 committed Nov 3, 2021
2 parents 1b9e865 + 6b12615 commit 1654027
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 11 deletions.
21 changes: 15 additions & 6 deletions docs/imdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,26 @@ imdb = Imdb(raw_title="The Matrix")

## Movie ID

If you know the IMDb movie id you can pass it to `load_sources` to perform a more
accurate search for the movie:
If you know the IMDb movie ID you can instantiate the `Phylm` class with an `imdb_id`
property:

```python
phylm.load_source("imdb", movie_id="0133093")
from phylm.sources import Imdb

imdb = Imdb(raw_title="The Matrix", imdb_id="0133093")
```

`phylm` will first perform a search based on the ID. If the ID is valid the result will
be selected, if not then it will fall back to a title search.
Then, when running `load_source` for `imdb`, `phylm` will first perform a search based
on the ID. If the ID is valid the result will be selected, if not then it will fall back
to a title search.

Alternatively, you can pass it to `load_source` with `"imdb"` as the first argument:

```python
phylm.load_source("imdb", imdb_id="0133093")
```

Alternatively, instantiate the IMDb source class with the ID:
Or instantiate the IMDb source class with the ID:

```python
from phylm.sources import Imdb
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "phylm"
version = "4.0.4"
version = "4.0.5"
description = "Phylm"
authors = ["Dom Batten <[email protected]>"]
license = "MIT"
Expand Down
27 changes: 23 additions & 4 deletions src/phylm/phylm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,31 @@
class Phylm:
"""Main `Phylm` entrypoint."""

def __init__(self, title: str) -> None:
"""Initialize a `Phylm` object."""
self.title: str = title
def __init__(self, title: str, imdb_id: Optional[str] = None) -> None:
"""Initialize a `Phylm` object.
Args:
title: the title of the movie
imdb_id: an optional `IMDb` ID of the movie
"""
self.title = title
self.imdb_id = imdb_id
self._imdb: Optional[Imdb] = None
self._mtc: Optional[Mtc] = None
self._rt: Optional[Rt] = None

def __repr__(self) -> str:
"""Return the string representation.
Returns:
the string representation
"""
return f"<class '{self.__class__.__name__}' title:'{self.title}'>"

def __str__(self) -> str:
"""Return the string representation."""
return self.__repr__()

@property
def imdb(self) -> Imdb:
"""Return the IMDb data.
Expand Down Expand Up @@ -84,7 +102,8 @@ def load_source(self, source: str, imdb_id: Optional[str] = None) -> "Phylm":
"""
if source == "imdb":
if not self._imdb:
self._imdb = Imdb(raw_title=self.title, movie_id=imdb_id)
movie_id = imdb_id or self.imdb_id
self._imdb = Imdb(raw_title=self.title, movie_id=movie_id)
return self

if source == "mtc":
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/test_phylm.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ def test_init_instance_variables(self) -> None:

assert phylm.title == title

def test_imdb_id(self) -> None:
"""
Given an `imdb_id`,
When a `Phylm` object is created with the id,
Then `imdb_id` is added as an instance variable
"""
imdb_id = "bar"

phylm = Phylm(title="foo", imdb_id=imdb_id)

assert phylm.imdb_id == imdb_id


class TestRepr:
"""Tests for the __repr__ method."""

def test_repr(self) -> None:
"""
Given a `Phylm` object,
When the string representation is retrieved,
Then a informative string is returned
"""
phylm = Phylm(title="foo")

assert repr(phylm) == "<class 'Phylm' title:'foo'>"
assert str(phylm) == "<class 'Phylm' title:'foo'>"


class TestLoadSource:
"""Tests for the `load_source` method."""
Expand Down Expand Up @@ -76,6 +103,22 @@ def test_recognized_source_imdb_with_movie_id(self, mock_imdb: MagicMock) -> Non
assert phylm.imdb == mock_imdb.return_value
mock_imdb.assert_called_once_with(raw_title="bar", movie_id="abc")

@patch(f"{MODULE_PATH}.Imdb", autospec=True)
def test_recognized_source_imdb_with_movie_id_instance_variable(
self, mock_imdb: MagicMock
) -> None:
"""
Given a `Phylm` instance,
When the `load_source` method is invoked with an `imdb_id` provided at `init`,
Then the source is loaded with the `movie_id` instance variable
"""
phylm = Phylm(title="foo", imdb_id="abc")

phylm.load_source("imdb")

assert phylm.imdb == mock_imdb.return_value
mock_imdb.assert_called_once_with(raw_title="foo", movie_id="abc")

@patch(f"{MODULE_PATH}.Mtc", autospec=True)
def test_recognized_source_mtc(self, mock_mtc: MagicMock) -> None:
"""
Expand Down

0 comments on commit 1654027

Please sign in to comment.