From 0fe5aea38748507f0bd0a6ed2ede0872b7e68983 Mon Sep 17 00:00:00 2001 From: sushi-chaaaan Date: Mon, 30 Sep 2024 19:33:09 +0900 Subject: [PATCH 1/4] =?UTF-8?q?MediaDetails=E3=82=92=E5=AE=9A=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/birdxplorer_common/models.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/common/birdxplorer_common/models.py b/common/birdxplorer_common/models.py index 908d52a..95df64c 100644 --- a/common/birdxplorer_common/models.py +++ b/common/birdxplorer_common/models.py @@ -15,11 +15,14 @@ Union, ) from uuid import UUID +from typing import Annotated, Any, Dict, List, Literal, Optional, Type, TypeAlias, TypeVar, Union from pydantic import BaseModel as PydanticBaseModel from pydantic import ConfigDict from pydantic import Field as PydanticField from pydantic import GetCoreSchemaHandler, HttpUrl, TypeAdapter, model_validator +from pydantic import BaseModel as PydanticBaseModel, Field as PydanticField +from pydantic import ConfigDict, GetCoreSchemaHandler, HttpUrl, TypeAdapter from pydantic.alias_generators import to_camel from pydantic.main import IncEx from pydantic_core import core_schema From d90f0a9887e9cf73e06868141987784a26d0721f Mon Sep 17 00:00:00 2001 From: sushi-chaaaan Date: Mon, 30 Sep 2024 19:33:26 +0900 Subject: [PATCH 2/4] =?UTF-8?q?MediaDetails=E3=82=92DB=E5=81=B4=E3=81=A7?= =?UTF-8?q?=E8=A1=A8=E7=8F=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/birdxplorer_common/storage.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/common/birdxplorer_common/storage.py b/common/birdxplorer_common/storage.py index 6eee4af..4b07f26 100644 --- a/common/birdxplorer_common/storage.py +++ b/common/birdxplorer_common/storage.py @@ -7,15 +7,20 @@ from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column, relationship from sqlalchemy.types import CHAR, DECIMAL, JSON, Integer, String, Uuid -from .models import BinaryBool, LanguageIdentifier -from .models import Link as LinkModel -from .models import LinkId, Media, MediaDetails, MediaType, NonNegativeInt -from .models import Note as NoteModel -from .models import NoteId, NotesClassification, NotesHarmful, ParticipantId -from .models import Post as PostModel -from .models import PostId, SummaryString -from .models import Topic as TopicModel from .models import ( + BinaryBool, + LanguageIdentifier, + LinkId, + Media, + MediaDetails, + MediaType, + NonNegativeInt, + NoteId, + NotesClassification, + NotesHarmful, + ParticipantId, + PostId, + SummaryString, TopicId, TopicLabel, TwitterTimestamp, @@ -23,6 +28,10 @@ UserId, UserName, ) +from .models import Link as LinkModel +from .models import Note as NoteModel +from .models import Post as PostModel +from .models import Topic as TopicModel from .models import XUser as XUserModel from .settings import GlobalSettings From 0c58393da4bc542ac60bee8680c3283802dbacf6 Mon Sep 17 00:00:00 2001 From: sushi-chaaaan Date: Mon, 30 Sep 2024 20:04:34 +0900 Subject: [PATCH 3/4] =?UTF-8?q?MediaDetails=E3=81=A7None=E3=82=92=E8=A8=B1?= =?UTF-8?q?=E5=AE=B9=E3=81=97=E3=81=AA=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/birdxplorer_common/models.py | 3 --- common/birdxplorer_common/storage.py | 25 ++++++++----------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/common/birdxplorer_common/models.py b/common/birdxplorer_common/models.py index 95df64c..908d52a 100644 --- a/common/birdxplorer_common/models.py +++ b/common/birdxplorer_common/models.py @@ -15,14 +15,11 @@ Union, ) from uuid import UUID -from typing import Annotated, Any, Dict, List, Literal, Optional, Type, TypeAlias, TypeVar, Union from pydantic import BaseModel as PydanticBaseModel from pydantic import ConfigDict from pydantic import Field as PydanticField from pydantic import GetCoreSchemaHandler, HttpUrl, TypeAdapter, model_validator -from pydantic import BaseModel as PydanticBaseModel, Field as PydanticField -from pydantic import ConfigDict, GetCoreSchemaHandler, HttpUrl, TypeAdapter from pydantic.alias_generators import to_camel from pydantic.main import IncEx from pydantic_core import core_schema diff --git a/common/birdxplorer_common/storage.py b/common/birdxplorer_common/storage.py index 4b07f26..6eee4af 100644 --- a/common/birdxplorer_common/storage.py +++ b/common/birdxplorer_common/storage.py @@ -7,20 +7,15 @@ from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column, relationship from sqlalchemy.types import CHAR, DECIMAL, JSON, Integer, String, Uuid +from .models import BinaryBool, LanguageIdentifier +from .models import Link as LinkModel +from .models import LinkId, Media, MediaDetails, MediaType, NonNegativeInt +from .models import Note as NoteModel +from .models import NoteId, NotesClassification, NotesHarmful, ParticipantId +from .models import Post as PostModel +from .models import PostId, SummaryString +from .models import Topic as TopicModel from .models import ( - BinaryBool, - LanguageIdentifier, - LinkId, - Media, - MediaDetails, - MediaType, - NonNegativeInt, - NoteId, - NotesClassification, - NotesHarmful, - ParticipantId, - PostId, - SummaryString, TopicId, TopicLabel, TwitterTimestamp, @@ -28,10 +23,6 @@ UserId, UserName, ) -from .models import Link as LinkModel -from .models import Note as NoteModel -from .models import Post as PostModel -from .models import Topic as TopicModel from .models import XUser as XUserModel from .settings import GlobalSettings From 494dc98b65acc5fb80b76e91dd53cc43c6463e64 Mon Sep 17 00:00:00 2001 From: sushi-chaaaan Date: Wed, 2 Oct 2024 16:47:30 +0900 Subject: [PATCH 4/4] =?UTF-8?q?Post.link=E3=82=92computed=20field=E3=81=AB?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/birdxplorer_api/routers/data.py | 3 --- common/birdxplorer_common/models.py | 26 ++++++++++++++++++++++++-- common/tests/conftest.py | 3 --- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/api/birdxplorer_api/routers/data.py b/api/birdxplorer_api/routers/data.py index e0ef426..e51e13d 100644 --- a/api/birdxplorer_api/routers/data.py +++ b/api/birdxplorer_api/routers/data.py @@ -132,9 +132,6 @@ def get_posts( search_url=search_url, ) - for post in posts: - post.link = HttpUrl(f"https://x.com/{post.x_user.name}/status/{post.post_id}") - base_url = str(request.url).split("?")[0] next_offset = offset + limit prev_offset = max(offset - limit, 0) diff --git a/common/birdxplorer_common/models.py b/common/birdxplorer_common/models.py index 908d52a..25b1e4b 100644 --- a/common/birdxplorer_common/models.py +++ b/common/birdxplorer_common/models.py @@ -19,7 +19,7 @@ from pydantic import BaseModel as PydanticBaseModel from pydantic import ConfigDict from pydantic import Field as PydanticField -from pydantic import GetCoreSchemaHandler, HttpUrl, TypeAdapter, model_validator +from pydantic import GetCoreSchemaHandler, HttpUrl, TypeAdapter, model_validator, computed_field from pydantic.alias_generators import to_camel from pydantic.main import IncEx from pydantic_core import core_schema @@ -770,7 +770,6 @@ def validate_link_id(cls, values: Dict[str, Any]) -> Dict[str, Any]: class Post(BaseModel): post_id: PostId - link: Optional[HttpUrl] = None x_user_id: UserId x_user: XUser text: str @@ -781,6 +780,29 @@ class Post(BaseModel): impression_count: NonNegativeInt links: List[Link] = [] + @property + @computed_field + def link(self) -> HttpUrl: + """ + PostのX上でのURLを返す。 + + Examples + -------- + >>> post = Post(post_id="1234567890123456789", + x_user_id="1234567890123456789", + x_user=XUser(user_id="1234567890123456789", + name="test", + profile_image="https://x.com/test"), + text="test", + created_at=1288834974657, + like_count=1, + repost_count=1, + impression_count=1) + >>> post.link + HttpUrl('https://x.com/test/status/1234567890123456789') + """ + return HttpUrl(f"https://x.com/{self.x_user.name}/status/{self.post_id}") + class PaginationMeta(BaseModel): next: Optional[HttpUrl] = None diff --git a/common/tests/conftest.py b/common/tests/conftest.py index 78865d7..c972acd 100644 --- a/common/tests/conftest.py +++ b/common/tests/conftest.py @@ -267,7 +267,6 @@ def post_samples( posts = [ post_factory.build( post_id="2234567890123456781", - link=None, x_user_id="1234567890123456781", x_user=x_user_samples[0], text="""\ @@ -283,7 +282,6 @@ def post_samples( ), post_factory.build( post_id="2234567890123456791", - link=None, x_user_id="1234567890123456781", x_user=x_user_samples[0], text="""\ @@ -299,7 +297,6 @@ def post_samples( ), post_factory.build( post_id="2234567890123456801", - link=None, x_user_id="1234567890123456782", x_user=x_user_samples[1], text="""\