Initial working version

This commit is contained in:
2025-05-29 15:04:36 -04:00
parent 7206661877
commit e991862a01
10 changed files with 406 additions and 0 deletions

61
bot/library_cache.py Normal file
View File

@ -0,0 +1,61 @@
# bot/library_cache.py
import json
import os
import time
from typing import List
from plex_client import PlexClient
CACHE_FILE = 'bot/library_cache.json'
MAX_CACHE_AGE_HOURS = 6
class LibraryCache:
def __init__(self):
if self.is_cache_stale():
print("📦 Plex cache is stale or missing — refreshing...")
self.refresh_cache()
else:
print("✅ Using cached Plex library data.")
self.load_cache()
def is_cache_stale(self) -> bool:
if not os.path.exists(CACHE_FILE):
return True
age_hours = (time.time() - os.path.getmtime(CACHE_FILE)) / 3600
return age_hours > MAX_CACHE_AGE_HOURS
def refresh_cache(self):
plex = PlexClient()
media = plex.server.library.sections()
new_data = []
for section in media:
if section.type in ['movie', 'show']:
for item in section.all():
new_data.append({
'title': item.title.strip(),
'type': section.type,
'genres': [g.tag for g in getattr(item, 'genres', [])],
'year': getattr(item, 'year', None)
})
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)
def load_cache(self):
with open(CACHE_FILE, 'r', encoding='utf-8') as f:
self.data = json.load(f)
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)."""
for item in self.data:
if item['title'].lower() == title.lower() and (media_type is None or item['type'] == media_type):
return True
return False