mirror of
https://github.com/jayanta525/github-pages-directory-listing.git
synced 2025-06-18 14:15:33 -04:00
python: add icon png, fix time format
This commit is contained in:
parent
681dca7c85
commit
117cf454e6
54
src/main.py
54
src/main.py
@ -2,7 +2,13 @@
|
||||
use os package to iterate through files in a directory
|
||||
"""
|
||||
import os
|
||||
import time
|
||||
# import time
|
||||
import json
|
||||
import base64
|
||||
import datetime as dt
|
||||
|
||||
with open('icons.json', encoding="utf-8") as json_file:
|
||||
data = json.load(json_file)
|
||||
|
||||
|
||||
def main():
|
||||
@ -17,15 +23,16 @@ def main():
|
||||
with open(os.path.join(dirname, 'index.html'), 'w', encoding="utf-8") as f:
|
||||
f.write("\n".join([
|
||||
get_template_head(dirname),
|
||||
"<tr><td><a href=\"../\">../</a></td><td>-</td><td>-</td></tr>" if dirname != "." else "",
|
||||
]))
|
||||
"<tr class=\"w-2/4 bg-white border-b hover:bg-gray-50\"><th scope=\"row\" class=\" py-2 px-2 lg:px-6 font-medium text-gray-900 whitespace-nowrap flex align-middle\"><img style=\"max-width:23px; margin-right:5px\" src=\"" + get_icon_base64("o.folder-home") + "\"/>" +
|
||||
"<a class=\"my-auto text-blue-700\" href=\"../\">../</a></th><td>-</td><td>-</td></tr>" if dirname != "." else "",
|
||||
]))
|
||||
for subdirname in dirnames:
|
||||
f.write("<tr><td><a href=\"" + subdirname + "/\">" +
|
||||
subdirname + "/</a></td><td>-</td><td>-</td></tr>\n")
|
||||
f.write("<tr class=\"w-1/4 bg-white border-b hover:bg-gray-50\"><th scope=\"row\" class=\" py-2 px-2 lg:px-6 font-medium text-gray-900 whitespace-nowrap flex align-middle\"><img style=\"max-width:23px; margin-right:5px\" src=\"" + get_icon_base64("o.folder") + "\"/>" + "<a class=\"my-auto text-blue-700\" href=\"" + subdirname + "/\">" +
|
||||
subdirname + "/</a></th><td>-</td><td>-</td></tr>\n")
|
||||
for filename in filenames:
|
||||
path = (dirname == '.' and filename or dirname +
|
||||
'/' + filename)
|
||||
f.write("<tr><td><a href=\"" + filename + "\">" + filename + "</a></td><td>" +
|
||||
f.write("<tr class=\"w-1/4 bg-white border-b hover:bg-gray-50\"><th scope=\"row\" class=\" py-2 px-2 lg:px-6 font-medium text-gray-900 whitespace-nowrap flex align-middle\"><img style=\"max-width:23px; margin-right:5px\" src=\"" + get_icon_base64(filename) + "\"/>" + "<a class=\"my-auto text-blue-700\" href=\"" + filename + "\">" + filename + "</a></th><td>" +
|
||||
get_file_size(path) + "</td><td>" + get_file_modified_time(path) + "</td></tr>\n")
|
||||
|
||||
f.write("\n".join([
|
||||
@ -53,7 +60,8 @@ def get_file_modified_time(filepath):
|
||||
"""
|
||||
get file modified time
|
||||
"""
|
||||
return time.ctime(os.path.getmtime(filepath))
|
||||
return dt.datetime.fromtimestamp(os.path.getmtime(filepath)).strftime('%Y-%m-%d %H:%M:%S')
|
||||
# return time.ctime(os.path.getmtime(filepath)).strftime('%X %x')
|
||||
|
||||
|
||||
def get_template_head(foldername):
|
||||
@ -75,8 +83,40 @@ def get_template_foot():
|
||||
return foot
|
||||
|
||||
|
||||
def get_icon_url(filename):
|
||||
"""
|
||||
get icon url
|
||||
"""
|
||||
return "/png/" + get_icon_from_filename(filename)
|
||||
|
||||
|
||||
def get_icon_base64(filename):
|
||||
"""
|
||||
get icon base64
|
||||
"""
|
||||
with open("png/" + get_icon_from_filename(filename), "rb") as file:
|
||||
return "data:image/png;base64, " + base64.b64encode(file.read()).decode('ascii')
|
||||
|
||||
|
||||
def get_icon_from_filename(filename):
|
||||
"""
|
||||
get icon from filename
|
||||
"""
|
||||
extension = "." + filename.split(".")[-1]
|
||||
# extension = "." + extension
|
||||
# print(extension)
|
||||
for i in data:
|
||||
if extension in i["extension"]:
|
||||
# print(i["icon"])
|
||||
return i["icon"] + ".png"
|
||||
# print("no icon found")
|
||||
return "unknown.png"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
# get_icon_from_filename("test.txppt")
|
||||
|
||||
|
||||
# for subdirname in dirnames:
|
||||
# print("FOLDER" + os.path.join(dirname, subdirname))
|
||||
|
Loading…
Reference in New Issue
Block a user