ytptube/app/library/encoder.py
Jesse Bate 429f97a60c perf: exclude unused fields from ItemDTO list and WebSocket serialization
Every item_updated WebSocket event and every history/queue list response
was serializing the full ItemDTO including fields the UI never reads.

Add ItemDTO.serialize_for_list() which excludes: options,
template_chapter, temp_dir, total_bytes, total_bytes_estimate,
tmpfilename, archive_id. The global Encoder now uses serialize_for_list()
for all list/event contexts; the full serialize() remains available for
detail endpoints (GET /api/history/{id}).

Mark the excluded fields optional in the TypeScript StoreItem type.
2026-06-10 20:09:13 +09:30

38 lines
1.1 KiB
Python

import json
from datetime import date
from pathlib import Path
from yt_dlp.networking.impersonate import ImpersonateTarget
from yt_dlp.utils import DateRange
class Encoder(json.JSONEncoder):
"""
This class is used to serialize objects to JSON.
The only difference between this and the default JSONEncoder is that this one
will call the __dict__ method of an object if it exists.
"""
def default(self, o):
from .ItemDTO import ItemDTO
if isinstance(o, DateRange):
return {"start": str(o.start).replace("-", ""), "end": str(o.end).replace("-", "")}
if isinstance(o, (Path, date, ImpersonateTarget, ValueError)):
return str(o)
if isinstance(o, ItemDTO):
return o.serialize_for_list()
if isinstance(o, object):
if hasattr(o, "serialize"):
return o.serialize()
if hasattr(o, "model_dump"):
return o.model_dump()
if hasattr(o, "__dict__"):
return o.__dict__
return json.JSONEncoder.default(self, o)