Merge remote-tracking branch 'origin/feature/index'

This commit is contained in:
Hyunchul Kim 2020-11-16 12:09:53 +09:00
commit 970a40c6f6
20 changed files with 535 additions and 10 deletions

33
.gitignore vendored
View file

@ -2,3 +2,36 @@ index.html
.vscode/ .vscode/
.python-version .python-version
*.ai *.ai
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
/.firebase/
/.firebaserc
/.idea/
/venv/
firebase.json

View file

@ -1,6 +1,7 @@
import base64 import base64
import os import os
import subprocess import subprocess
from io import BytesIO from io import BytesIO
import magic import magic
@ -9,6 +10,9 @@ import markdown
from distribusi.page_template import html_footer, html_head from distribusi.page_template import html_footer, html_head
from distribusi.mappings import CODE_TYPES, FILE_TYPES, SUB_TYPES from distribusi.mappings import CODE_TYPES, FILE_TYPES, SUB_TYPES
from distribusi import fregments
MIME_TYPE = magic.Magic(mime=True) MIME_TYPE = magic.Magic(mime=True)
@ -56,8 +60,7 @@ def thumbnail(image, name, args):
return "<figure><a href='{}'><img src='{}'></a><figcaption>{}</figcaption></figure>".format(name, name, name) return "<figure><a href='{}'><img src='{}'></a><figcaption>{}</figcaption></figure>".format(name, name, name)
def div(args, type_, subtype, tag, name): def div(args, type_, subtype, tag, name, id):
id_name = name.split('.')[0].replace(' ', '_')
if args.no_filenames: if args.no_filenames:
filename = '' filename = ''
else: else:
@ -71,8 +74,7 @@ def div(args, type_, subtype, tag, name):
html = '<div id="{}" class="{}">{}</div>' html = '<div id="{}" class="{}">{}</div>'
else: else:
html = '<div id="{}" class="{}">{}' + filename + '</div>' html = '<div id="{}" class="{}">{}' + filename + '</div>'
return html.format(id, subtype, tag)
return html.format(id_name, subtype, tag)
def check_distribusi_index(args, index): def check_distribusi_index(args, index):
@ -110,7 +112,7 @@ def write_index(args,index, html, html_head, html_footer):
f.write(html_footer) f.write(html_footer)
def distribusify(args, directory): # noqa def distribusify(args, directory, freg): # noqa
for root, dirs, files in os.walk(directory): for root, dirs, files in os.walk(directory):
if args.exclude_directory: if args.exclude_directory:
@ -125,6 +127,14 @@ def distribusify(args, directory): # noqa
dirs.sort() dirs.sort()
files.sort() files.sort()
#
# fregments index
# 작가 폴더 내인 경우 아티스트명 저장
#
path = root.split('/')
if len(path) == 3:
artist = path[2].strip()
if not args.remove_index: if not args.remove_index:
html = [] html = []
@ -132,7 +142,6 @@ def distribusify(args, directory): # noqa
print('Generating directory listing for', root) print('Generating directory listing for', root)
for name in sorted(files): for name in sorted(files):
if 'index.html' not in name: if 'index.html' not in name:
full_path = os.path.join(root, name) full_path = os.path.join(root, name)
mime = MIME_TYPE.from_file(full_path) mime = MIME_TYPE.from_file(full_path)
@ -187,8 +196,10 @@ def distribusify(args, directory): # noqa
subtype = subtype + ' unkown-file' subtype = subtype + ' unkown-file'
a = a.replace('{}', name) a = a.replace('{}', name)
if len(path) == 3 and artist:
id = freg.get_index(artist, name)
html.append(div(args, type_, subtype, a, name, id))
html.append(div(args, type_, subtype, a, name))
if root != directory: if root != directory:
if args.menu_with_index: if args.menu_with_index:
@ -197,12 +208,15 @@ def distribusify(args, directory): # noqa
html.append('<a href="../">../</a>') html.append('<a href="../">../</a>')
for name in dirs: for name in dirs:
'''
if args.menu_with_index: if args.menu_with_index:
a = "<a href='{}/index.html'>{}</a>".replace('{}', name) a = "<a href='{}/index.html'>{}</a>".replace('{}', name)
else: else:
a = "<a href='{}'>{}/</a>".replace('{}', name) a = "<a href='{}'>{}/</a>".replace('{}', name)
html.insert(0, div(args, 'dir', 'dir', a, 'folder')) html.insert(0, div(args, 'dir', 'dir', a, 'folder'))
'''
index = os.path.join(root, 'index.html') index = os.path.join(root, 'index.html')
if os.path.exists(index): if os.path.exists(index):
@ -221,3 +235,19 @@ def distribusify(args, directory): # noqa
os.remove(index) os.remove(index)
except Exception as e: except Exception as e:
print(e) print(e)
def build_index(args, directory, freg):
#
# fregments index
# 임시 데이터 저장
#
print("--------- Build main index --------------")
html = []
freg_data = freg.get_fregments()
for f in freg_data:
index = "{}".format(f.index)
url = "/{}/#{} ".format(f.artist, index.zfill(4))
label = "{} 번째 조각".format(f.index)
html.append('<a href="{}">{}</a><br/>'.format(url, label))
index = os.path.join(directory, 'index.html')
write_index(args, index, html, html_head, html_footer)

View file

@ -0,0 +1,187 @@
import os
import platform
import json
from operator import itemgetter
class Fregment:
def __init__(self, index, update, directory, artist, file):
self.index = index
self.update = update
self.directory = directory
self.artist = artist
self.file = file
def __repr__(self):
return repr((self.index, self.update, self.directory, self.artist, self.file))
class Fregments:
def __init__(self):
self.index = {}
self.indextable = []
self.timetable = []
self.config_file = 'config.json'
self.index_file = 'fregments_index.json'
with open(self.index_file) as json_file:
self.json_data = json.load(json_file)
self.temp_data = {"fregments":[]}
self.count = len(self.json_data)
def creation_date(self, path_to_file):
"""
Try to get the date that a file was created, falling back to when it was
last modified if that isn't possible.
See http://stackoverflow.com/a/39501288/1709587 for explanation.
"""
if platform.system() == 'Windows':
return os.path.getctime(path_to_file)
else:
stat = os.stat(path_to_file)
try:
return stat.st_mtime
except AttributeError:
return stat.st_birthtime
def occupancy(self, directory, file):
f = file + ".meta"
meta_path = os.path.join(directory, f)
with open(meta_path) as json_file:
meta = json.load(json_file)
occupation = meta["occupation"]
if occupation > -1:
origin_path = os.path.join(directory, file)
date = self.creation_date(origin_path)
arr = directory.split("/")
if arr.__len__() == 2:
artist = arr[1]
else:
artist = arr[2]
self.index[occupation] = Fregment(occupation, date, directory, artist, file)
def is_meta(self, file):
fa = file.split(".")
i = len(fa)-1
if i>0 and fa[i] == "meta":
return True
else:
return False
def has_meta(self, directory, file):
f = file+".meta"
meta_path = os.path.join(directory, f)
if os.path.isfile(meta_path):
return True
else:
return False
def preindex(self, directory):
for root, dirs, files in os.walk(directory):
arr = root.split("/")
# 2뎁스까지만 인덱스 함.
if arr.__len__() < 4:
# files index
for f in files:
if self.is_meta(f):
pass
elif f == "index.html" or f == ".DS_Store":
pass
elif self.has_meta(root, f):
self.occupancy(root, f)
else:
self.add_timetable(root, f)
# dirs index
for d in dirs:
if self.has_meta(root, d):
self.occupancy(root, d)
else:
self.add_timetable(root, d)
def postindex(self):
self.timetable = sorted(self.timetable, key=lambda fregment: fregment.update)
print("----------- INDEXING ------------")
# indexing
for f in self.timetable:
f.index = self.get_lastindex()
self.index[f.index] = f
self.update_indextable()
self.save()
def update_indextable(self):
self.indextable = []
for f in self.index:
self.indextable.append(self.index[f])
self.indextable = sorted(self.indextable, key=lambda fregment: fregment.index)
def get_lastindex(self):
last = 0
self.update_indextable()
for f in self.indextable:
if f.index == last:
last = last + 1
return last
def add_timetable(self, directory, file):
path = os.path.join(directory, file)
date = self.creation_date(path)
arr = directory.split("/")
if arr.__len__() > 2:
artist = arr[2]
self.timetable.append(Fregment(-1, date, directory, artist, file))
'''
# [deprecated] preindex 하기 전 소소
def add(self, artist, fregment):
temp = {
"index" : 0,
"update" : 0,
"file" : {
"artist": artist,
"fregment": fregment
}
}
added = False
for f in self.json_data['fregments']:
# 기존 조각과 비교
if f['file'] == temp['file']:
added = True
if added:
print("Already added - artist:", artist, ", fregment: ", fregment)
else:
self.count = self.count + 1
print("Add fregment - artist:", artist, ", fregment: ", fregment)
temp["index"] = self.count
temp["update"] = int(time.time())
self.temp_data['fregments'].append(temp)
'''
def save(self):
print(json.dumps(self.indextable, cls=CustomEncoder))
with open(self.index_file, 'w') as outfile:
json.dump(self.indextable, outfile, indent=4, cls=CustomEncoder)
self.count = len(self.indextable)
def get_fregments(self):
return self.indextable
def get_count(self):
return self.count
def get_index(self, artist, name):
for f in self.indextable:
if f.artist == artist and f.file == name:
return ("{}".format(f.index)).zfill(4)
return -1;
class CustomEncoder(json.JSONEncoder):
def default(self, o):
return {'__{}__'.format(o.__class__.__name__): o.__dict__}
if __name__ == "__main__":
freg = Fregments()

View file

@ -1,7 +1,21 @@
# Added by Hyunchul # Added by Hyunchul
# 2020. 10. 26 # 2020. 10. 26
from distribusi.cli import build_argparser, distribusify from distribusi.cli import build_argparser, distribusify
from distribusi import fregments
from distribusi.distribusi import build_index
parser = build_argparser() parser = build_argparser()
args = parser.parse_args() args = parser.parse_args()
distribusify(args, args.directory)
event_path = './events'
data_path = './test_data'
freg = fregments.Fregments()
freg.preindex(event_path)
freg.preindex(data_path)
freg.postindex()
distribusify(args, event_path, freg)
distribusify(args, data_path, freg)
build_index(args, data_path, freg)

3
events/event_0000.meta Normal file
View file

@ -0,0 +1,3 @@
{
"occupation": 0
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

3
events/event_0001.meta Normal file
View file

@ -0,0 +1,3 @@
{
"occupation": 1
}

3
events/event_0008.meta Normal file
View file

@ -0,0 +1,3 @@
{
"occupation": 8
}

218
fregments_index.json Normal file
View file

@ -0,0 +1,218 @@
[
{
"__Fregment__": {
"index": 0,
"update": 1605171358.9636276,
"directory": "./events",
"artist": "events",
"file": "event_0000"
}
},
{
"__Fregment__": {
"index": 1,
"update": 1605171365.1618817,
"directory": "./events",
"artist": "events",
"file": "event_0001"
}
},
{
"__Fregment__": {
"index": 2,
"update": 1603870906.290785,
"directory": "./test_data/carrot",
"artist": "carrot",
"file": "\uc9c8\uc8fc.mp4"
}
},
{
"__Fregment__": {
"index": 3,
"update": 1604043931.1111157,
"directory": "./test_data/carrot",
"artist": "carrot",
"file": "RTF\ud14c\uc2a4\ud2b8.rtf"
}
},
{
"__Fregment__": {
"index": 4,
"update": 1604043931.1281314,
"directory": "./test_data/carrot",
"artist": "carrot",
"file": "\uafb8\ubb3c\uafb8\ubb3c.mov"
}
},
{
"__Fregment__": {
"index": 5,
"update": 1604043931.147918,
"directory": "./test_data/fig",
"artist": "fig",
"file": "20201022"
}
},
{
"__Fregment__": {
"index": 6,
"update": 1604043931.1488578,
"directory": "./test_data/fig",
"artist": "fig",
"file": "20201029"
}
},
{
"__Fregment__": {
"index": 7,
"update": 1604043931.238141,
"directory": "./test_data/grape",
"artist": "grape",
"file": "IMG_1334.png"
}
},
{
"__Fregment__": {
"index": 8,
"update": 1605171377.7292128,
"directory": "./events",
"artist": "events",
"file": "event_0008"
}
},
{
"__Fregment__": {
"index": 9,
"update": 1604043931.259867,
"directory": "./test_data/grape",
"artist": "grape",
"file": "IMG_1340.jpg"
}
},
{
"__Fregment__": {
"index": 10,
"update": 1604043931.3548143,
"directory": "./test_data/grape",
"artist": "grape",
"file": "IMG_1690.png"
}
},
{
"__Fregment__": {
"index": 11,
"update": 1604043931.4324255,
"directory": "./test_data/grape",
"artist": "grape",
"file": "IMG_1693.png"
}
},
{
"__Fregment__": {
"index": 12,
"update": 1604043931.4409137,
"directory": "./test_data/grape",
"artist": "grape",
"file": "sample.pdf"
}
},
{
"__Fregment__": {
"index": 13,
"update": 1604050831.3844233,
"directory": "./test_data/fig",
"artist": "fig",
"file": "test.txt"
}
},
{
"__Fregment__": {
"index": 14,
"update": 1604053581.7941525,
"directory": "./test_data/grape",
"artist": "grape",
"file": "fbdbdbf54d766dd86e5964de01ddc16b.jpg"
}
},
{
"__Fregment__": {
"index": 15,
"update": 1604054141.7061436,
"directory": "./test_data/carrot",
"artist": "carrot",
"file": "1030.txt"
}
},
{
"__Fregment__": {
"index": 16,
"update": 1604911149.3638797,
"directory": "./test_data/fig",
"artist": "fig",
"file": "test001.txt"
}
},
{
"__Fregment__": {
"index": 17,
"update": 1604912196.730964,
"directory": "./test_data/carrot",
"artist": "carrot",
"file": "test002.txt"
}
},
{
"__Fregment__": {
"index": 18,
"update": 1604912200.5855331,
"directory": "./test_data/carrot",
"artist": "carrot",
"file": "test_folder"
}
},
{
"__Fregment__": {
"index": 19,
"update": 1604915491.9132912,
"directory": "./test_data/fig",
"artist": "fig",
"file": "20201020"
}
},
{
"__Fregment__": {
"index": 20,
"update": 1604915593.2194371,
"directory": "./test_data/fig",
"artist": "fig",
"file": "fig002.txt"
}
},
{
"__Fregment__": {
"index": 21,
"update": 1605170712.9904313,
"directory": "./events/event_0000",
"artist": "event_0000",
"file": "124469821_121248626457919_479957910997498417_n.jpg"
}
},
{
"__Fregment__": {
"index": 22,
"update": 1605170736.042438,
"directory": "./events/event_0000",
"artist": "event_0000",
"file": "124157237_121248643124584_872058020963845448_n.jpg"
}
},
{
"__Fregment__": {
"index": 23,
"update": 1605170741.7326746,
"directory": "./events/event_0000",
"artist": "event_0000",
"file": "124476076_121248659791249_2342598949363915450_n.jpg"
}
}
]

2
test.sh Normal file → Executable file
View file

@ -15,5 +15,5 @@
# distribusi, use at own risk! # distribusi, use at own risk!
# --no-hidden Exclude hidden directories # --no-hidden Exclude hidden directories
# --menu-with-index Append index.html to menu items to aid navigation # --menu-with-index Append index.html to menu items to aid navigation
conda activate foh
python distribusi/test.py -d ./test_data/ python distribusi/test.py -d ./test_data/

33
test_data/404.html Normal file
View file

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Page Not Found</title>
<style media="screen">
body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
#message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px 16px; border-radius: 3px; }
#message h3 { color: #888; font-weight: normal; font-size: 16px; margin: 16px 0 12px; }
#message h2 { color: #ffa100; font-weight: bold; font-size: 16px; margin: 0 0 8px; }
#message h1 { font-size: 22px; font-weight: 300; color: rgba(0,0,0,0.6); margin: 0 0 16px;}
#message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
#message a { display: block; text-align: center; background: #039be5; text-transform: uppercase; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
#message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
#load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
@media (max-width: 600px) {
body, #message { margin-top: 0; background: white; box-shadow: none; }
body { border-top: 16px solid #ffa100; }
}
</style>
</head>
<body>
<div id="message">
<h2>404</h2>
<h1>Page Not Found</h1>
<p>The specified file was not found on this website. Please check the URL for mistakes and try again.</p>
<h3>Why am I seeing this?</h3>
<p>This page was generated by the Firebase Command-Line Interface. To modify it, edit the <code>404.html</code> file in your project's configured <code>public</code> directory.</p>
</div>
</body>
</html>

View file

View file

View file

@ -1,2 +1,3 @@
언론·출판에 대한 허가나 검열과 집회·결사에 대한 허가는 인정되지 아니한다. 모든 국민은 헌법과 법률이 정한 법관에 의하여 법률에 의한 재판을 받을 권리를 가진다. 언론·출판에 대한 허가나 검열과 집회·결사에 대한 허가는 인정되지 아니한다. 모든 국민은 헌법과 법률이 정한 법관에 의하여 법률에 의한 재판을 받을 권리를 가진다.
대통령은 내우·외환·천재·지변 또는 중대한 재정·경제상의 위기에 있어서 국가의 안전보장 또는 공공의 안녕질서를 유지하기 위하여 긴급한 조치가 필요하고 국회의 집회를 기다릴 여유가 없을 때에 한하여 최소한으로 필요한 재정·경제상의 처분을 하거나 이에 관하여 법률의 효력을 가지는 명령을 발할 수 있다. 대통령은 내우·외환·천재·지변 또는 중대한 재정·경제상의 위기에 있어서 국가의 안전보장 또는 공공의 안녕질서를 유지하기 위하여 긴급한 조치가 필요하고 국회의 집회를 기다릴 여유가 없을 때에 한하여 최소한으로 필요한 재정·경제상의 처분을 하거나 이에 관하여 법률의 효력을 가지는 명령을 발할 수 있다.
새로운 글 업데이트

0
test_data/fig/fig002.txt Normal file
View file

0
test_data/fig/test.txt Normal file
View file

View file

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB