Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Commit

Permalink
feat: add theme completion report
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Aug 5, 2023
1 parent acad48e commit 3a985e0
Show file tree
Hide file tree
Showing 29 changed files with 1,288 additions and 38 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ jobs:
python -m pip install --upgrade --target=./Contents/Libraries/Shared -r \
requirements.txt --no-warn-script-location
- name: Install npm packages
working-directory: Themerr-plex.bundle
run: |
npm install
mv ./node_modules ./Contents/Resources/web
- name: Build plist
working-directory: Themerr-plex.bundle
env:
Expand Down Expand Up @@ -89,6 +95,7 @@ jobs:
!**/Themerr-plex.bundle/Dockerfile
!**/Themerr-plex.bundle/docs
!**/Themerr-plex.bundle/scripts
!**/Themerr-plex.bundle/tests
- name: Package Release
shell: bash
Expand Down
75 changes: 75 additions & 0 deletions .github/workflows/localize.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
name: localize

on:
push:
branches: [nightly]
paths: # prevents workflow from running unless these files change
- '.github/workflows/localize.yml'
- 'Contents/Strings/Themerr-plex.po'
- 'Contents/Code/**.py'
- 'Contents/Resources/web/templates/**'
workflow_dispatch:

jobs:
localize:
name: Update Localization
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Python 2.7
uses: LizardByte/.github/actions/setup_python2@nightly

- name: Set up Python Dependencies
run: |
python -m pip install --upgrade pip setuptools
python -m pip install -r requirements.txt
- name: Update Strings
run: |
python ./scripts/_locale.py --extract
- name: git diff
run: |
# disable the pager
git config --global pager.diff false
# print the git diff
git diff Contents/Scripts/plugger.po
# set the variable with minimal output, replacing `\t` with ` `
OUTPUT=$(git diff --numstat Contents/Strings/themerr-plex.po | sed -e "s#\t# #g")
echo "git_diff=${OUTPUT}" >> $GITHUB_ENV
- name: git reset
if: ${{ env.git_diff == '1 1 Contents/Scripts/plugger.po' }} # only run if more than 1 line changed
run: |
git reset --hard
- name: Get current date
id: date
run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT

- name: Create/Update Pull Request
uses: peter-evans/create-pull-request@v4
with:
add-paths: |
Contents/Scripts/*.po
token: ${{ secrets.GH_BOT_TOKEN }} # must trigger PR tests
commit-message: New localization template
branch: localize/update
delete-branch: true
base: nightly
title: New Babel Updates
body: |
Update report
- Updated ${{ steps.date.outputs.date }}
- Auto-generated by [create-pull-request][1]
[1]: https://github.com/peter-evans/create-pull-request
labels: |
babel
l10n
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,7 @@ plexhints-temp

# Remove python modules
Contents/Libraries/Shared/

# npm
node_modules/
package-lock.json
29 changes: 10 additions & 19 deletions Contents/Code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# standard imports
import re
import sys

# plex debugging
try:
Expand All @@ -27,16 +26,12 @@
from typing import Optional

# local imports
if sys.version_info.major < 3:
from default_prefs import default_prefs
from constants import issue_url_games, issue_url_movies
from plex_api_helper import add_themes, get_plex_item, plex_listener
from youtube_dl_helper import process_youtube
else:
from .default_prefs import default_prefs
from .constants import issue_url_games, issue_url_movies
from .plex_api_helper import add_themes, get_plex_item, plex_listener
from .youtube_dl_helper import process_youtube
from default_prefs import default_prefs
from constants import contributes_to, issue_url_games, issue_url_movies
from plex_api_helper import add_themes, get_plex_item, plex_listener
from youtube_dl_helper import process_youtube
from webapp import start_server
import completion_report


def ValidatePrefs():
Expand Down Expand Up @@ -124,12 +119,13 @@ def Start():
if prefs_valid.header == 'Error':
Log.Warn('Themerr-plex plug-in preferences are not valid.')

Log.Debug('Themerr-plex plug-in started.')

# start watching plex
plex_listener()
Log.Debug('plex_listener started, watching for activity from new Plex Movie agent.')

start_server() # start the web server if it is not running
Log.Debug('plug-in started.')


@handler(prefix='/music/themerr-plex', name='Themerr-plex', thumb='attribution.png')
def main():
Expand Down Expand Up @@ -194,12 +190,7 @@ class Themerr(Agent.Movies):
primary_provider = False
fallback_agent = False
accepts_from = []
contributes_to = [
'com.plexapp.agents.imdb',
'com.plexapp.agents.themoviedb',
# 'com.plexapp.agents.thetvdb', # not available as movie agent
'dev.lizardbyte.retroarcher-plex'
]
contributes_to = contributes_to

@staticmethod
def search(results, media, lang, manual):
Expand Down
27 changes: 27 additions & 0 deletions Contents/Code/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
# -*- coding: utf-8 -*-

# standard imports
import os

# plex debugging
try:
import plexhints # noqa: F401
except ImportError:
pass
else: # the code is running outside of Plex
from plexhints.core_kit import Core # core kit

app_support_directory = Core.app_support_path
plugin_identifier = 'dev.lizardbyte.themerr-plex'
plugin_support_directory = os.path.join(app_support_directory, 'Plug-in Support')
plugin_support_data_directory = os.path.join(plugin_support_directory, 'Data')
themerr_data_directory = os.path.join(plugin_support_data_directory, plugin_identifier, 'DataItems')

contributes_to = [
'tv.plex.agents.movie',
'com.plexapp.agents.imdb',
'com.plexapp.agents.themoviedb',
# 'com.plexapp.agents.thetvdb', # not available as movie agent
'dev.lizardbyte.retroarcher-plex'
]

guid_map = dict(
imdb='imdb',
tmdb='themoviedb',
Expand Down
6 changes: 5 additions & 1 deletion Contents/Code/default_prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
int_plexapi_upload_retries_max='3',
int_plexapi_upload_threads='3',
str_youtube_user='',
str_youtube_passwd=''
str_youtube_passwd='',
enum_locale='en',
str_http_host='0.0.0.0',
int_http_port='9494',
bool_log_werkzeug_messages='False'
)
90 changes: 77 additions & 13 deletions Contents/Code/plex_api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import hashlib
import os
import shutil
import sys
import time
import threading

Expand All @@ -22,7 +21,7 @@
# imports from Libraries\Shared
from future.moves import queue
import requests
from typing import Optional
from typing import Optional, Tuple
import urllib3
from urllib3.exceptions import InsecureRequestWarning
from plexapi.alert import AlertListener
Expand All @@ -31,12 +30,8 @@
from plexapi.utils import reverseSearchType

# local imports
if sys.version_info.major < 3:
from constants import guid_map, issue_url_movies
from youtube_dl_helper import process_youtube
else:
from .constants import guid_map, issue_url_movies
from .youtube_dl_helper import process_youtube
from constants import app_support_directory, guid_map, issue_url_movies
from youtube_dl_helper import process_youtube

plex = None

Expand All @@ -45,7 +40,6 @@
processing_completed = []

# constants
app_support_directory = Core.app_support_path
metadata_movie_directory = os.path.join(app_support_directory, 'Metadata', 'Movies')


Expand Down Expand Up @@ -141,6 +135,35 @@ def add_themes(rating_key, theme_files=None, theme_urls=None):
return uploaded


def get_theme_upload_path(plex_item):
# type: (any) -> str
"""
Get the path to the theme upload directory.
Get the hashed path of the theme upload directory for the item specified by the ``plex_item``.
Parameters
----------
plex_item : any
The item to get the theme upload path for.
Returns
-------
str
The path to the theme upload directory.
Examples
--------
>>> get_theme_upload_path(plex_item=...)
"...bundle/Uploads/themes..."
"""
guid = plex_item.guid
full_hash = hashlib.sha1(guid).hexdigest()
theme_upload_path = os.path.join(
metadata_movie_directory, full_hash[0], full_hash[1:] + '.bundle', 'Uploads', 'themes')
return theme_upload_path


def remove_uploaded_themes(plex_item):
# type: (any) -> None
"""
Expand All @@ -163,10 +186,7 @@ def remove_uploaded_themes(plex_item):
>>> remove_uploaded_themes(plex_item=...)
...
"""
guid = plex_item.guid
full_hash = hashlib.sha1(guid).hexdigest()
theme_upload_path = os.path.join(
metadata_movie_directory, full_hash[0], full_hash[1:] + '.bundle', 'Uploads', 'themes')
theme_upload_path = get_theme_upload_path(plex_item=plex_item)
if os.path.isdir(theme_upload_path):
shutil.rmtree(path=theme_upload_path, ignore_errors=True, onerror=remove_uploaded_themes_error_handler)

Expand Down Expand Up @@ -234,6 +254,50 @@ def upload_theme(plex_item, filepath=None, url=None):
return False


def get_database_id(item):
# type: (any) -> Tuple[Optional[str], Optional[str]]
agent = None
database_id = None
imdb_id = None # if this gets set we need to do additional processing

if item.guids: # guids is a blank list for items from legacy agents, only available for new agent items
agent = 'tv.plex.agents.movie'
for guid in item.guids:
split_guid = guid.id.split('://')
database = guid_map[split_guid[0]]
database_id = split_guid[1]

if database == 'igdb':
imdb_id = database_id
database_id = None

if database == 'themoviedb':
imdb_id = None # reset this as we won't need to process it
break
elif item.guid:
split_guid = item.guid.split('://')
agent = split_guid[0]
if agent == 'dev.lizardbyte.retroarcher-plex':
# dev.lizardbyte.retroarcher-plex://{igdb-1638}{platform-4}{(USA)}?lang=en
database_id = item.guid.split('igdb-')[1].split('}')[0]
elif agent == 'com.plexapp.agents.themoviedb':
# com.plexapp.agents.themoviedb://363088?lang=en
database_id = item.guid.split('://')[1].split('?')[0]
elif agent == 'com.plexapp.agents.imdb':
# com.plexapp.agents.imdb://tt0113189?lang=en
imdb_id = item.guid.split('://')[1].split('?')[0]

if imdb_id:
themerr_url = 'https://app.lizardbyte.dev/ThemerrDB/%s/%s/%s.json' % ('movies', 'imdb', imdb_id)
themerr_response = requests.get(url=themerr_url)

if themerr_response.status_code == requests.codes.ok:
themerr_json = themerr_response.json()
database_id = themerr_json['id']

return agent, database_id


def get_plex_item(rating_key):
# type: (int) -> any
"""
Expand Down
Loading

0 comments on commit 3a985e0

Please sign in to comment.