Skip to content

Commit 48c92a1

Browse files
authored
Merge pull request #52 from jkawamoto/timezone
Make datetime handling timezone-aware
2 parents c2219ad + 8f08c5d commit 48c92a1

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/mcp_youtube_transcript/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from contextlib import asynccontextmanager
1111
from dataclasses import dataclass
12-
from datetime import datetime, timedelta
12+
from datetime import datetime, timedelta, timezone
1313
from functools import lru_cache, partial
1414
from itertools import islice
1515
from typing import AsyncIterator, Tuple
@@ -22,7 +22,7 @@
2222
from mcp import ServerSession
2323
from mcp.server import FastMCP
2424
from mcp.server.fastmcp import Context
25-
from pydantic import Field, BaseModel
25+
from pydantic import Field, BaseModel, AwareDatetime
2626
from youtube_transcript_api import YouTubeTranscriptApi, FetchedTranscriptSnippet
2727
from youtube_transcript_api.proxies import WebshareProxyConfig, GenericProxyConfig, ProxyConfig
2828
from yt_dlp import YoutubeDL
@@ -83,14 +83,14 @@ class VideoInfo(BaseModel):
8383
title: str = Field(description="Title of the video")
8484
description: str = Field(description="Description of the video")
8585
uploader: str = Field(description="Uploader of the video")
86-
upload_date: datetime = Field(description="Upload date of the video")
86+
upload_date: AwareDatetime = Field(description="Upload date of the video")
8787
duration: str = Field(description="Duration of the video")
8888

8989

9090
def _parse_time_info(date: int, timestamp: int, duration: int) -> Tuple[datetime, str]:
9191
parsed_date = datetime.strptime(str(date), "%Y%m%d").date()
9292
parsed_time = datetime.strptime(str(timestamp), "%H%M%S%f").time()
93-
upload_date = datetime.combine(parsed_date, parsed_time)
93+
upload_date = datetime.combine(parsed_date, parsed_time, timezone.utc)
9494
duration_str = humanize.naturaldelta(timedelta(seconds=duration))
9595
return upload_date, duration_str
9696

tests/test_mcp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# This software is released under the MIT License.
66
#
77
# http://opensource.org/licenses/mit-license.php
8-
from datetime import datetime, timedelta
8+
from datetime import datetime, timedelta, timezone
99
import os
1010
from typing import AsyncGenerator
1111

@@ -376,12 +376,12 @@ async def test_get_video_info(mcp_client_session: ClientSession) -> None:
376376
)
377377
assert isinstance(res.content[0], TextContent)
378378

379-
info = VideoInfo.model_validate_json(res.content[0].text)
379+
info = VideoInfo.model_validate_json(res.content[0].text, strict=True)
380380
assert info == expect
381381
assert not res.isError
382382

383383

384384
def test_parse_time_info() -> None:
385385
upload_date, duration = _parse_time_info(20250921, 1650496000, 1234567)
386-
assert upload_date == datetime(2025, 9, 21, 16, 50, 49, 600000)
386+
assert upload_date == datetime(2025, 9, 21, 16, 50, 49, 600000, timezone.utc)
387387
assert duration == humanize.naturaldelta(timedelta(seconds=1234567))

0 commit comments

Comments
 (0)