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

73
bot/tautulli_client.py Normal file
View File

@ -0,0 +1,73 @@
# bot/tautulli_client.py
import os
import requests
from datetime import datetime, timedelta
from dotenv import load_dotenv
load_dotenv()
TAUTULLI_URL = os.getenv("TAUTULLI_URL")
TAUTULLI_API_KEY = os.getenv("TAUTULLI_API_KEY")
class TautulliClient:
def __init__(self):
self.base_url = f"{TAUTULLI_URL}/api/v2"
self.api_key = TAUTULLI_API_KEY
def _request(self, cmd: str, params: dict = {}):
payload = {
"apikey": self.api_key,
"cmd": cmd,
**params
}
try:
res = requests.get(self.base_url, params=payload)
res.raise_for_status()
return res.json().get("response", {}).get("data", [])
except Exception as e:
print(f"❌ Tautulli API error for {cmd}: {e}")
return []
def get_user_watch_history(self, username: str, media_type: str = "movie", days: int = 14, max_items: int = 20):
"""Get a list of recently watched movies or shows by username."""
cutoff = datetime.now() - timedelta(days=days)
print(media_type)
if media_type=='movie':
tautulli_media_type='movie'
elif media_type=='tv':
tautulli_media_type='episode'
history = self._request("get_history", {
"user": username,
"media_type":tautulli_media_type,
"order_column": "date",
"order_dir": "desc",
"length": 100 # buffer to allow filtering
})
history = history['data']
#print(history)
filtered = []
print('FULL HISTORY')
for item in history:
print(item['user'],item['media_type'],item['title'],item['grandparent_title'],username,cutoff,datetime.fromtimestamp(item["date"]))
try:
watched_date = datetime.fromtimestamp(item["date"])
if watched_date < cutoff:
continue
filtered.append({
"title": item["title"] if media_type == "movie" else item["grandparent_title"],
"type": media_type,
"genres": item.get("genre", "").split(", "),
"summary": item.get("summary", "")
})
if len(filtered) >= max_items:
break
except Exception:
continue
print(filtered)
return filtered