Working version

This commit is contained in:
2025-06-03 13:12:58 -04:00
parent e991862a01
commit 24dbe252de
3 changed files with 71 additions and 38 deletions

View File

@ -5,6 +5,7 @@ import os
import time
from typing import List
from plex_client import PlexClient
from difflib import SequenceMatcher
CACHE_FILE = 'bot/library_cache.json'
MAX_CACHE_AGE_HOURS = 6
@ -30,11 +31,16 @@ class LibraryCache:
new_data = []
for section in media:
if section.type in ['movie', 'show']:
section_type = section.type.lower()
# Normalize to 'show'
if section_type == 'show':
section_type = 'tv'
if section_type in ['movie', 'tv']:
for item in section.all():
new_data.append({
'title': item.title.strip(),
'type': section.type,
'type': section_type,
'genres': [g.tag for g in getattr(item, 'genres', [])],
'year': getattr(item, 'year', None)
})
@ -42,6 +48,7 @@ class LibraryCache:
self.data = new_data
self.save_cache()
def save_cache(self):
with open(CACHE_FILE, 'w', encoding='utf-8') as f:
json.dump(self.data, f, ensure_ascii=False, indent=2)
@ -53,9 +60,16 @@ class LibraryCache:
def get_titles_by_type(self, media_type: str) -> List[str]:
return [item['title'] for item in self.data if item['type'] == media_type]
def search(self, title: str, media_type: str = None) -> bool:
"""Check if title exists in library (case-insensitive, optional type filter)."""
def search(self, title: str, media_type: str) -> bool:
def is_match(a: str, b: str) -> bool:
ratio = SequenceMatcher(None, a.lower(), b.lower()).ratio()
return ratio > 0.8 # tweak as needed
for item in self.data:
if item['title'].lower() == title.lower() and (media_type is None or item['type'] == media_type):
if item["type"] != media_type:
continue
if is_match(title, item["title"]):
return True
return False