Repository navigation
Expand file tree
/
Copy pathreverseParser.py
More file actions
59 lines (46 loc) · 2.41 KB
/
Copy pathreverseParser.py
File metadata and controls
59 lines (46 loc) · 2.41 KB
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
import csv
import json
import random
import argparse
import sys
from pathlib import Path
def generate_priority_json(input_csv, output_json):
animals = ['"title": "Zu\\u00f1i Fetiches / Second Annual Report of the Bureau of Ethnology to the Secretary of the Smithsonian Institution, 1880-1881, Government Printing Office, Washington, 1883, pages 3-45",\n "creator": "Frank Hamilton Cushing",\n "language": "en",\n "identifier": "http://www.gutenberg.org/20067",\n "date": "2006-12-09"' '"title": "Legends of Texas",\n "language": "en",\n "identifier": "http://www.gutenberg.org/70859",\n "date": "2023-05-25"']
data_map = {}
try:
csv_path = Path(input_csv)
with open(csv_path, mode='r', encoding='utf-8') as f:
# Use csv.reader instead of DictReader for files without headers
reader = csv.reader(f)
for row in reader:
# Skip empty rows
if not row:
continue
# Assumes the structure: [orig_name, decrypt_key, file_id]
# file_id is at index 2 (the 3rd column)
try:
f_id = row[2]
data_map[f_id] = {
"priority": random.randint(1, 3),
"metadata": random.choice(animals)
}
except IndexError:
print(f"⚠️ Warning: Skipping a row that doesn't have 3 columns: {row}")
# Ensure output directory exists
json_path = Path(output_json)
json_path.parent.mkdir(parents=True, exist_ok=True)
with open(json_path, mode='w', encoding='utf-8') as f:
json.dump(data_map, f, indent=4)
print(f"✅ Success! Processed {len(data_map)} entries into {output_json}")
except FileNotFoundError:
print(f"❌ Error: The file '{input_csv}' was not found.")
except Exception as e:
print(f"❌ An unexpected error occurred: {e}")
def main():
parser = argparse.ArgumentParser(description="CLI tool for headerless CSV to JSON conversion.")
parser.add_argument("input", help="Path to headerless CSV (orig_name, key, file_id)")
parser.add_argument("output", help="Path to output JSON")
args = parser.parse_args()
generate_priority_json(args.input, args.output)
if __name__ == "__main__":
main()