2022-02-10 11:32:45 +00:00
|
|
|
from musicutil.types import File
|
2022-02-06 19:46:31 +00:00
|
|
|
from musicutil.utils.load_tag_information import load_tag_information
|
|
|
|
from musicutil.utils.scan_for_music import scan_for_music
|
|
|
|
|
2022-02-07 10:35:15 +00:00
|
|
|
from html import escape as escape_html
|
|
|
|
|
2022-02-06 19:46:31 +00:00
|
|
|
class GenHTMLArgs():
|
|
|
|
src: str
|
|
|
|
dest: str
|
|
|
|
title: str
|
|
|
|
description: str
|
2022-02-10 12:03:23 +00:00
|
|
|
by_folder: bool
|
2022-02-06 19:46:31 +00:00
|
|
|
|
|
|
|
def add_genhtml_command(subparsers):
|
|
|
|
genhtml_parser = subparsers.add_parser('genhtml')
|
|
|
|
genhtml_parser.add_argument(
|
|
|
|
'src',
|
|
|
|
type=str,
|
|
|
|
help='src base music directory')
|
|
|
|
genhtml_parser.add_argument(
|
|
|
|
'dest',
|
|
|
|
type=str,
|
|
|
|
help='dest html file')
|
|
|
|
genhtml_parser.add_argument(
|
|
|
|
'title',
|
|
|
|
type=str,
|
|
|
|
help='title')
|
|
|
|
genhtml_parser.add_argument(
|
|
|
|
'description',
|
|
|
|
type=str,
|
|
|
|
help='description')
|
2022-02-10 12:03:23 +00:00
|
|
|
genhtml_parser.add_argument(
|
|
|
|
'--by-folder',
|
|
|
|
action='store_true')
|
2022-02-06 19:46:31 +00:00
|
|
|
|
|
|
|
def get_genhtml_args(args) -> GenHTMLArgs:
|
|
|
|
command_args = GenHTMLArgs()
|
|
|
|
command_args.src = args.src
|
|
|
|
command_args.dest = args.dest
|
|
|
|
command_args.title = args.title
|
|
|
|
command_args.description = args.description
|
2022-02-10 12:03:23 +00:00
|
|
|
command_args.by_folder = args.by_folder
|
2022-02-06 19:46:31 +00:00
|
|
|
return command_args
|
|
|
|
|
2022-02-10 12:03:23 +00:00
|
|
|
def make_table_for_files(files: list[File], by_folder: bool = False) -> str:
|
2022-02-10 11:32:45 +00:00
|
|
|
html_content = ""
|
2022-02-10 12:03:23 +00:00
|
|
|
path_head = "<th>Path</th>" if not by_folder else ""
|
|
|
|
|
|
|
|
html_content += f"""
|
2022-02-10 11:32:45 +00:00
|
|
|
<table class="pure-table pure-table-horizontal">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
2022-02-10 12:03:23 +00:00
|
|
|
{path_head}
|
2022-02-10 11:32:45 +00:00
|
|
|
<th>Title</th>
|
|
|
|
<th>Artist</th>
|
|
|
|
<th>Format</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
"""
|
|
|
|
isOdd = True
|
2022-02-10 12:03:23 +00:00
|
|
|
print(files)
|
2022-02-10 11:32:45 +00:00
|
|
|
for file in files:
|
|
|
|
tdClass = "pure-table-odd" if isOdd else "pure-table-even"
|
|
|
|
data_path = escape_html(file.path_from_src)
|
|
|
|
data_title = escape_html(file.tags.title)
|
|
|
|
data_artist = escape_html(file.tags.artist)
|
|
|
|
data_extension = escape_html(file.extension)
|
2022-02-10 12:03:23 +00:00
|
|
|
path_data = f"<td>{data_path}</td>" if not by_folder else ""
|
|
|
|
|
2022-02-10 11:32:45 +00:00
|
|
|
html_content += f"""
|
|
|
|
<tr class="{tdClass}">
|
2022-02-10 12:03:23 +00:00
|
|
|
{path_data}
|
2022-02-10 11:32:45 +00:00
|
|
|
<td>{data_title}</td>
|
|
|
|
<td>{data_artist}</td>
|
|
|
|
<td>{data_extension}</td>
|
|
|
|
</tr>
|
|
|
|
"""
|
|
|
|
isOdd = not isOdd
|
|
|
|
html_content += """
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
"""
|
2022-02-10 12:03:23 +00:00
|
|
|
return html_content
|
2022-02-10 11:32:45 +00:00
|
|
|
|
2022-02-06 19:46:31 +00:00
|
|
|
class GenHTMLCommand():
|
|
|
|
def __init__(self, args: GenHTMLArgs):
|
|
|
|
self.args = args
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
print("Generating HTML...")
|
|
|
|
print("Scanning For Music...")
|
|
|
|
files = scan_for_music(self.args.src)
|
|
|
|
print("Loading Tag Information")
|
|
|
|
for file in files:
|
|
|
|
tags = load_tag_information(file)
|
|
|
|
file.tags = tags
|
|
|
|
|
|
|
|
files.sort(key=lambda file: file.tags.artist)
|
|
|
|
files.sort(key=lambda file: file.tags.title)
|
|
|
|
files.sort(key=lambda file: file.path_from_src)
|
|
|
|
|
|
|
|
html_content = ""
|
|
|
|
html_content += f"""
|
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
<link rel="stylesheet" href="https://unpkg.com/purecss@2.0.6/build/pure-min.css" integrity="sha384-Uu6IeWbM+gzNVXJcM9XV3SohHtmWE+3VGi496jvgX1jyvDTXfdK+rfZc8C1Aehk5" crossorigin="anonymous">
|
|
|
|
<title>{self.args.title}</title>
|
|
|
|
<meta property="og:title" content="{self.args.title}" />
|
|
|
|
<meta property="og:description" content="{self.args.description}" />
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
"""
|
|
|
|
|
2022-02-10 12:03:23 +00:00
|
|
|
if self.args.by_folder:
|
|
|
|
files_per_folder: dict[str, list[File]] = {}
|
|
|
|
for file in files:
|
|
|
|
if not file.path_from_src in files_per_folder.keys():
|
|
|
|
files_per_folder[file.path_from_src] = []
|
|
|
|
files_per_folder[file.path_from_src].append(file)
|
|
|
|
for folder in files_per_folder.keys():
|
|
|
|
folder_name = folder
|
|
|
|
folder_files = files_per_folder[folder_name]
|
|
|
|
html_content += f"<h1>{folder_name}</h1>"
|
|
|
|
html_content += make_table_for_files(folder_files, True)
|
|
|
|
else:
|
|
|
|
html_content += make_table_for_files(files)
|
2022-02-07 10:35:15 +00:00
|
|
|
|
2022-02-06 19:46:31 +00:00
|
|
|
html_content += """
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
2022-02-10 11:32:45 +00:00
|
|
|
|
2022-02-06 19:46:31 +00:00
|
|
|
print("Writing HTML...")
|
|
|
|
with open(self.args.dest, "w+") as html_file:
|
|
|
|
html_file.write(html_content)
|
|
|
|
|
|
|
|
|
|
|
|
|