-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscript_checker.py
More file actions
executable file
·349 lines (291 loc) · 11.8 KB
/
transcript_checker.py
File metadata and controls
executable file
·349 lines (291 loc) · 11.8 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python3
"""
Transcript JSON File Validator
This script checks all transcript JSON files for validity and structural integrity.
It displays progress with an overwriting status line and lists any problematic files.
Author: AI Assistant
Purpose: Ensure data integrity for transcript JSON files after metadata enhancements
"""
import os
import json
import glob
import sys
import argparse
from pathlib import Path
import yaml
def get_transcripts_directory():
"""
Get the transcripts directory from the configuration.
Returns:
str: Path to the transcripts directory
"""
# Try to load from the config file first
config_dir = os.path.join(os.path.expanduser("~"), '.config', 'ref')
config_file = os.path.join(config_dir, 'config.yaml')
if os.path.exists(config_file):
try:
with open(config_file, 'r') as f:
config = yaml.safe_load(f)
return os.path.expanduser(config['paths']['transcripts'])
except Exception as e:
print(f"Warning: Could not load config file: {e}")
# Fallback to default path
return os.path.expanduser("~/references/transcripts")
def clear_status_line():
"""Clear the current status line."""
print("\r" + " " * 80 + "\r", end="", flush=True)
def print_status(message):
"""Print a status message that can be overwritten."""
clear_status_line()
print(f"\r{message}", end="", flush=True)
def validate_json_structure(data, filename):
"""
Validate the expected structure of a transcript JSON file.
Args:
data (dict): The parsed JSON data
filename (str): The filename for error reporting
Returns:
list: List of validation errors (empty if valid)
"""
errors = []
# Check for required top-level keys
required_keys = ['transcript', 'duration', 'comments', 'metadata']
for key in required_keys:
if key not in data:
errors.append(f"Missing required key: '{key}'")
# Check transcript field
if 'transcript' in data:
if not isinstance(data['transcript'], str):
errors.append("'transcript' should be a string")
elif not data['transcript'].strip():
errors.append("'transcript' is empty")
# Check duration field
if 'duration' in data:
if not isinstance(data['duration'], (int, float)):
errors.append("'duration' should be a number")
elif data['duration'] < 0:
errors.append("'duration' should be non-negative")
# Check comments field
if 'comments' in data:
if not isinstance(data['comments'], list):
errors.append("'comments' should be a list")
# Check metadata structure
if 'metadata' in data:
if not isinstance(data['metadata'], dict):
errors.append("'metadata' should be a dictionary")
else:
metadata = data['metadata']
required_metadata_keys = ['id', 'title', 'channel', 'published_at']
for key in required_metadata_keys:
if key not in metadata:
errors.append(f"Missing metadata key: '{key}'")
elif not isinstance(metadata[key], str):
errors.append(f"Metadata '{key}' should be a string")
return errors
def check_transcript_files(transcripts_dir=None, pattern="*.json", verbose=False, quiet=False):
"""
Check all transcript JSON files for validity and structure.
Args:
transcripts_dir (str, optional): Directory to check. If None, uses config default.
pattern (str): File pattern to match. Defaults to "*.json".
verbose (bool): Enable verbose output.
quiet (bool): Suppress progress messages.
Returns:
tuple: (total_files, valid_files, invalid_files_list)
"""
if transcripts_dir is None:
transcripts_dir = get_transcripts_directory()
if not os.path.exists(transcripts_dir):
print(f"Error: Transcripts directory does not exist: {transcripts_dir}")
return 0, 0, []
# Find all JSON files in the transcripts directory
json_pattern = os.path.join(transcripts_dir, pattern)
json_files = glob.glob(json_pattern)
if not json_files:
if not quiet:
print(f"No files matching '{pattern}' found in: {transcripts_dir}")
return 0, 0, []
if not quiet:
print(f"Found {len(json_files)} files matching '{pattern}' in: {transcripts_dir}")
if not verbose:
print(f"Checking transcript files...\n")
valid_files = 0
invalid_files = []
for i, json_file in enumerate(json_files, 1):
filename = os.path.basename(json_file)
if verbose:
print(f"Checking [{i}/{len(json_files)}]: {filename}")
elif not quiet:
print_status(f"Checking [{i}/{len(json_files)}]: {filename}")
try:
# Try to load and parse the JSON file
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# Validate the structure
validation_errors = validate_json_structure(data, filename)
if validation_errors:
invalid_files.append({
'file': filename,
'path': json_file,
'errors': validation_errors,
'type': 'structure'
})
if verbose:
print(f" ⚠️ Structure issues: {len(validation_errors)} errors")
else:
valid_files += 1
if verbose:
print(f" ✅ Valid")
except json.JSONDecodeError as e:
invalid_files.append({
'file': filename,
'path': json_file,
'errors': [f"JSON decode error: {str(e)}"],
'type': 'json'
})
if verbose:
print(f" ❌ JSON error: {str(e)}")
except Exception as e:
invalid_files.append({
'file': filename,
'path': json_file,
'errors': [f"File read error: {str(e)}"],
'type': 'file'
})
if verbose:
print(f" 🔧 File error: {str(e)}")
if not quiet and not verbose:
clear_status_line()
return len(json_files), valid_files, invalid_files
def parse_arguments():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="Validate transcript JSON files for structure and syntax.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s # Check all JSON files (default)
%(prog)s --verbose # Show detailed progress for each file
%(prog)s --quiet # Only show summary results
%(prog)s --directory /path/to/dir # Check specific directory
%(prog)s --pattern "*.json" # Check files matching pattern
%(prog)s --output json # Output results in JSON format
"""
)
parser.add_argument(
"-d", "--directory",
help="Directory containing transcript files (default: from config)"
)
parser.add_argument(
"-p", "--pattern",
default="*.json",
help="File pattern to match (default: *.json)"
)
parser.add_argument(
"-v", "--verbose",
action="store_true",
help="Show detailed progress for each file"
)
parser.add_argument(
"-q", "--quiet",
action="store_true",
help="Only show summary results"
)
parser.add_argument(
"-o", "--output",
choices=["text", "json"],
default="text",
help="Output format (default: text)"
)
parser.add_argument(
"--list-only",
action="store_true",
help="Only list problematic files, don't show detailed errors"
)
return parser.parse_args()
def output_results_json(total_files, valid_files, invalid_files):
"""Output results in JSON format."""
result = {
"summary": {
"total_files": total_files,
"valid_files": valid_files,
"invalid_files": len(invalid_files)
},
"problematic_files": invalid_files
}
print(json.dumps(result, indent=2))
def main():
"""Main function to run the transcript file checker."""
args = parse_arguments()
# Handle conflicting options
if args.verbose and args.quiet:
print("Error: --verbose and --quiet cannot be used together")
sys.exit(1)
if not args.quiet:
print("=" * 60)
print("TRANSCRIPT JSON FILE VALIDATOR")
print("=" * 60)
total_files, valid_files, invalid_files = check_transcript_files(
transcripts_dir=args.directory,
pattern=args.pattern,
verbose=args.verbose,
quiet=args.quiet
)
if total_files == 0:
return
# Handle different output formats
if args.output == "json":
output_results_json(total_files, valid_files, invalid_files)
sys.exit(1 if invalid_files else 0)
# Text output format (default)
if not args.quiet:
print(f"\nValidation Summary:")
print(f"==================")
print(f"Total files checked: {total_files}")
print(f"Valid files: {valid_files}")
print(f"Invalid files: {len(invalid_files)}")
if invalid_files:
if not args.quiet:
if args.list_only:
print(f"\nProblematic Files:")
print(f"==================")
for file_info in invalid_files:
print(f" • {file_info['file']} ({file_info['type']})")
else:
print(f"\nProblematic Files:")
print(f"==================")
# Group by error type
json_errors = [f for f in invalid_files if f['type'] == 'json']
structure_errors = [f for f in invalid_files if f['type'] == 'structure']
file_errors = [f for f in invalid_files if f['type'] == 'file']
if json_errors:
print(f"\n❌ JSON Parse Errors ({len(json_errors)} files):")
for file_info in json_errors:
print(f" • {file_info['file']}")
if not args.list_only:
for error in file_info['errors']:
print(f" - {error}")
if structure_errors:
print(f"\n⚠️ Structure Issues ({len(structure_errors)} files):")
for file_info in structure_errors:
print(f" • {file_info['file']}")
if not args.list_only:
for error in file_info['errors']:
print(f" - {error}")
if file_errors:
print(f"\n🔧 File Access Errors ({len(file_errors)} files):")
for file_info in file_errors:
print(f" • {file_info['file']}")
if not args.list_only:
for error in file_info['errors']:
print(f" - {error}")
if not args.list_only:
print(f"\nRecommendation: Review and fix the problematic files listed above.")
sys.exit(1)
else:
if not args.quiet:
print(f"\n✅ All transcript JSON files are valid!")
print(f" All {total_files} files passed validation checks.")
sys.exit(0)
if __name__ == "__main__":
main()