-
Notifications
You must be signed in to change notification settings - Fork 1
/
content_parser.py
164 lines (135 loc) · 5.03 KB
/
content_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
Copyright (c) 2024 AstreaTSS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from dataclasses import dataclass
from enum import IntEnum
from html.parser import HTMLParser
from typing import Any
import atproto
class LinkMode(IntEnum):
URL = 0
HASHTAG = 1
MENTION = 2
@dataclass
class LinkData:
start_index: int
end_index: int
url: str
@dataclass
class HashtagData:
start_index: int
end_index: int
hashtag: str
class ContentParser(HTMLParser):
def __init__(self) -> None:
super().__init__()
self.data: list[str] = []
self.hashtags: list[HashtagData] = []
self.links: list[LinkData] = []
self.link_mode = LinkMode.URL
self.double_line: bool = False
self.character_index = 0
def handle_data(self, data: str) -> None:
if self.double_line:
self.data.append("\n\n")
self.character_index += 2
if self.link_mode == LinkMode.HASHTAG:
self.hashtags[-1].hashtag += data
self.data.append(data)
self.character_index += len(data)
self.double_line = True
def handle_startendtag(self, tag: str, _: Any) -> None:
if tag == "br":
self._handle_single_line()
def handle_starttag(self, tag: str, attrs: list[tuple[str, str]]) -> None:
if tag == "a":
if class_attr := next(
(attr[1] for attr in attrs if attr[0] == "class"), None
):
if "mention hashtag" in class_attr:
self.link_mode = LinkMode.HASHTAG
self.hashtags.append(
HashtagData(
self.character_index,
-1,
"",
)
)
elif "u-url mention" in class_attr:
self.link_mode = LinkMode.MENTION
elif href_attr := next(
(attr[1] for attr in attrs if attr[0] == "href"), None
):
self.link_mode = LinkMode.URL
self.links.append(
LinkData(
self.character_index,
-1,
href_attr,
)
)
elif tag == "br":
self._handle_single_line()
elif tag == "span":
self.double_line = False
def _handle_single_line(self) -> None:
self.data.append("\n")
self.character_index += 1
self.double_line = False
def handle_endtag(self, tag: str) -> None:
if tag == "a":
if self.link_mode == LinkMode.MENTION:
return
if self.link_mode == LinkMode.HASHTAG:
self.hashtags[-1].end_index = self.character_index
self.link_mode = LinkMode.URL
else:
self.links[-1].end_index = self.character_index
elif tag == "p":
self.double_line = True
elif tag == "span":
self.double_line = False
@property
def content(self) -> str:
return "".join(self.data).strip()
def build_facets(self) -> list[atproto.models.AppBskyRichtextFacet.Main] | None:
link_facets = [
atproto.models.AppBskyRichtextFacet.Main(
features=[
atproto.models.AppBskyRichtextFacet.Link(uri=link_data.url.strip())
],
index=atproto.models.AppBskyRichtextFacet.ByteSlice(
byteEnd=link_data.end_index,
byteStart=link_data.start_index,
),
)
for link_data in self.links
]
return link_facets or None
if __name__ == "__main__":
import json
input_str = input("Enter test string: ")
try:
test_str = json.loads(f'"{input_str}"')
except json.JSONDecodeError:
test_str = input_str
parser = ContentParser()
parser.feed(test_str)
print(parser.content) # noqa: T201
print(parser.hashtags) # noqa: T201
print(parser.links) # noqa: T201