mjestecko/tools/track_listing_generator.py

93 lines
2.9 KiB
Python
Raw Normal View History

#!/usr/bin/python3
from sys import argv, exit
from os import walk, path
2023-05-26 09:00:48 +00:00
from page_shares import HEAD_EMBED, TAIL_EMBED
if len(argv) <= 1:
print("No directory was supplied")
exit(-1)
page = """<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="utf-8"/>
<title>Tracks</title>
<link type="text/css" rel="stylesheet" href="/style.css"/>
<script type="text/javascript" src="/xm.js"></script>
<script type="text/javascript">
(function (window, document) {
if (!window.XMPlayer)
2023-05-25 06:14:13 +00:00
window.XMPlayer = {};
var XMPlayer = window.XMPlayer;
2023-05-25 06:14:13 +00:00
var was_init = false
// https://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript
function b64toBlob(base64) {
const decoded = atob(base64);
const uInt8Array = new Uint8Array(decoded.length);
for (let i = 0; i < decoded.length; ++i)
uInt8Array[i] = decoded.charCodeAt(i);
return new Blob([uInt8Array]);
}
async function decompressGzippedBase64(str) {
const ds = new DecompressionStream(`gzip`);
const decompressedStream = b64toBlob(str).stream().pipeThrough(ds);
return new Response(decompressedStream).blob();
}
window.loadAndPlayTrack = function(url) {
2023-05-25 06:14:13 +00:00
if (!was_init) {
XMPlayer.init();
was_init = true;
}
var request = new XMLHttpRequest();
2023-05-25 06:14:13 +00:00
request.responseType = `text`;
request.open('GET', url);
request.send();
2023-05-25 06:14:13 +00:00
request.onload = async(_) => {
if (request.readyState === 4 && request.status === 200) {
XMPlayer.stop();
2023-05-25 06:14:13 +00:00
XMPlayer.load(await (await decompressGzippedBase64(request.response)).arrayBuffer());
XMPlayer.play();
}
}
}
})(window, document)
</script>
</head>
<body>
2023-05-26 09:00:48 +00:00
"""
page += HEAD_EMBED
2023-05-26 09:00:48 +00:00
page += """
<h3 id="articles">Tracks</h3>
<p>.xm module tracks of my own. Btw, they're playable in browser :3</p>
<p>Note that some files are rendered incorrectly.</p>
<hr/>
"""
for _, _, files in walk(argv[1]):
files.sort()
for f in files:
2023-05-25 06:14:13 +00:00
# note: Base64 gzip encoded data is expected.
if not f.endswith('.xm.txt'):
continue
page += (
2023-05-25 06:14:13 +00:00
f""" <div><p style="display: inline;">{f[:-4]}</p><button style="float: right;" onclick="window.loadAndPlayTrack('/tracks/{f}')">play</button></div>\n"""
" <hr/>\n"
)
2023-05-25 14:06:05 +00:00
break
2023-05-26 09:00:48 +00:00
page += TAIL_EMBED
page += """</div>
</body>
</html>
"""
print(page)