mirror of
https://github.com/shadoll/moma.git
synced 2026-08-28 03:27:34 +00:00
Refactor extractors and formatters for improved structure and functionality
- Converted static methods to instance methods in FileInfoExtractor and FilenameExtractor for better encapsulation. - Enhanced MediaInfoExtractor to initialize with file path and extract media information upon instantiation. - Updated MetadataExtractor to handle metadata extraction with improved error handling and added methods for meta type detection. - Introduced ColorFormatter for consistent text formatting across the application. - Refactored MediaFormatter to utilize the new extractor structure and improve output formatting. - Removed redundant utility functions and replaced them with direct calls in extractors. - Added ProposedNameFormatter for better handling of proposed filename formatting. - Updated extension handling to use MEDIA_TYPES for descriptions instead of VIDEO_EXT_DESCRIPTIONS.
This commit is contained in:
+98
-61
@@ -2,70 +2,107 @@ from pathlib import Path
|
||||
from .extractors.filename_extractor import FilenameExtractor
|
||||
from .extractors.metadata_extractor import MetadataExtractor
|
||||
from .extractors.mediainfo_extractor import MediaInfoExtractor
|
||||
from .extractors.fileinfo_extractor import FileInfoExtractor
|
||||
|
||||
|
||||
class MediaExtractor:
|
||||
"""Class to extract various metadata from media files using specialized extractors"""
|
||||
|
||||
def __init__(self):
|
||||
self.mediainfo_extractor = MediaInfoExtractor()
|
||||
def __init__(self, file_path: Path):
|
||||
self.file_path = file_path
|
||||
self.filename_extractor = FilenameExtractor(file_path)
|
||||
self.metadata_extractor = MetadataExtractor(file_path)
|
||||
self.mediainfo_extractor = MediaInfoExtractor(file_path)
|
||||
self.fileinfo_extractor = FileInfoExtractor(file_path)
|
||||
|
||||
# Define sources for each data type
|
||||
self._sources = {
|
||||
'title': [
|
||||
('metadata', lambda: self.metadata_extractor.extract_title()),
|
||||
('filename', lambda: self.filename_extractor.extract_title())
|
||||
],
|
||||
'year': [
|
||||
('filename', lambda: self.filename_extractor.extract_year())
|
||||
],
|
||||
'source': [
|
||||
('filename', lambda: self.filename_extractor.extract_source())
|
||||
],
|
||||
'frame_class': [
|
||||
('mediainfo', lambda: self.mediainfo_extractor.extract_frame_class()),
|
||||
('filename', lambda: self.filename_extractor.extract_frame_class())
|
||||
],
|
||||
'resolution': [
|
||||
('mediainfo', lambda: self.mediainfo_extractor.extract_resolution())
|
||||
],
|
||||
'aspect_ratio': [
|
||||
('mediainfo', lambda: self.mediainfo_extractor.extract_aspect_ratio())
|
||||
],
|
||||
'hdr': [
|
||||
('mediainfo', lambda: self.mediainfo_extractor.extract_hdr())
|
||||
],
|
||||
'audio_langs': [
|
||||
('mediainfo', lambda: self.mediainfo_extractor.extract_audio_langs())
|
||||
],
|
||||
'metadata': [
|
||||
('metadata', lambda: self.metadata_extractor.extract_all_metadata())
|
||||
],
|
||||
'meta_type': [
|
||||
('metadata', lambda: self.metadata_extractor.extract_meta_type())
|
||||
],
|
||||
'meta_description': [
|
||||
('metadata', lambda: self.metadata_extractor.extract_meta_description())
|
||||
],
|
||||
'file_size': [
|
||||
('fileinfo', lambda: self.fileinfo_extractor.extract_size())
|
||||
],
|
||||
'modification_time': [
|
||||
('fileinfo', lambda: self.fileinfo_extractor.extract_modification_time())
|
||||
],
|
||||
'file_name': [
|
||||
('fileinfo', lambda: self.fileinfo_extractor.extract_file_name())
|
||||
],
|
||||
'file_path': [
|
||||
('fileinfo', lambda: self.fileinfo_extractor.extract_file_path())
|
||||
],
|
||||
'extension': [
|
||||
('fileinfo', lambda: self.fileinfo_extractor.extract_extension())
|
||||
],
|
||||
'tracks': [
|
||||
('mediainfo', lambda: self.mediainfo_extractor.extract_tracks())
|
||||
]
|
||||
}
|
||||
|
||||
# Conditions for when a value is considered valid
|
||||
self._conditions = {
|
||||
'title': lambda x: x is not None,
|
||||
'year': lambda x: x is not None,
|
||||
'source': lambda x: x is not None,
|
||||
'frame_class': lambda x: x and x != 'Unclassified',
|
||||
'resolution': lambda x: x is not None,
|
||||
'aspect_ratio': lambda x: x is not None,
|
||||
'hdr': lambda x: x is not None,
|
||||
'audio_langs': lambda x: x is not None,
|
||||
'metadata': lambda x: x is not None,
|
||||
'tracks': lambda x: x != ""
|
||||
}
|
||||
|
||||
def extract_title(self, file_path: Path) -> str | None:
|
||||
"""Extract movie title from metadata or filename"""
|
||||
# Try metadata first
|
||||
title = MetadataExtractor.extract_title(file_path)
|
||||
if title:
|
||||
return title
|
||||
# Fallback to filename
|
||||
return FilenameExtractor.extract_title(file_path)
|
||||
|
||||
def extract_year(self, file_path: Path) -> str | None:
|
||||
"""Extract year from filename"""
|
||||
return FilenameExtractor.extract_year(file_path)
|
||||
|
||||
def extract_source(self, file_path: Path) -> str | None:
|
||||
"""Extract video source from filename"""
|
||||
return FilenameExtractor.extract_source(file_path)
|
||||
|
||||
def extract_frame_class(self, file_path: Path) -> str | None:
|
||||
"""Extract frame class from media info or filename"""
|
||||
# Try media info first
|
||||
frame_class = self.mediainfo_extractor.extract_frame_class(file_path)
|
||||
if frame_class:
|
||||
return frame_class
|
||||
# Fallback to filename
|
||||
return FilenameExtractor.extract_frame_class(file_path)
|
||||
|
||||
def extract_resolution(self, file_path: Path) -> str | None:
|
||||
"""Extract actual video resolution (WIDTHxHEIGHT) from media info"""
|
||||
return self.mediainfo_extractor.extract_resolution(file_path)
|
||||
|
||||
def extract_aspect_ratio(self, file_path: Path) -> str | None:
|
||||
"""Extract video aspect ratio from media info"""
|
||||
return self.mediainfo_extractor.extract_aspect_ratio(file_path)
|
||||
|
||||
def extract_hdr(self, file_path: Path) -> str | None:
|
||||
"""Extract HDR info from media info"""
|
||||
return self.mediainfo_extractor.extract_hdr(file_path)
|
||||
|
||||
def extract_audio_langs(self, file_path: Path) -> str:
|
||||
"""Extract audio languages from media info"""
|
||||
return self.mediainfo_extractor.extract_audio_langs(file_path)
|
||||
|
||||
def extract_metadata(self, file_path: Path) -> dict:
|
||||
"""Extract general metadata"""
|
||||
return MetadataExtractor.extract_all_metadata(file_path)
|
||||
|
||||
def extract_all(self, file_path: Path) -> dict:
|
||||
"""Extract all rename-related data"""
|
||||
return {
|
||||
'title': self.extract_title(file_path),
|
||||
'year': self.extract_year(file_path),
|
||||
'source': self.extract_source(file_path),
|
||||
'frame_class': self.extract_frame_class(file_path),
|
||||
'resolution': self.extract_resolution(file_path),
|
||||
'aspect_ratio': self.extract_aspect_ratio(file_path),
|
||||
'hdr': self.extract_hdr(file_path),
|
||||
'audio_langs': self.extract_audio_langs(file_path),
|
||||
'metadata': self.extract_metadata(file_path)
|
||||
}
|
||||
def get(self, key: str, source: str | None = None):
|
||||
"""Get extracted data by key, optionally from specific source"""
|
||||
if key not in self._sources:
|
||||
raise ValueError(f"Unknown key: {key}")
|
||||
|
||||
condition = self._conditions.get(key, lambda x: x is not None)
|
||||
|
||||
if source:
|
||||
for src, func in self._sources[key]:
|
||||
if src == source:
|
||||
val = func()
|
||||
return val if condition(val) else None
|
||||
raise ValueError(f"No such source '{source}' for key '{key}'")
|
||||
else:
|
||||
# Use fallback: return first valid value
|
||||
for src, func in self._sources[key]:
|
||||
val = func()
|
||||
if condition(val):
|
||||
return val
|
||||
return None
|
||||
Reference in New Issue
Block a user