File size: 1,503 Bytes
e430a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d342b4
 
e430a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d342b4
e430a81
1d342b4
 
 
e430a81
 
 
 
 
 
 
 
 
 
 
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
""" 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()