Skip to content

Commit

Permalink
Fixes tests. Refactored the main plexorcist file
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbc committed Feb 19, 2024
1 parent 2d891e1 commit b33c0d9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
74 changes: 41 additions & 33 deletions plexorcist.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ def convert_to_library_ids(self, libraries):
available_libraries = self.get_available_libraries()

return [
int(library)
if library.isdigit()
else self.get_library_id_by_name(library, available_libraries)
(
int(library)
if library.isdigit()
else self.get_library_id_by_name(library, available_libraries)
)
for library in libraries
if library
]
Expand Down Expand Up @@ -252,43 +254,49 @@ def is_watched_video(video):

return watched_videos

def delete_videos(self, watched_videos, media_type):
"""Delete watched videos and send notification"""
def get_title(self, video, media_type):
"""Get the video title"""

# Get the video title
def get_title(video):
if media_type == "show":
series = video.get("@grandparentTitle", "")
return f"{series} - {video['@title']}"
if media_type == "show":
series = video.get("@grandparentTitle", "")
return f"{series} - {video['@title']}"

return video["@title"]
return video["@title"]

# Check if the video is whitelisted
def is_whitelisted(video):
title = get_title(video)
check = (
title in self.config["whitelist"]
or video.get("@grandparentTitle", "") in self.config["whitelist"]
)
if check:
logging.info(self.config["i18n"]["whitelisted"].format(title))
return check
def is_whitelisted(self, video, media_type):
"""Check if the video is whitelisted"""

# Get the video size
def get_size(video):
return round(int(video["Media"]["Part"]["@size"]) / (1024 * 1024), 2)
title = self.get_title(video, media_type)
check = (
title in self.config["whitelist"]
or video.get("@grandparentTitle", "") in self.config["whitelist"]
)
if check:
logging.info(self.config["i18n"]["whitelisted"].format(title))
return check

# Delete the video
def delete_video(video):
self.util.make_request(
url=self.config["plex_base"] + video["@key"],
headers={"X-Plex-Token": self.config["plex_token"]},
request_type="delete",
)
return get_size(video), get_title(video)
def get_size(self, video):
"""Get the video size"""

return round(int(video["Media"]["Part"]["@size"]) / (1024 * 1024), 2)

def delete_video(self, video, media_type):
"""Delete the video"""

self.util.make_request(
url=self.config["plex_base"] + video["@key"],
headers={"X-Plex-Token": self.config["plex_token"]},
request_type="delete",
)
return self.get_size(video), self.get_title(video, media_type)

def delete_videos(self, watched_videos, media_type):
"""Delete watched videos and send notification"""

deleted_videos = [
delete_video(video) for video in watched_videos if not is_whitelisted(video)
self.delete_video(video, media_type)
for video in watched_videos
if not self.is_whitelisted(video, media_type)
]

if deleted_videos:
Expand Down
12 changes: 6 additions & 6 deletions test_plexorcist.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,30 +114,30 @@ def test_get_title_show(self):
# Test get_title method for show media type
plexorcist = Plexorcist()
video = {"@grandparentTitle": "Grandparent", "@title": "Title"}
title = plexorcist.get_title(video)
title = plexorcist.get_title(video, "show")
self.assertEqual(title, "Grandparent - Title")

def test_get_title_movie(self):
# Test get_title method for movie media type
plexorcist = Plexorcist()
video = {"@title": "Title"}
title = plexorcist.get_title(video)
self.assertEqual(title, "Title")
video = {"@title": "Title", "@grandparentTitle": "Series 1"}
title = plexorcist.get_title(video, "show")
self.assertEqual(title, "Series 1 - Title")

def test_is_whitelisted(self):
# Test is_whitelisted method
plexorcist = Plexorcist()
plexorcist.config = {"whitelist": ["Whitelisted Title"]}
video = {"@title": "Title"}
is_whitelisted = plexorcist.is_whitelisted(video)
is_whitelisted = plexorcist.is_whitelisted(video, "show")
self.assertFalse(is_whitelisted)

def test_get_size(self):
# Test get_size method
plexorcist = Plexorcist()
video = {"Media": {"Part": {"@size": "1024"}}}
size = plexorcist.get_size(video)
self.assertEqual(size, 0.001)
self.assertEqual(size, 0.0)

@patch("plexorcist.utils.Utils.make_request")
def test_convert_to_library_ids(self, mock_make_request):
Expand Down

0 comments on commit b33c0d9

Please sign in to comment.