Coverage for app \ services \ video_files.py: 89%

35 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-28 20:58 -0400

1from pathlib import Path 

2from typing import Dict, List, Optional 

3 

4from app.settings import DOWNLOADS_DIR 

5from app.settings import VIDEO_EXTENSIONS 

6 

7 

8def find_primary_video_file(video_dir: Path) -> Optional[Path]: 

9 if not video_dir.exists() or not video_dir.is_dir(): 

10 return None 

11 base_name = video_dir.name 

12 for ext in VIDEO_EXTENSIONS: 

13 matches = sorted(video_dir.glob(f"*{ext}"), key=lambda path: _video_sort_key(path, base_name)) 

14 if matches: 

15 return matches[0] 

16 return None 

17 

18 

19def _video_sort_key(path: Path, base_name: str): 

20 exact_name = 0 if path.stem == base_name else 1 

21 extra_parts = path.stem.count(".") 

22 return (exact_name, extra_parts, len(path.name), path.name.lower()) 

23 

24 

25def list_question_json_files() -> List[Dict[str, str]]: 

26 files: List[Dict[str, str]] = [] 

27 if not DOWNLOADS_DIR.exists(): 

28 return files 

29 for video_dir in sorted(DOWNLOADS_DIR.iterdir()): 

30 if not video_dir.is_dir(): 

31 continue 

32 questions_dir = video_dir / "questions" 

33 if not questions_dir.is_dir(): 

34 continue 

35 for json_file in sorted(questions_dir.glob("*.json")): 

36 try: 

37 rel_path = json_file.relative_to(DOWNLOADS_DIR).as_posix() 

38 except ValueError: 

39 continue 

40 files.append( 

41 { 

42 "video_id": video_dir.name, 

43 "name": json_file.name, 

44 "rel_path": rel_path, 

45 } 

46 ) 

47 files.sort(key=lambda item: (item["video_id"], item["name"])) 

48 return files