Skip to content

Commit

Permalink
Merge pull request #2 from stefanbc/tests
Browse files Browse the repository at this point in the history
Introduces tests for the main Plexorcist class file
  • Loading branch information
stefanbc authored Feb 21, 2024
2 parents c8dafc6 + 83f8e91 commit e440b84
Show file tree
Hide file tree
Showing 4 changed files with 385 additions and 82 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
python-version: ['3.8', '3.9', '3.10']
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
pip install -r requirements.txt
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
- uses: actions/checkout@v4.1.1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint
pip install -r requirements.txt
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
44 changes: 22 additions & 22 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
python-version: ['3.8', '3.9', '3.10']
steps:
- uses: actions/checkout@v4.1.1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install coverage
- name: Run tests
run: |
coverage run -m unittest discover
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Upload coverage to Code Climate
uses: paambaati/codeclimate-action@v5.0.0
env:
CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE_TOKEN }}
- uses: actions/checkout@v4.1.1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install coverage
- name: Run tests
run: |
coverage run -m unittest discover
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Upload coverage to Code Climate
uses: paambaati/codeclimate-action@v5.0.0
env:
CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE_TOKEN }}
91 changes: 50 additions & 41 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 All @@ -204,8 +206,8 @@ def get_available_libraries(self):
)

if response is not None:
data = xmltodict.parse(response.content)
return data["MediaContainer"]["Directory"]
data = xmltodict.parse(response.content, force_list=True)
return data["MediaContainer"][0]["Directory"]

return []

Expand All @@ -220,24 +222,25 @@ def get_library_id_by_name(self, library_name, available_libraries):
def handle_videos(self, response):
"""Handle videos"""

data = xmltodict.parse(response.content)
videos = data["MediaContainer"]["Video"]
media_type = data["MediaContainer"]["@viewGroup"]
data = xmltodict.parse(response.content, force_list=True)
videos = data["MediaContainer"][0]["Video"]
media_type = data["MediaContainer"][0]["@viewGroup"]

if videos and len(videos) > 0:
# Filter watched videos
watched_videos = self.filter_videos(videos=videos)
watched_videos = self.filter_videos(videos)

# Delete watched videos and send notification
self.delete_videos(watched_videos=watched_videos, media_type=media_type)
self.delete_videos(watched_videos, media_type)

def filter_videos(self, videos):
"""Filter videos"""

# Check if video was watched and / or is older than
def is_watched_video(video):
return (
video.get("@viewCount")
isinstance(video, dict)
and video.get("@viewCount")
and int(video["@viewCount"]) >= 1
and (
self.config["older_than"] == 0
Expand All @@ -252,43 +255,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"][0]["Part"][0]["@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
Loading

0 comments on commit e440b84

Please sign in to comment.