egrace479 commited on
Commit
d073923
·
1 Parent(s): 71ad5b4

Add script to generate licenses.csv file by adding license URLs to media catalog files.

Browse files
Files changed (1) hide show
  1. scripts/make_licenses.py +91 -0
scripts/make_licenses.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from pathlib import Path
3
+ import argparse
4
+ import sys
5
+
6
+ # Output of match_owners.py (catalog-media.csv) should be fed in.
7
+ # Will also work with the media manifest.
8
+
9
+ CC_URL = "https://creativecommons.org/"
10
+
11
+
12
+ def get_license_url(license_version):
13
+ """
14
+ Function to generate the appropriate Creative Commons URL for a given license.
15
+ All licenses in the media manifest are "cc-XX", a variation on "publicdomain", or "No known copyright restrictions".
16
+
17
+ Parameters:
18
+ -----------
19
+ license_version - String. License (eg., "cc-by-nc 3.0").
20
+
21
+ Returns:
22
+ --------
23
+ license_url - String. Creative Commons URL associated with the license_version.
24
+
25
+ """
26
+ # First check for version number and isolate it
27
+ if "." in license_version:
28
+ version = license_version.split(sep="-")[-1]
29
+ license_name = license_version.split(sep="-" + version)[0]
30
+ else:
31
+ # No version specified, so default to latest version (4.0)
32
+ license_name = license_version
33
+ version = "4.0"
34
+ # Check which type of license
35
+ if license_name[:5] == "cc-by":
36
+ by_x = license_name.split(sep="cc-")[1]
37
+ license_url = CC_URL + "licenses/" + by_x + "/" + version
38
+ elif (license_name[:4] == "cc-0") or ("public" in license_name):
39
+ license_url = CC_URL + "publicdomain/zero/1.0"
40
+ else:
41
+ # "No known copyright restrictions"
42
+ license_url = None
43
+ return license_url
44
+
45
+
46
+ def main(src_csv, dest_dir):
47
+ # Check CSV compatibility
48
+ try:
49
+ print("Reading CSV")
50
+ df = pd.read_csv(src_csv, low_memory=False)
51
+ except Exception as e:
52
+ sys.exit(e)
53
+
54
+ # Check for "License Name" column
55
+ print("Processing data")
56
+ if "License Name" not in list(df.columns):
57
+ sys.exit("Source CSV does not have a column labeled License Name.")
58
+
59
+ # Check filepath given by user
60
+ dest_dir_path = Path(dest_dir)
61
+ if not dest_dir_path.is_absolute():
62
+ # Use ToL-EDA as reference folder
63
+ base_path = Path(__file__).parent.parent.resolve()
64
+ filepath = base_path / dest_dir_path
65
+ else:
66
+ filepath = dest_dir_path
67
+
68
+ filepath.mkdir(parents=True, exist_ok=True)
69
+
70
+ # Add links for licenses to catalog manifest
71
+ print("Getting URLs for licenses")
72
+ df["license_link"] = df["License Name"].apply(get_license_url)
73
+
74
+ # Save license file
75
+ df.to_csv(str(filepath / "licenses.csv"), index=False)
76
+
77
+
78
+ if __name__ == "__main__":
79
+ parser = argparse.ArgumentParser(
80
+ description="Add license URLs to catalog-media file"
81
+ )
82
+ parser.add_argument("input_file", help="Path to the input CSV file")
83
+ parser.add_argument(
84
+ "--output_path",
85
+ default="data",
86
+ required=False,
87
+ help="Path to the folder for output license file",
88
+ )
89
+ args = parser.parse_args()
90
+
91
+ main(args.input_file, args.output_path)