ccel-paragraphs / prune-nonfree.py
jncraton's picture
Adjust logging
1d342b4
""" Prune non-free ThML files """
import re
import os
import xml.etree.ElementTree as ET
from pathlib import Path
def get_field(root, field):
try:
text = root.find(f".//{field}").text
text = re.sub(r"\s+", " ", text)
except:
text = None
return text
def prune():
log = open("removed.txt", "a")
global count
for filename in list(Path(".").rglob("*.xml")):
filename = str(filename)
if "authInfo." in filename:
continue
try:
tree = ET.parse(filename)
except:
print("ERROR: Unable to parse:", filename)
continue
root = tree.getroot()
# Skip content with copyright restrictions
# The copyright statements aren’t consistent.
# The ones that contain the text "public domain" (case insensitive)
# or nothing at all should be safe.
rights = get_field(root, "DC.Rights")
if rights and "public domain" not in rights.lower():
os.unlink(filename)
print(f"Removed {filename} due to copyright: {rights}")
log.write(f"Removed {filename} due to copyright\n")
log.close()
if __name__ == "__main__":
print(
"This script will permanently delete any xml files with non-free DC.Rights under the current directory."
)
confirm = input("Type `delete` to continue: ")
if confirm != "delete":
print("Exiting without modification")
exit(1)
prune()