Add view count to videos
- Video model: view_count column (Integer, nullable) - ytdlp._normalize_video: extract view_count from yt-dlp info - _VIDEO_SELECT: include v.view_count in all queries - VideoDetail schema: view_count field - Watch page: formatViews() helper, show "X.XM views" in meta row alongside date and category Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -60,6 +60,7 @@ class Video(Base):
|
|||||||
tags = Column(Text) # JSON array string
|
tags = Column(Text) # JSON array string
|
||||||
category = Column(String)
|
category = Column(String)
|
||||||
chapters = Column(Text) # JSON array of {start_time, end_time, title}
|
chapters = Column(Text) # JSON array of {start_time, end_time, title}
|
||||||
|
view_count = Column(Integer)
|
||||||
|
|
||||||
|
|
||||||
class UserVideo(Base):
|
class UserVideo(Base):
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ class VideoDetail(BaseModel):
|
|||||||
download_resolution: Optional[str] = None
|
download_resolution: Optional[str] = None
|
||||||
local_file_url: Optional[str] = None
|
local_file_url: Optional[str] = None
|
||||||
is_recommended: bool = False
|
is_recommended: bool = False
|
||||||
|
view_count: Optional[int] = None
|
||||||
|
|
||||||
model_config = {"from_attributes": True}
|
model_config = {"from_attributes": True}
|
||||||
|
|
||||||
@@ -421,7 +422,7 @@ def surprise_me(
|
|||||||
|
|
||||||
_VIDEO_SELECT = """
|
_VIDEO_SELECT = """
|
||||||
SELECT v.id, v.youtube_video_id, v.title, v.description, v.thumbnail_url,
|
SELECT v.id, v.youtube_video_id, v.title, v.description, v.thumbnail_url,
|
||||||
v.duration_seconds, v.published_at, v.tags, v.category,
|
v.duration_seconds, v.published_at, v.tags, v.category, v.view_count,
|
||||||
c.id AS channel_id, c.name AS channel_name, c.youtube_channel_id AS channel_youtube_id,
|
c.id AS channel_id, c.name AS channel_name, c.youtube_channel_id AS channel_youtube_id,
|
||||||
COALESCE(uv.watched, 0) AS watched,
|
COALESCE(uv.watched, 0) AS watched,
|
||||||
COALESCE(uv.watch_progress_seconds, 0) AS watch_progress_seconds,
|
COALESCE(uv.watch_progress_seconds, 0) AS watch_progress_seconds,
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ def _normalize_video(info: dict) -> dict:
|
|||||||
"tags": json.dumps(info.get("tags") or []),
|
"tags": json.dumps(info.get("tags") or []),
|
||||||
"category": info.get("category") or (info.get("categories") or [None])[0],
|
"category": info.get("category") or (info.get("categories") or [None])[0],
|
||||||
"chapters": json.dumps(chapters) if chapters else None,
|
"chapters": json.dumps(chapters) if chapters else None,
|
||||||
|
"view_count": info.get("view_count"),
|
||||||
"channel": {
|
"channel": {
|
||||||
"youtube_channel_id": info.get("channel_id"),
|
"youtube_channel_id": info.get("channel_id"),
|
||||||
"name": info.get("channel") or info.get("uploader", ""),
|
"name": info.get("channel") or info.get("uploader", ""),
|
||||||
|
|||||||
@@ -27,6 +27,14 @@ function formatDate(s) {
|
|||||||
return new Date(s).toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" });
|
return new Date(s).toLocaleDateString("en-US", { year: "numeric", month: "long", day: "numeric" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatViews(n) {
|
||||||
|
if (!n) return null;
|
||||||
|
if (n >= 1_000_000_000) return `${(n / 1_000_000_000).toFixed(1)}B views`;
|
||||||
|
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(n >= 100_000_000 ? 0 : 1)}M views`;
|
||||||
|
if (n >= 1_000) return `${Math.round(n / 1_000)}K views`;
|
||||||
|
return `${n} views`;
|
||||||
|
}
|
||||||
|
|
||||||
function formatSubs(n) {
|
function formatSubs(n) {
|
||||||
if (!n) return null;
|
if (!n) return null;
|
||||||
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(n >= 10_000_000 ? 0 : 1)}M`;
|
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(n >= 10_000_000 ? 0 : 1)}M`;
|
||||||
@@ -885,6 +893,7 @@ export default function Watch() {
|
|||||||
<div className="flex items-center justify-between flex-wrap gap-3">
|
<div className="flex items-center justify-between flex-wrap gap-3">
|
||||||
<div className="flex items-center gap-2 text-sm text-zinc-500 flex-wrap">
|
<div className="flex items-center gap-2 text-sm text-zinc-500 flex-wrap">
|
||||||
{date && <span>{date}</span>}
|
{date && <span>{date}</span>}
|
||||||
|
{video?.view_count > 0 && <><span>·</span><span>{formatViews(video.view_count)}</span></>}
|
||||||
{video?.category && <><span>·</span><span>{video.category}</span></>}
|
{video?.category && <><span>·</span><span>{video.category}</span></>}
|
||||||
{video?.duration_seconds && (
|
{video?.duration_seconds && (
|
||||||
<><span>·</span>
|
<><span>·</span>
|
||||||
|
|||||||
Reference in New Issue
Block a user