|
1 | 1 | from datetime import datetime |
2 | | -from typing import Optional |
3 | 2 | import os |
4 | 3 |
|
5 | 4 | import requests |
@@ -147,36 +146,34 @@ def format_post(self, pet): |
147 | 146 | alt_text=f"Photo of {name}, a {pet.breed} available for adoption", |
148 | 147 | tags=tags, |
149 | 148 | ) |
| 149 | + |
150 | 150 | def _build_text_and_facets(self, post: Post) -> tuple[str, list]: |
151 | 151 | body = post.text |
152 | 152 | facets: list = [] |
153 | 153 | separator = "\n\n" |
154 | 154 | limit = 300 |
155 | 155 |
|
156 | | - tag_strings = [f"#{tag}" for tag in (post.tags or []) if tag] |
157 | | - if tag_strings: |
158 | | - tags_section = " ".join(tag_strings) |
159 | | - # Truncate body so the full text (body + separator + tags) fits in limit chars. |
160 | | - max_body = limit - len(separator) - len(tags_section) |
161 | | - full_text = f"{body[:max_body]}{separator}{tags_section}" |
162 | | - else: |
163 | | - full_text = body[:limit] |
| 156 | + tag_strings = [f"#{tag}" for tag in (post.tags) if tag] |
| 157 | + tags_section = " ".join(tag_strings) |
| 158 | + link_section = post.link |
| 159 | + # Truncate body so the full text (body + link + tags + separators) fits in limit chars. |
| 160 | + max_body = limit - len(separator) - len(link_section) - len(separator) - len(tags_section) |
| 161 | + full_text = f"{body[:max_body]}{separator}{link_section}{separator}{tags_section}" |
164 | 162 |
|
165 | 163 | encoded = full_text.encode("utf-8") |
166 | 164 |
|
167 | | - if post.link: |
168 | | - link_bytes = post.link.encode("utf-8") |
169 | | - link_idx = encoded.find(link_bytes) |
170 | | - if link_idx != -1: |
171 | | - facets.append({ |
172 | | - "index": { |
173 | | - "byteStart": link_idx, |
174 | | - "byteEnd": link_idx + len(link_bytes), |
175 | | - }, |
176 | | - "features": [ |
177 | | - {"$type": "app.bsky.richtext.facet#link", "uri": post.link} |
178 | | - ], |
179 | | - }) |
| 165 | + link_bytes = post.link.encode("utf-8") |
| 166 | + link_idx = encoded.find(link_bytes) |
| 167 | + if link_idx != -1: |
| 168 | + facets.append({ |
| 169 | + "index": { |
| 170 | + "byteStart": link_idx, |
| 171 | + "byteEnd": link_idx + len(link_bytes), |
| 172 | + }, |
| 173 | + "features": [ |
| 174 | + {"$type": "app.bsky.richtext.facet#link", "uri": post.link} |
| 175 | + ], |
| 176 | + }) |
180 | 177 |
|
181 | 178 | search_from = 0 |
182 | 179 | for tag_str in tag_strings: |
|
0 commit comments