| import pandas as pd |
| from pathlib import Path |
| import argparse |
| import sys |
|
|
| |
| |
| |
| MANIFEST_PATH = "data/eol-cargo-archive_catalog_combined-manifest-checksums_links.csv" |
| OWNER_PATH = "data/media_manifest_missing_licenses_jul26_owners.csv" |
|
|
|
|
| def get_owners_titles(manifest, owners): |
| """ |
| Function to attach owner names and image titles to manifest entries in combined manifest. |
| Fills empty "Copyright Owner" and "Title" values with "not provided". |
| |
| Parameters: |
| ----------- |
| manifest - DataFrame composed of manifest entries aligned on MD5s from three different manifests, |
| includes columns "md5_combined_manifest", "combined_id_full_manifest". |
| owners - DataFrame of July 26, 2023 media manifest entries that had missing owners, updated with their information. |
| |
| Returns: |
| -------- |
| full_owner_manifest - Media manifest DataFrame with missing owners resolved. |
| num_matched - Number of records matched with the owner fix file. |
| """ |
|
|
| |
| owners_manifest = pd.merge(owners, |
| manifest, |
| left_on="combined_id_owner", |
| right_on = "combined_id_full_manifest", |
| how = "inner") |
| |
| |
| owners_manifest["copyright_owner"] = owners_manifest["Copyright Owner"] |
|
|
| |
| license_cols = list(manifest.columns) |
| license_cols.append("title") |
| owners_manifest = owners_manifest[license_cols] |
|
|
| |
| rematched_owner_ids = list(owners_manifest.combined_id_full_manifest.unique()) |
| num_matched = len(rematched_owner_ids) |
|
|
| |
| un_matched_owner_manifest = manifest.loc[~manifest.combined_id_full_manifest.isin(rematched_owner_ids)] |
| un_matched_owner_manifest["title"] = "not provided" |
|
|
| |
| full_owner_manifest = pd.concat([un_matched_owner_manifest, owners_manifest], ignore_index=True) |
|
|
| |
| full_owner_manifest["copyright_owner"].fillna("not provided", inplace=True) |
| full_owner_manifest["title"].fillna("not provided", inplace=True) |
|
|
| return full_owner_manifest, num_matched |
|
|
|
|
| def update_owners(manifest, owners, filepath): |
| """ |
| Function to fetch and attach the missing owner and title information to EOL catalog entries and save catalog-media file. |
| |
| Parameters: |
| ----------- |
| manifest - DataFrame composed of manifest entries aligned on MD5s from three different manifests, |
| includes columns "md5_combined_manifest", "combined_id_full_manifest". |
| owners - DataFrame of July 26, 2023 media manifest entries that had missing owners, updated with their information. |
| """ |
| |
| |
| owners["combined_id_owner"] = owners["EOL content ID"].astype(str) + "_" + owners["EOL page ID"].astype(str) |
|
|
| updated_manifest, num_matched = get_owners_titles(manifest, owners) |
|
|
| |
| updated_manifest.to_csv(filepath, index=False) |
|
|
| |
| print(f"Of the {manifest.shape[0]} entries in manifest, {num_matched} were matched to the owner fix file.") |
| print( |
| "Licenses still missing Copyright Owners: \n", |
| manifest.loc[ |
| manifest["copyright_owner"].isna(), "license_name" |
| ].value_counts(), |
| ) |
|
|
|
|
| def main(dest_dir): |
| |
| try: |
| |
| print("Reading combined media manifest CSV") |
| manifest = pd.read_csv( |
| MANIFEST_PATH, |
| low_memory=False, |
| ) |
| print("Reading owner fix CSV") |
| owners = pd.read_csv( |
| OWNER_PATH, |
| dtype={"EOL content ID": "int64", "EOL page ID": "int64"}, |
| low_memory=False, |
| ) |
| except Exception as e: |
| sys.exit(e) |
|
|
| |
| dest_dir_path = Path(dest_dir) |
| if not dest_dir_path.is_absolute(): |
| |
| base_path = Path(__file__).parent.parent.resolve() |
| filepath = base_path / dest_dir_path |
| else: |
| filepath = dest_dir_path |
|
|
| filepath.mkdir(parents=True, exist_ok=True) |
|
|
| |
| update_owners(manifest, owners, str(filepath / "combined_manifest.csv")) |
|
|
|
|
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser(description="Attach missing owner info to combined manifest") |
|
|
| parser.add_argument( |
| "--output_path", |
| default="data", |
| required=False, |
| help="Path to the folder for output combined manifest file", |
| ) |
| args = parser.parse_args() |
|
|
| main(args.output_path) |
|
|