op 2.0
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
import os
|
||||
import json
|
||||
|
||||
def load_config(file_path):
|
||||
"""Load the JSON config file and return a dictionary."""
|
||||
with open(file_path, 'r') as file:
|
||||
return json.load(file)
|
||||
|
||||
def json_to_env(json_file_path):
|
||||
with open(json_file_path, 'r') as json_file:
|
||||
config = json.load(json_file)
|
||||
for key, value in config.items():
|
||||
os.environ[key.upper()] = str(value)
|
||||
@@ -0,0 +1,72 @@
|
||||
import sys
|
||||
import os
|
||||
import shutil
|
||||
import xml.etree.ElementTree as ET
|
||||
import json
|
||||
|
||||
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
from XML_JSON.json_scripts import load_config
|
||||
from Chapters_List.extract_overdrive_chapters import extract_chapters
|
||||
from Chapters_List.chapter_ms import add_ms_to_chapters
|
||||
from XML_JSON.json_scripts import load_config
|
||||
from Chapters_List.ms_durations import ms_to_durations
|
||||
|
||||
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
config_path = os.path.join(parent_dir, 'config.json')
|
||||
|
||||
config = load_config(config_path)
|
||||
|
||||
root_dir = config['annex']
|
||||
|
||||
'''Takes the .odm.metadata file and turns it into metadata.xml'''
|
||||
def rename_xml(subdir, file):
|
||||
old_file = os.path.join(subdir, file)
|
||||
new_file = os.path.join(subdir, "metadata.xml")
|
||||
shutil.copy2(old_file, new_file)
|
||||
|
||||
def odm_to_xml(subdir, files):
|
||||
for file in files:
|
||||
if file.endswith('.odm.metadata'):
|
||||
rename_xml(subdir, file)
|
||||
break
|
||||
|
||||
'''Cleans the metadata'''
|
||||
def extract_metadata(xml_file):
|
||||
tree = ET.parse(xml_file)
|
||||
root = tree.getroot()
|
||||
data = {}
|
||||
for child in root:
|
||||
'''Finds occurrences of Title, Creators, Subjects and isolates'''
|
||||
if child.tag == "Title":
|
||||
data["Title"] = child.text
|
||||
elif child.tag == "Creators":
|
||||
'''Identifies Authors and Narrators'''
|
||||
for creator in child:
|
||||
if creator.attrib["role"] == "Author":
|
||||
data["Author"] = creator.text
|
||||
elif creator.attrib["role"] == "Narrator":
|
||||
data["Narrator"] = creator.text
|
||||
elif child.tag == "Subjects":
|
||||
subjects = []
|
||||
for subject in child:
|
||||
subjects.append(subject.text)
|
||||
data["Subjects"] = ", ".join(subjects)
|
||||
return data
|
||||
|
||||
'''Turns the cleaned xml into a json'''
|
||||
def xml_to_json(folder_path):
|
||||
for dirpath, _, filenames in os.walk(folder_path):
|
||||
for filename in filenames:
|
||||
if filename == "metadata.xml":
|
||||
xml_file = os.path.join(dirpath, filename)
|
||||
metadata = extract_metadata(xml_file)
|
||||
cleaned_file = os.path.join(dirpath, "cleaned_metadata.json")
|
||||
with open(cleaned_file, "w") as f:
|
||||
json.dump(metadata, f)
|
||||
|
||||
def parse_metadata():
|
||||
for subdir, _, files in os.walk(root_dir):
|
||||
odm_to_xml(subdir, files)
|
||||
xml_to_json(root_dir)
|
||||
Reference in New Issue
Block a user