93 lines
2.9 KiB
Python
Executable File
93 lines
2.9 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
from sys import argv, exit
|
|
from os import walk, path
|
|
|
|
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)
|
|
window.XMPlayer = {};
|
|
|
|
var XMPlayer = window.XMPlayer;
|
|
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) {
|
|
if (!was_init) {
|
|
XMPlayer.init();
|
|
was_init = true;
|
|
}
|
|
var request = new XMLHttpRequest();
|
|
request.responseType = `text`;
|
|
request.open('GET', url);
|
|
request.send();
|
|
request.onload = async(_) => {
|
|
if (request.readyState === 4 && request.status === 200) {
|
|
XMPlayer.stop();
|
|
XMPlayer.load(await (await decompressGzippedBase64(request.response)).arrayBuffer());
|
|
XMPlayer.play();
|
|
}
|
|
}
|
|
}
|
|
})(window, document)
|
|
</script>
|
|
</head>
|
|
<body>
|
|
"""
|
|
page += HEAD_EMBED
|
|
|
|
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:
|
|
# note: Base64 gzip encoded data is expected.
|
|
if not f.endswith('.xm.txt'):
|
|
continue
|
|
page += (
|
|
f""" <div><p style="display: inline;">{f[:-4]}</p><button style="float: right;" onclick="window.loadAndPlayTrack('/tracks/{f}')">play</button></div>\n"""
|
|
" <hr/>\n"
|
|
)
|
|
break
|
|
|
|
page += TAIL_EMBED
|
|
|
|
page += """</div>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
print(page)
|