59 lines
1.4 KiB
Python
59 lines
1.4 KiB
Python
|
from typing import Optional, Literal, List
|
||
|
from datetime import datetime
|
||
|
from pydantic import BaseModel, Field
|
||
|
import re
|
||
|
from bs4 import BeautifulSoup
|
||
|
import pypandoc
|
||
|
|
||
|
from masto_ld.models.models import Account
|
||
|
from masto_ld.patterns.wikilink import Wikilink
|
||
|
|
||
|
from typing import TYPE_CHECKING
|
||
|
if TYPE_CHECKING:
|
||
|
from masto_ld.models.thread import Thread
|
||
|
|
||
|
class Mention(BaseModel):
|
||
|
acct: str
|
||
|
id: int
|
||
|
url: str
|
||
|
username: str
|
||
|
|
||
|
class Status(BaseModel):
|
||
|
"""
|
||
|
Model of a toot on mastodon
|
||
|
|
||
|
See: https://mastodonpy.readthedocs.io/en/stable/#toot-dicts
|
||
|
"""
|
||
|
id: int
|
||
|
url: str
|
||
|
account: Account
|
||
|
content: str
|
||
|
created_at: datetime
|
||
|
visibility: Literal['public', 'unlisted', 'private', 'direct']
|
||
|
in_reply_to_id: Optional[int] = None
|
||
|
in_reply_to_account_id: Optional[int] = None
|
||
|
mentions: Optional[List[Mention]] = None
|
||
|
|
||
|
def format_content(self, format:str='commonmark-raw_html') -> str:
|
||
|
"""Format the HTMl content of the post using pypandoc"""
|
||
|
output = pypandoc.convert_text(self.content, format, format='html')
|
||
|
return output
|
||
|
|
||
|
@property
|
||
|
def soup(self) -> BeautifulSoup:
|
||
|
return BeautifulSoup(self.content)
|
||
|
|
||
|
@property
|
||
|
def wikilinks(self) -> List[Wikilink]:
|
||
|
return Wikilink.parse(self.content)
|
||
|
|
||
|
class Config:
|
||
|
extra='ignore'
|
||
|
|
||
|
class Post(BaseModel):
|
||
|
#timestamp: Optional[datetime] = None
|
||
|
text:str
|
||
|
status:Optional[Status] = None
|
||
|
|
||
|
|