feat: add models and enum

This commit is contained in:
firedotguy
2026-01-30 20:49:49 +03:00
parent 1a606da55f
commit c7e3812ee8
17 changed files with 210 additions and 5 deletions

View File

@@ -14,6 +14,7 @@ from itd.routes.search import search
from itd.routes.files import upload_file
from itd.routes.auth import refresh_token, change_password, logout
from itd.routes.verification import verificate, get_verification_status
from itd.request import set_cookies
def refresh_on_error(func):
@@ -35,6 +36,7 @@ class Client:
if token:
self.token = token.replace('Bearer ', '')
elif self.cookies:
set_cookies(self.cookies)
self.refresh_auth()
else:
raise ValueError('Provide token or cookie')

25
itd/enums.py Normal file
View File

@@ -0,0 +1,25 @@
from enum import Enum
class NotificationType(Enum):
WALL_POST = 'wall_post'
REPLY = 'reply'
REPOST = 'repost'
COMMENT = 'comment'
FOLLOW = 'follow'
LIKE = 'like'
class NotificationTargetType(Enum):
POST = 'post'
class ReportTargetType(Enum):
POST = 'post'
USER = 'user'
COMMENT = 'comment'
class ReportTargetReason(Enum):
SPAM = 'spam' # спам
VIOLENCE = 'violence' # насилие
HATE = 'hate' # ненависть
ADULT = 'adult' # 18+
FRAUD = 'fraud' # обман\мошенничество
OTHER = 'other' # другое

17
itd/models/_text.py Normal file
View File

@@ -0,0 +1,17 @@
from uuid import UUID
from datetime import datetime
from pydantic import BaseModel, Field
from itd.models.user import UserPost
class _TextObject(BaseModel):
id: UUID
content: str
author: UserPost
attachments: list[UUID]
created_at: datetime = Field(alias='createdAt')
model_config = {'populate_by_name': True}

14
itd/models/comment.py Normal file
View File

@@ -0,0 +1,14 @@
from uuid import UUID
from datetime import datetime
from pydantic import Field
from itd.models._text import _TextObject
class CommentShort(_TextObject):
likes_count: int = Field(0, alias='likesCount')
replies_count: int = Field(0, alias='repliesCount')
is_liked: bool = Field(False, alias='isLiked')
replies: list['CommentShort'] = []

10
itd/models/file.py Normal file
View File

@@ -0,0 +1,10 @@
from uuid import UUID
from pydantic import BaseModel, Field
class File(BaseModel):
id: UUID
url: str
filename: str
mime_type: str = Field('image/png', alias='mimeType')
size: int

8
itd/models/hashtag.py Normal file
View File

@@ -0,0 +1,8 @@
from uuid import UUID
from pydantic import BaseModel, Field
class Hashtag(BaseModel):
id: UUID
name: str
posts_count: int = Field(0, alias='postsCount')

View File

@@ -0,0 +1,22 @@
from uuid import UUID
from datetime import datetime
from pydantic import BaseModel, Field
from itd.enums import NotificationType, NotificationTargetType
from itd.models.user import UserNotification
class Notification(BaseModel):
id: UUID
type: NotificationType
target_type: NotificationTargetType | None = Field(None, alias='targetType') # none - follows, other - NotificationTragetType.POST
target_id: int | None = Field(None, alias='targetId') # none - follows
preview: str | None = None # follow - none, comment/reply - content, repost - original post content, like - post content, wall_post - wall post content
read: bool = False
read_at: datetime | None = Field(None, alias='readAt')
created_at: datetime = Field(alias='createdAt')
actor: UserNotification

29
itd/models/post.py Normal file
View File

@@ -0,0 +1,29 @@
from pydantic import Field
from itd.models.user import UserPost
from itd.models._text import _TextObject
class PostShort(_TextObject):
likes_count: int = Field(0, alias='likesCount')
comments_count: int = Field(0, alias='commentsCount')
reposts_count: int = Field(0, alias='repostsCount')
views_count: int = Field(0, alias='viewsCount')
class OriginalPost(PostShort):
is_deleted: bool = Field(False, alias='isDeleted')
class Post(PostShort):
is_liked: bool = Field(False, alias='isLiked')
is_reposted: bool = Field(False, alias='isReposted')
is_viewed: bool = Field(False, alias='isViewed')
is_owner: bool = Field(False, alias='isOwner')
comments: list = []
original_post: OriginalPost | None = None
wall_recipient_id: int | None = None
wall_recipient: UserPost | None = None

16
itd/models/report.py Normal file
View File

@@ -0,0 +1,16 @@
from uuid import UUID
from datetime import datetime
from pydantic import BaseModel, Field
from itd.enums import ReportTargetType, ReportTargetReason
class Report(BaseModel):
id: UUID
reason: ReportTargetReason
description: str | None = None
target_type: ReportTargetType = Field(alias='targetType')
target_id: UUID
created_at: datetime = Field(alias='createdAt')

37
itd/models/user.py Normal file
View File

@@ -0,0 +1,37 @@
from uuid import UUID
from datetime import datetime
from pydantic import BaseModel, Field
class UserNotification(BaseModel):
id: UUID
username: str
display_name: str = Field(alias='displayName')
avatar: str
model_config = {'populate_by_name': True}
class UserPost(UserNotification):
verified: bool = False
class UserSearch(UserPost):
followers_count: int = Field(0, alias='followersCount')
class User(UserSearch):
banner: str | None = None
bio: str | None = None
pinned_post_id: UUID | None
private: bool | None = Field(None, alias='isPrivate') # none for not me
wall_closed: bool = Field(False, alias='wallClosed')
following_count: int = Field(0, alias='followingCount')
posts_count: int = Field(0, alias='postsCount')
is_following: bool | None = Field(None, alias='isFollowing') # none for me
is_followed: bool | None = Field(None, alias='isFollowedBy') # none for me
created_at: datetime = Field(alias='createdAt')

View File

@@ -0,0 +1,23 @@
from uuid import UUID
from datetime import datetime
from pydantic import BaseModel, Field
class Verification(BaseModel):
id: UUID
user_id: UUID = Field(alias='userId')
video_url: str = Field(alias='videoUrl')
status: str # should be enum, but we dont know all statuses (what status for accepted?)
reject_reason: str | None = Field(None, alias='rejectionReason')
reviewer: str | None = Field(None, alias='reviewedBy')
reviewed_at: datetime | None = Field(None, alias='reviewedAt')
created_at: datetime = Field(alias='createdAt')
updated_at: datetime = Field(alias='updatedAt')
class VerificationStatus(BaseModel):
status: str # should be enum, but we dont know all statuses (what status for accepted?)
request_id: UUID = Field(alias='requestId')
submitted_at: datetime = Field(alias='submittedAt')

View File

@@ -7,7 +7,7 @@ def add_comment(token: str, post_id: str, content: str, reply_comment_id: str |
return fetch(token, 'post', f'posts/{post_id}/comments', data)
def get_comments(token: str, post_id: str, limit: int = 20, cursor: int = 0, sort: str = 'popular'):
return fetch(token, 'get', f'posts/{post_id}/comments', {'limit': limit, 'sort': sort, 'cursor': cursor})
return fetch(token, 'get', f'posts/{post_id}/comments', {'limit': limit, 'sort': sort, 'cursor': cursor})['data']
def like_comment(token: str, comment_id: str):
return fetch(token, 'post', f'comments/{comment_id}/like')

View File

@@ -1,7 +1,7 @@
from itd.request import fetch
def get_hastags(token: str, limit: int = 10):
return fetch(token, 'get', 'hashtags/trending', {'limit': limit})
return fetch(token, 'get', 'hashtags/trending', {'limit': limit})['data']
def get_posts_by_hastag(token: str, hashtag: str, limit: int = 20, cursor: int = 0):
return fetch(token, 'get', f'hashtags/{hashtag}/posts', {'limit': limit, 'cursor': cursor})

View File

@@ -18,7 +18,7 @@ def get_posts(token: str, username: str | None = None, limit: int = 20, cursor:
if tab:
data['tab'] = tab
return fetch(token, 'get', 'posts', data)
return fetch(token, 'get', 'posts', data)['data']
def get_post(token: str, id: str):
return fetch(token, 'get', f'posts/{id}')

View File

@@ -12,6 +12,6 @@ authors = [
]
license = "MIT"
dependencies = [
"requests"
"requests", "pydantic"
]
requires-python = ">=3.9"

View File

@@ -0,0 +1,2 @@
pydantic==2.11.9
requests==2.32.3

View File

@@ -5,7 +5,7 @@ setup(
version='0.2.0',
packages=find_packages(),
install_requires=[
'requests'
'requests', 'pydantic'
],
python_requires=">=3.9"
)