forked from amnh-sciviz/collectionscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_metadata.py
122 lines (101 loc) · 3.5 KB
/
prepare_metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
import argparse
import math
import os
from pprint import pprint
import sys
import lib.io_utils as io
import lib.item_utils as tu
import lib.list_utils as lu
import lib.math_utils as mu
# input
parser = argparse.ArgumentParser()
parser.add_argument("-config", dest="CONFIG_FILE", default="config-sample.yml", help="Config file")
parser.add_argument("-per", dest="ITEMS_PER_FILE", default=-1, type=int, help="Number of items per file; -1 for automatic")
a = parser.parse_args()
config = tu.loadConfig(a.CONFIG_FILE)
itemFields = config["itemFields"] if "itemFields" in config else None
if itemFields is None:
print(f'No item metadata fields are set in config. Skipping...')
sys.exit()
OUTPUT_DIR = f'apps/{config["name"]}/'
OUTPUT_FILE_REL = "data/metadata/"
OUTPUT_FILE = OUTPUT_DIR + OUTPUT_FILE_REL
CONFIG_FILE = OUTPUT_DIR + "js/config/config.metadata.js"
# Make sure output dirs exist
io.makeDirectories([OUTPUT_FILE, CONFIG_FILE])
io.removeFiles(OUTPUT_FILE + "*.json")
items, categories = tu.getItems(config)
itemCount = len(items)
# Add item slides from stories to item metadata
storyItems = {}
if "stories" in config:
stories = config["stories"]
for key, story in stories.items():
for slide in story["slides"]:
if "itemId" in slide:
slideMeta = slide.copy()
slideMeta.pop("itemId", None)
storyItems[str(slide["itemId"])] = slideMeta
itemsPerFile = a.ITEMS_PER_FILE
if itemsPerFile < 1:
targetFiles = 1000
itemsPerFile = mu.roundInt(1.0 * itemCount / targetFiles)
itemsPerFile = min(itemsPerFile, 1000)
totalFiles = mu.ceilInt(1.0 * itemCount / itemsPerFile)
print(f'{itemsPerFile} per file with a total of {totalFiles} files')
fileItems = []
fileIndex = 0
for i, item in enumerate(items):
itemOut = []
for field in itemFields:
if field["column"] not in item:
continue
value = item[field["column"]]
if str(value).strip() == "":
continue
fieldOut = field.copy()
fieldOut["value"] = value
fieldOut.pop("column", None)
itemOut.append(fieldOut)
# Add story metadata if it exists
if item["_id"] in storyItems:
storyItem = storyItems[item["_id"]]
if "html" in storyItem:
itemOut.append({
"isHTML": True,
"isDescription": True,
"value": storyItem["html"]
})
if "text" in storyItem:
itemOut.append({
"isHTML": True,
"isDescription": True,
"value": "<p>" + storyItem["text"] + "</p>"
})
if "audio" in storyItem:
itemOut.append({
"isAudio": True,
"value": storyItem["audio"]
})
if "object" in storyItem:
itemOut.append({
"isObject": True,
"value": storyItem["object"]
})
print(f'Story item: {OUTPUT_FILE}{fileIndex}.json <{len(fileItems)}>')
fileItems.append(itemOut)
if len(fileItems) >= itemsPerFile or i >= (itemCount-1):
filenameOut = f'{OUTPUT_FILE}{fileIndex}.json'
io.writeJSON(filenameOut, fileItems)
fileIndex += 1
fileItems = []
mu.printProgress(i+1, itemCount)
outjson = {
"metadata": {
"itemCount": itemCount,
"itemsPerFile": itemsPerFile,
"itemsPath": OUTPUT_FILE_REL
}
}
io.writeJSON(CONFIG_FILE, outjson, pretty=True, prepend="_.extend(CONFIG, ", append=");")