Skip to content

Commit

Permalink
Merge pull request #10 from zeroquinc/code-cleanup
Browse files Browse the repository at this point in the history
chore: cleanup and optimize code readability
  • Loading branch information
zeroquinc authored May 4, 2024
2 parents 7bca786 + 5a1f8b6 commit 70f9ec5
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 50 deletions.
31 changes: 31 additions & 0 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name-template: 'v$RESOLVED_VERSION'
tag-template: 'v$RESOLVED_VERSION'
categories:
- title: '🚀 Features'
labels:
- 'feature'
- 'enhancement'
- title: '🐛 Bug Fixes'
labels:
- 'fix'
- 'bugfix'
- 'bug'
- title: '🧰 Maintenance'
label: 'chore'
change-template: '- $TITLE @$AUTHOR (#$NUMBER)'
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks.
version-resolver:
major:
labels:
- 'major'
minor:
labels:
- 'minor'
patch:
labels:
- 'patch'
default: patch
template: |
## Changes
$CHANGES
36 changes: 36 additions & 0 deletions .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Label PRs based on PR title

on:
pull_request:
types: [opened, synchronize]

jobs:
labelPR:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install GitHub CLI
run: |
curl -OL https://github.com/cli/cli/releases/download/v2.4.0/gh_2.4.0_linux_amd64.deb
sudo dpkg -i gh_2.4.0_linux_amd64.deb
- name: Get PR number
id: pr_number
run: echo "::set-output name=number::${{ github.event.pull_request.number }}"

- name: Add label
run: |
PR_TITLE="${{ github.event.pull_request.title }}"
if [[ "$PR_TITLE" == fix:* ]]; then
gh pr edit ${{ steps.pr_number.outputs.number }} --add-label "fix"
elif [[ "$PR_TITLE" == feature:* ]]; then
gh pr edit ${{ steps.pr_number.outputs.number }} --add-label "feature"
elif [[ "$PR_TITLE" == chore:* ]]; then
gh pr edit ${{ steps.pr_number.outputs.number }} --add-label "chore"
elif [[ "$PR_TITLE" == enhancement:* ]]; then
gh pr edit ${{ steps.pr_number.outputs.number }} --add-label "enhancement"
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Release Drafter

on:
push:
branches:
- main
pull_request_target:
types: [closed]

jobs:
release:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Install Release Drafter
uses: release-drafter/release-drafter@v6
with:
config-name: release-drafter.yml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 2 additions & 3 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
"""
Global config variables
"""

LOG_LEVEL = 'INFO' # or 'DEBUG, WARNING, ERROR, CRITICAL'
TMDB_THUMBNAIL_ENABLED = False # Set this to False to disable the TMDB thumbnail
LOG_LEVEL = 'DEBUG' # or 'DEBUG, WARNING, ERROR, CRITICAL'
TMDB_THUMBNAIL_ENABLED = True # Set this to False to disable the TMDB thumbnail
TIME_REMAINING_RPC_ENABLED = True # Set this to False to disable the time remaining RPC

"""
Expand Down
31 changes: 21 additions & 10 deletions src/custom_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@

from config import LOG_LEVEL

"""
This file contains the custom logger.
"""

# Function to switch the logger according to the LOG_LEVEL variable
def switch_logger():
"""
Switch the logger configuration based on the LOG_LEVEL variable.
Explanation:
This function sets up the logger configuration based on the value of the LOG_LEVEL variable. It removes the existing logger configuration and adds a new one with specific settings determined by the LOG_LEVEL value.
Args:
None
Returns:
None
Raises:
ValueError: If the LOG_LEVEL is not 'DEBUG' or 'INFO'.
"""
logger.remove()
if LOG_LEVEL == 'DEBUG':
logger.add(sys.stdout, level='DEBUG', colorize=True, format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <red>{file}</red> | <yellow>{function}</yellow> | <white>{level}</white> | <level>{message}</level>")
elif LOG_LEVEL == 'INFO':
logger.add(sys.stdout, level='INFO', colorize=True, format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <white>{level}</white> | <level>{message}</level>")
log_configs = {
'DEBUG': {"level": 'DEBUG', "colorize": True, "format": "<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <red>{file}</red> | <yellow>{function}</yellow> | <white>{level}</white> | <level>{message}</level>"},
'INFO': {"level": 'INFO', "colorize": True, "format": "<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <white>{level}</white> | <level>{message}</level>"}
}
if LOG_LEVEL in log_configs:
logger.add(sys.stdout, **log_configs[LOG_LEVEL])
else:
raise ValueError("Invalid log level")

# Call the function to set the logger according to the LOG_LEVEL variable
switch_logger()
50 changes: 18 additions & 32 deletions src/imdb.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,46 @@
import requests
import json

from .custom_logger import logger
from .globals import port

"""
This file contains functions to get IMDb information about a media.
"""

# Function to get the media type of a media
def get_media_type(info):
media_type = info['type']
logger.debug(f"Media type: {media_type}")
# If the media type is an episode, return 'tv', this is necessary because the TMDB API uses 'tv' for TV shows and Kodi uses 'episode' for TV shows
return 'tv' if media_type == 'episode' else media_type

# Function to get the IMDb ID of a media
def get_imdb_id(info, media_type):
# If the media type is a channel, return None
if media_type == 'channel':
return None

imdb_id = None
if media_type == 'tv':
imdb_id = get_imdb_id_for_tv_show(info)
elif media_type == 'movie':
imdb_id = get_imdb_id_for_movie(info)

function_map = {'tv': get_imdb_id_for_tv_show, 'movie': get_imdb_id_for_movie}
imdb_id = function_map.get(media_type, lambda x: None)(info)
logger.debug(f"IMDb ID: {imdb_id}")
return imdb_id

# Function to get the IMDb ID of a TV show
def get_imdb_id_for_tv_show(info):
tv_show_id = info['tvshowid']
# If tvshowid is not None or -1, fetch the IMDb ID from the TV show details
if tv_show_id != -1:
imdb_id = get_imdb_id_from_tv_show_details(tv_show_id)
else:
return None
return imdb_id
return get_imdb_id_from_tv_show_details(tv_show_id) if tv_show_id != -1 else None

# Function to get the IMDb ID of a TV show via the Kodi JSONRPC API
def get_imdb_id_from_tv_show_details(tv_show_id):
tv_show_url = f"http://localhost:{port}/jsonrpc?request={{%22jsonrpc%22:%222.0%22,%22method%22:%22VideoLibrary.GetTVShowDetails%22,%22params%22:{{%22tvshowid%22:{tv_show_id},%22properties%22:[%22uniqueid%22]}},%22id%22:%22libTvShow%22}}"
tv_show_response = requests.get(tv_show_url).json()
data = {
"jsonrpc": "2.0",
"method": "VideoLibrary.GetTVShowDetails",
"params": {
"tvshowid": tv_show_id,
"properties": ["uniqueid"]
},
"id": "libTvShow"
}
tv_show_url = f"http://localhost:{port}/jsonrpc"
headers = {'content-type': 'application/json'}
tv_show_response = requests.post(tv_show_url, data=json.dumps(data), headers=headers).json()
logger.debug(f"Kodi JSONRPC search response: {tv_show_response}")
# Check if 'result' key exists in the response
if 'result' in tv_show_response and 'tvshowdetails' in tv_show_response['result'] and 'uniqueid' in tv_show_response['result']['tvshowdetails'] and 'tmdb' in tv_show_response['result']['tvshowdetails']['uniqueid']:
return tv_show_response['result']['tvshowdetails']['uniqueid']['imdb']
return None
return tv_show_response.get('result', {}).get('tvshowdetails', {}).get('uniqueid', {}).get('imdb', None)

# Function to get the TMDB ID of a TV show
def get_imdb_id_for_movie(info):
imdb_id = info['uniqueid']['imdb']
return imdb_id
return info['uniqueid']['imdb']

# Get IMDb URL
def get_imdb_url(imdb_id):
url = f"https://www.imdb.com/title/{imdb_id}/" if imdb_id else None
logger.debug(f"IMDb URL: {url}")
Expand Down
14 changes: 9 additions & 5 deletions src/letterboxd.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from .custom_logger import logger

"""
This module contains functions related to Letterboxd.
"""

# Function to get the Letterboxd URL of a media
def get_letterboxd_url(tmdb_id):
"""
Constructs the Letterboxd URL for a given TMDB ID.
Parameters:
- tmdb_id: The Movie Database (TMDB) ID of the media.
Returns:
- The Letterboxd URL string if tmdb_id is provided, else None.
"""
url = f"https://letterboxd.com/tmdb/{tmdb_id}" if tmdb_id else None
logger.debug(f"Letterboxd URL: {url}")
return url

0 comments on commit 70f9ec5

Please sign in to comment.