-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
46 lines (39 loc) · 1.22 KB
/
utils.py
File metadata and controls
46 lines (39 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import requests
from PIL import Image, ImageTk
import io
import threading
TMDB_IMAGE_BASE = "https://image.tmdb.org/t/p/w300"
# Basit in-memory cache: {url: PIL.ImageTk.PhotoImage}
_image_cache = {}
_image_cache_lock = threading.Lock()
def fetch_poster_image(poster_path):
"""
Poster yolu verilince PIL ImageTk.PhotoImage döner.
Cache içeriyorsa oradan verir.
Yoksa indirir ve cache'e ekler.
"""
if not poster_path:
return None
url = TMDB_IMAGE_BASE + poster_path
with _image_cache_lock:
if url in _image_cache:
return _image_cache[url]
try:
resp = requests.get(url, timeout=10)
resp.raise_for_status()
img_data = resp.content
pil_image = Image.open(io.BytesIO(img_data)).convert("RGBA")
photo_image = ImageTk.PhotoImage(pil_image)
with _image_cache_lock:
_image_cache[url] = photo_image
return photo_image
except Exception as e:
print(f"[UTILS ERROR] fetch_poster_image: {e}")
return None
def safe_int(value, default=0):
try:
return int(value)
except (ValueError, TypeError):
return default
def media_key(media_type, media_id):
return f"{media_type}_{media_id}"