-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acad48e
commit 3c9ab72
Showing
5 changed files
with
304 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# future imports | ||
from __future__ import division | ||
|
||
# standard imports | ||
import os | ||
import sys | ||
|
||
# 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 | ||
from plexhints.log_kit import Log # log kit | ||
|
||
# imports from Libraries\Shared | ||
from typing import Optional, Tuple | ||
|
||
# local imports | ||
import plex_api_helper | ||
if sys.version_info.major < 3: | ||
from constants import contributes_to, guid_map, issue_url_games, issue_url_movies, themerr_data_directory | ||
from plex_api_helper import get_database_id | ||
else: | ||
from .constants import contributes_to, guid_map, issue_url_games, issue_url_movies, themerr_data_directory | ||
from .plex_api_helper import get_database_id | ||
|
||
|
||
def build_modify_button(item, database_info, btn_text): | ||
# type: (any, Tuple[str, str], str) -> str | ||
""" | ||
Create the modification button for the plex item. | ||
""" | ||
item_agent = database_info[0] | ||
database_id = database_info[1] | ||
|
||
if item_agent == 'dev.lizardbyte.retroarcher-plex': | ||
issue_url = issue_url_games | ||
elif item_agent in contributes_to: | ||
issue_url = issue_url_movies | ||
else: | ||
issue_url = None | ||
|
||
modify_button = "" | ||
|
||
if issue_url: | ||
issue_title = '%s (%s)' % (item.title, item.year) | ||
item_issue_url = issue_url % (issue_title, database_id) | ||
|
||
if database_id: | ||
modify_button = '<a href="{0}" class="btn btn-info" role="button">{1}</a>'.format(item_issue_url, btn_text) | ||
|
||
return modify_button | ||
|
||
|
||
def generate_report(): | ||
""" | ||
Generate a report with the theme song completion status of all items in the library. | ||
""" | ||
# get all Plex items from supported metadata agents | ||
plex_server = plex_api_helper.setup_plexapi() | ||
plex_library = plex_server.library | ||
Log.Debug('plex_library: %s' % plex_library) | ||
|
||
sections = plex_library.sections() | ||
Log.Debug('sections: %s' % sections) | ||
|
||
html = """<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Themerr-plex completion report</title> | ||
<!-- Bootstrap CSS --> | ||
<link rel="stylesheet" href="https://app.lizardbyte.dev/node_modules/bootstrap/dist/css/bootstrap.min.css"> | ||
<link rel="stylesheet" href="https://app.lizardbyte.dev/css/custom.css"> | ||
</head> | ||
<body> | ||
<!-- Bootstrap JS --> | ||
<script src="https://app.lizardbyte.dev/node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script> | ||
<div class="container"> | ||
""" | ||
for section in sections: | ||
if section.agent not in contributes_to: | ||
# todo - there is a small chance that a library with an unsupported agent could still have | ||
# a individual items that was matched with a supported agent... | ||
continue # skip unsupported metadata agents | ||
|
||
# get all items in the section | ||
all_items = section.all() | ||
Log.Debug('section count items: %s' % len(all_items)) | ||
|
||
# get all items in the section with theme songs | ||
items_with_themes = section.all(theme__exists=True) | ||
Log.Debug('section count items with themes: %s' % len(items_with_themes)) | ||
|
||
# add each section to a html report | ||
# todo - fix me up ... maybe add this to the attribution text? | ||
html += """ | ||
<div class="row"> | ||
<div class="col-12"> | ||
<h1>{0}</h1> | ||
</div> | ||
</div> | ||
""".format(section.title) | ||
|
||
# add a percent completion gauge to the report | ||
percent_complete = int(len(items_with_themes) / len(all_items) * 100) if len(items_with_themes) else 0 | ||
if percent_complete == 100: | ||
progress_color = 'bg-success' | ||
btn_color = 'btn-success' | ||
elif percent_complete > 50: | ||
progress_color = 'bg-warning' | ||
btn_color = 'btn-warning' | ||
else: | ||
progress_color = 'bg-danger' | ||
btn_color = 'btn-warning' | ||
html += """ | ||
<div class="row"> | ||
<div class="col-12"> | ||
<div class="progress"> | ||
<div class="progress-bar {0}" role="progressbar" style="width: {1}%" aria-valuenow="{1}" | ||
aria-valuemin="0" aria-valuemax="100">{1}%</div> | ||
</div> | ||
</div> | ||
</div> | ||
""".format(progress_color, percent_complete) | ||
|
||
if items_with_themes: | ||
html += """ | ||
<div class="row"> | ||
<div class="col-12"> | ||
<button class="btn {0}" type="button" data-bs-toggle="collapse" data-bs-target="#{1}_complete_collapse" | ||
aria-expanded="false" aria-controls="{1}_complete_collapse"> | ||
Completed | ||
</button> | ||
<div class="collapse" id="{1}_complete_collapse"> | ||
<table class="table table-bordered caption-top"> | ||
<caption>Items with theme songs</caption> | ||
<tr> | ||
<th>Item Name</th> | ||
<th>Year</th> | ||
<th>Edit in ThemerrDB</th> | ||
</tr> | ||
""".format('btn-success', section.title) | ||
|
||
for item in items_with_themes: | ||
database_info = get_database_id(item=item) | ||
modify_button = build_modify_button(item=item, database_info=database_info, btn_text='Edit') | ||
html += """ | ||
<tr> | ||
<td>{0}</td> | ||
<td>{1}</td> | ||
<td>{2}</td> | ||
</tr> | ||
""".format(item.title, item.year, modify_button) | ||
|
||
html += """ | ||
</table> | ||
</div> | ||
</div> | ||
</div> | ||
""" | ||
|
||
# use list comprehension to get all items without theme songs | ||
items_without_themes = [item for item in all_items if item not in items_with_themes] | ||
|
||
if items_without_themes: | ||
html += """ | ||
<div class="row"> | ||
<div class="col-12"> | ||
<button class="btn {0}" type="button" data-bs-toggle="collapse" data-bs-target="#{1}_missing_collapse" | ||
aria-expanded="false" aria-controls="{1}_missing_collapse"> | ||
Missing | ||
</button> | ||
<div class="collapse show" id="{1}_missing_collapse"> | ||
<table class="table table-bordered caption-top"> | ||
<caption>Items missing theme songs</caption> | ||
<tr> | ||
<th>Item Name</th> | ||
<th>Year</th> | ||
<th>Add to ThemerrDB</th> | ||
</tr> | ||
""".format(btn_color, section.title) | ||
|
||
for item in items_without_themes: | ||
database_info = get_database_id(item=item) | ||
modify_button = build_modify_button(item=item, database_info=database_info, btn_text='Add') | ||
html += """ | ||
<tr> | ||
<td>{0}</td> | ||
<td>{1}</td> | ||
<td>{2}</td> | ||
</tr> | ||
""".format(item.title, item.year, modify_button) | ||
|
||
html += """ | ||
</table> | ||
</div> | ||
</div> | ||
</div> | ||
""" | ||
|
||
# complete html | ||
html += """ | ||
</div> | ||
</body> | ||
</html> | ||
""" | ||
|
||
# write the report to a file | ||
report_file = os.path.join(themerr_data_directory, 'themerr-completion-report.html') | ||
Core.storage.save(filename=report_file, data=html, binary=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters