Complete rewrite of filename parser to support new field order where Subject/Asset moved up and Country/Language moved down, plus new Social Media field. BREAKING CHANGE: V2.1 Structure (November 2025) Old (V1): [JOB]_[BRAND]_[COUNTRY]_[LANG]_[SUBJECT]_[ASSET]_[SPOT]_[DUR]_[RATIO]_[TRACKING] New (V2.1): [JOB]_[BRAND]_[SUBJECT]_[ASSET]_[DUR]_[RATIO]_[SPOT]_[COUNTRY]_[LANG]_[SOCIAL]_[TRACKING] Field Position Changes: - Subject Title: Position 5 → 3 (MOVED UP) - Asset Type: Position 6 → 4 (MOVED UP) - Duration: Position 8 → 5 (MOVED UP) - Aspect Ratio: Position 9 → 6 (MOVED UP) - Spot Version: 7 → 7 (SAME) - Country Code: Position 3 → 8 (MOVED DOWN) - Language Code: Position 4 → 9 (MOVED DOWN) - Social Media: NEW → Position 10 - Tracking ID: Position 10 → 11 New Social Media Field: - Field: social_media_version - Position: 10 (after language, before tracking) - Format: 3 uppercase letters - Codes: FBP, FBR, IGF, IGR (expandable) - Optional: Only present for social media assets Parse Algorithm Changes: - Positions 1-4 now: Job, Brand, Subject, Asset (fixed) - Positions 5-11: Pattern-based detection (flexible) - Duration detected by \d+S pattern - Aspect ratio detected by \d+x\d+ or contains 'x' - Spot detected by MST/REF - Country detected by 2 upper alpha (after ratio) - Language detected by 2-3 lower alpha (after country) - Social detected by known codes (after language) - Tracking ID detected by 6 alphanumeric + optional -N strip_upload_components() Updated: Now outputs: [BRAND]_[SUBJECT]_[ASSET]_[DUR]_[RATIO]_[SPOT]_[COUNTRY]_[LANG]_[SOCIAL] - Includes social media version if present - Still strips job number and tracking ID Testing: All 7 test cases from specification passed: ✅ All fields present ✅ Minimal (no duration/social/tracking) ✅ No duration ✅ No spot version ✅ With -N tracking (folder-only mode) ✅ No social media (most common) ✅ No tracking ID Example: Input: 1234567_RAF_TEST_OLV_6S_1x1_REF_GL_it_IGF_abc123.mp4 Parsed: brand=RAF, subject=TEST, country=GL, lang=it, social=IGF Clean: RAF_TEST_OLV_6S_1x1_REF_GL_it_IGF.mp4 Backward Compatibility: None - system not live yet, clean cutover to V2.1 format only. Backup: filename_parser_v1_backup.py contains old version for reference. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
265 lines
9.8 KiB
Python
265 lines
9.8 KiB
Python
"""
|
|
Filename Parser - V2.1 Naming Convention Parser
|
|
Updated November 2025 with new field positions
|
|
Compatible with Python 3.6+
|
|
"""
|
|
|
|
import re
|
|
import logging
|
|
|
|
logger = logging.getLogger('FilenameParser')
|
|
|
|
class FilenameParser:
|
|
"""
|
|
Parse V2.1 naming convention filenames:
|
|
[JOB]_[BRAND]_[SUBJECT]_[ASSET]_[DUR]_[RATIO]_[SPOT]_[COUNTRY]_[LANG]_[SOCIAL]_[TRACKING]
|
|
|
|
Example: 1234567_RAF_ME-MOMENT_OLV_6S_1x1_REF_GL_it_IGF_pOiJ9s
|
|
"""
|
|
|
|
# Known social media platform codes
|
|
SOCIAL_MEDIA_CODES = ['FBP', 'FBR', 'IGF', 'IGR'] # Expandable
|
|
|
|
def parse_filename(self, filename):
|
|
"""
|
|
Parse V2.1 filename into components
|
|
|
|
New Structure (V2.1):
|
|
[JOB]_[BRAND]_[SUBJECT]_[ASSET]_[DUR]_[RATIO]_[SPOT]_[COUNTRY]_[LANG]_[SOCIAL]_[TRACKING]
|
|
|
|
Args:
|
|
filename: Filename to parse (with or without extension)
|
|
|
|
Returns:
|
|
dict with parsed components and validation results
|
|
"""
|
|
validation_errors = []
|
|
warnings = []
|
|
|
|
# Remove extension
|
|
if '.' in filename:
|
|
filename_without_ext, extension = filename.rsplit('.', 1)
|
|
extension = '.' + extension
|
|
else:
|
|
filename_without_ext = filename
|
|
extension = ''
|
|
|
|
# Split by underscore
|
|
parts = filename_without_ext.split('_')
|
|
|
|
# Minimum 7 parts: JOB + BRAND + SUBJECT + ASSET + RATIO + COUNTRY + LANG
|
|
if len(parts) < 7:
|
|
validation_errors.append("Invalid structure: expected min 7 parts, got {}".format(len(parts)))
|
|
|
|
parsed = {
|
|
'original_filename': filename,
|
|
'filename_without_ext': filename_without_ext,
|
|
'extension': extension,
|
|
'omg_job_number': None,
|
|
'brand_code': None,
|
|
'subject_title': None,
|
|
'asset_type': None,
|
|
'seconds': None,
|
|
'aspect_ratio': None,
|
|
'spot_version': None,
|
|
'country_code': None,
|
|
'language_code': None,
|
|
'social_media_version': None,
|
|
'tracking_id': None,
|
|
'tracking_mode': 'full',
|
|
'tracking_id_with_suffix': None,
|
|
'has_master': False,
|
|
'validation_errors': [],
|
|
'warnings': [],
|
|
'is_valid': False
|
|
}
|
|
|
|
if len(parts) < 7:
|
|
parsed['validation_errors'] = validation_errors
|
|
return parsed
|
|
|
|
index = 0
|
|
|
|
# ===================================================================
|
|
# FIXED POSITIONS (Always in these positions)
|
|
# ===================================================================
|
|
|
|
# 1. OMG Job Number (digits only, max 10)
|
|
if index < len(parts) and parts[index].isdigit():
|
|
omg = parts[index]
|
|
if len(omg) > 10:
|
|
validation_errors.append("OMG Job Number too long: {} (max 10)".format(omg))
|
|
else:
|
|
parsed['omg_job_number'] = omg
|
|
index += 1
|
|
else:
|
|
if index < len(parts):
|
|
validation_errors.append("OMG Job Number missing or invalid: {}".format(parts[index]))
|
|
index += 1
|
|
|
|
# 2. Brand Code (2-5 chars, uppercase)
|
|
if index < len(parts):
|
|
brand = parts[index].upper()
|
|
if 2 <= len(brand) <= 5:
|
|
parsed['brand_code'] = brand
|
|
else:
|
|
validation_errors.append("Brand Code invalid: {} (must be 2-5 chars)".format(brand))
|
|
index += 1
|
|
|
|
# 3. Subject Title (NEW POSITION - was 5, now 3)
|
|
if index < len(parts):
|
|
subject = parts[index]
|
|
if len(subject) > 15:
|
|
warnings.append("Subject title exceeds 15 chars: {}".format(subject))
|
|
parsed['subject_title'] = subject
|
|
index += 1
|
|
|
|
# 4. Asset Type (NEW POSITION - was 6, now 4)
|
|
if index < len(parts):
|
|
asset = parts[index].upper()
|
|
if len(asset) == 3:
|
|
parsed['asset_type'] = asset
|
|
else:
|
|
validation_errors.append("Asset Type invalid: {} (must be 3 chars)".format(asset))
|
|
index += 1
|
|
|
|
# ===================================================================
|
|
# VARIABLE/OPTIONAL POSITIONS (Pattern-based detection)
|
|
# ===================================================================
|
|
|
|
# Now parse remaining parts using pattern detection
|
|
# Fields can appear in this order but some may be missing:
|
|
# [DURATION] [RATIO] [SPOT] [COUNTRY] [LANG] [SOCIAL] [TRACKING]
|
|
|
|
found_ratio = False
|
|
found_country = False
|
|
found_language = False
|
|
|
|
while index < len(parts):
|
|
part = parts[index]
|
|
|
|
# Duration: Digits + 'S' (e.g., "6S", "30S") - BEFORE ratio
|
|
if not found_ratio and re.match(r'^\d+S$', part, re.IGNORECASE):
|
|
parsed['seconds'] = part[:-1] # Remove 'S'
|
|
logger.debug("Found duration: {}".format(part))
|
|
index += 1
|
|
|
|
# Aspect Ratio: Contains 'x' or ':' (e.g., "16x9", "1x1")
|
|
elif not found_ratio and ('x' in part.lower() or ':' in part):
|
|
parsed['aspect_ratio'] = part
|
|
found_ratio = True
|
|
logger.debug("Found aspect ratio: {}".format(part))
|
|
index += 1
|
|
|
|
# Spot Version: Exactly "MST" or "REF" - AFTER ratio, BEFORE country
|
|
elif found_ratio and not found_country and part.upper() in ['MST', 'REF']:
|
|
parsed['spot_version'] = part.upper()
|
|
parsed['has_master'] = (part.upper() == 'MST')
|
|
logger.debug("Found spot version: {}".format(part))
|
|
index += 1
|
|
|
|
# Country Code: 2 uppercase alpha - AFTER ratio/spot
|
|
elif found_ratio and not found_country and len(part) == 2 and part.isalpha() and part.isupper():
|
|
parsed['country_code'] = part.upper()
|
|
found_country = True
|
|
logger.debug("Found country: {}".format(part))
|
|
index += 1
|
|
|
|
# Language Code: 2-3 lowercase alpha - AFTER country
|
|
elif found_country and not found_language and len(part) in [2, 3] and part.isalpha() and part.islower():
|
|
parsed['language_code'] = part.lower()
|
|
found_language = True
|
|
logger.debug("Found language: {}".format(part))
|
|
index += 1
|
|
|
|
# Social Media: One of known codes - AFTER language
|
|
elif found_language and part.upper() in self.SOCIAL_MEDIA_CODES:
|
|
parsed['social_media_version'] = part.upper()
|
|
logger.debug("Found social media: {}".format(part))
|
|
index += 1
|
|
|
|
# Tracking ID: 6 alphanumeric, optionally with -N suffix
|
|
elif re.match(r'^[a-zA-Z0-9]{6}(-N)?$', part):
|
|
tracking = part
|
|
tracking_mode = 'full'
|
|
base_tracking_id = tracking
|
|
|
|
if tracking.endswith('-N'):
|
|
tracking_mode = 'folder_only'
|
|
base_tracking_id = tracking[:-2] # Strip -N suffix
|
|
logger.info("Detected folder-only tracking ID: {} (base: {})".format(tracking, base_tracking_id))
|
|
|
|
parsed['tracking_id'] = base_tracking_id
|
|
parsed['tracking_mode'] = tracking_mode
|
|
parsed['tracking_id_with_suffix'] = tracking
|
|
logger.debug("Found tracking ID: {}".format(tracking))
|
|
index += 1
|
|
|
|
# Unknown part - could be aspect ratio fallback
|
|
elif not found_ratio:
|
|
# Might be aspect ratio in unexpected format
|
|
parsed['aspect_ratio'] = part
|
|
found_ratio = True
|
|
warnings.append("Aspect ratio in unexpected format: {}".format(part))
|
|
index += 1
|
|
else:
|
|
# Unknown component - skip it
|
|
warnings.append("Unknown component skipped: {}".format(part))
|
|
index += 1
|
|
|
|
# Set validation status
|
|
parsed['validation_errors'] = validation_errors
|
|
parsed['warnings'] = warnings
|
|
parsed['is_valid'] = len(validation_errors) == 0
|
|
|
|
return parsed
|
|
|
|
def strip_upload_components(self, filename):
|
|
"""
|
|
Strip OMG Job Number and Tracking ID from filename
|
|
Returns clean filename in V2.1 order
|
|
|
|
Args:
|
|
filename: Original filename
|
|
|
|
Returns:
|
|
Clean filename for upload (no job number, no tracking ID)
|
|
|
|
Example:
|
|
Input: 1234567_RAF_TEST_OLV_6S_1x1_REF_GL_it_IGF_abc123.mp4
|
|
Output: RAF_TEST_OLV_6S_1x1_REF_GL_it_IGF.mp4
|
|
"""
|
|
parsed = self.parse_filename(filename)
|
|
|
|
if not parsed:
|
|
return filename
|
|
|
|
# Build clean filename in V2.1 order
|
|
# [BRAND]_[SUBJECT]_[ASSET]_[DUR]_[RATIO]_[SPOT]_[COUNTRY]_[LANG]_[SOCIAL]
|
|
clean_parts = []
|
|
|
|
if parsed['brand_code']:
|
|
clean_parts.append(parsed['brand_code'])
|
|
if parsed['subject_title']:
|
|
clean_parts.append(parsed['subject_title'])
|
|
if parsed['asset_type']:
|
|
clean_parts.append(parsed['asset_type'])
|
|
if parsed['seconds']:
|
|
clean_parts.append(parsed['seconds'] + 'S')
|
|
if parsed['aspect_ratio']:
|
|
clean_parts.append(parsed['aspect_ratio'])
|
|
if parsed['spot_version']:
|
|
clean_parts.append(parsed['spot_version'])
|
|
if parsed['country_code']:
|
|
clean_parts.append(parsed['country_code'])
|
|
if parsed['language_code']:
|
|
clean_parts.append(parsed['language_code'])
|
|
if parsed['social_media_version']:
|
|
clean_parts.append(parsed['social_media_version'])
|
|
|
|
clean_filename = '_'.join(clean_parts)
|
|
|
|
if parsed['extension']:
|
|
clean_filename += parsed['extension']
|
|
|
|
return clean_filename
|