social media crossposting tool. 3rd time's the charm
mastodon misskey crossposting bluesky

chore: clean-up bsky output

zenfyr.dev f2b23c64 a7238685

verified
+31 -37
+11
atproto/models.py
··· 1 + import json 1 2 from abc import ABC, abstractmethod 2 3 from dataclasses import dataclass, field 3 4 from typing import Any ··· 5 6 6 7 URI = "at://" 7 8 URI_LEN = len(URI) 9 + 10 + 11 + def cid_from_json(data: str | None) -> str | None: 12 + if not data: 13 + return None 14 + 15 + try: 16 + return str(json.loads(data)["cid"]) 17 + except (json.JSONDecodeError, AttributeError, KeyError): 18 + return None 8 19 9 20 10 21 class AtUri:
+20 -37
bluesky/output.py
··· 14 14 SelfLabel, 15 15 SelfLabels, 16 16 StrongRef, 17 + cid_from_json, 17 18 ) 18 19 from atproto.store import get_store 19 20 from bluesky.client import BlueskyClient ··· 224 225 self.log.error("Skipping '%s': failed to fetch parent posts from db") 225 226 return 226 227 227 - try: 228 - root_cid_data = root_post["extra_data"] 229 - root_cid = ( 230 - json.loads(root_cid_data).get("cid", "") if root_cid_data else "" 231 - ) 232 - reply_cid_data = reply_post["extra_data"] 233 - reply_cid = ( 234 - json.loads(reply_cid_data).get("cid", "") if reply_cid_data else "" 235 - ) 236 - except (json.JSONDecodeError, AttributeError, KeyError): 228 + root_cid = cid_from_json(root_post["extra_data"]) 229 + reply_cid = cid_from_json(reply_post["extra_data"]) 230 + 231 + if not root_cid or not reply_cid: 237 232 self.log.error("Skipping '%s': failed to parse CID from db") 238 233 return 239 234 ··· 318 313 ) 319 314 if not quoted_post: 320 315 self.log.error("Skipping '%s': quoted post not found in db!") 321 - else: 322 - quoted_mappings = self._get_mappings( 323 - quoted_post["id"], self.url, self.did 324 - ) 325 - if not quoted_mappings: 326 - self.log.error( 327 - "Skipping '%s': failed to find mappings for quoted post" 328 - ) 329 - else: 330 - bluesky_quoted_post = self._get_post( 331 - self.url, self.did, quoted_mappings[0]["identifier"] 332 - ) 333 - if not bluesky_quoted_post: 334 - self.log.error( 335 - "Skipping '%s': Failed to find Bluesky quoted post!" 336 - ) 337 - else: 338 - quoted_cid_data = bluesky_quoted_post["extra_data"] 339 - quoted_cid = ( 340 - json.loads(quoted_cid_data).get("cid", "") 341 - if quoted_cid_data 342 - else "" 343 - ) 344 - quoted_uri = quoted_mappings[0]["identifier"] 316 + return 317 + 318 + quoted_mappings = self._get_mappings(quoted_post["id"], self.url, self.did) 319 + if not quoted_mappings: 320 + self.log.error("Skipping '%s': failed to find mappings for quoted post") 321 + return 322 + 323 + quoted_cid = cid_from_json(quoted_mappings[0]["extra_data"]) 324 + if not quoted_cid: 325 + self.log.error("Skipping '%s': failed to parse CID from db") 326 + return 327 + 328 + quoted_uri = quoted_mappings[0]["identifier"] 345 329 346 330 splitter = TokenSplitter(max_chars=300, max_link_len=30) 347 331 token_blocks = splitter.split(tokens) ··· 583 567 if not mappings: 584 568 return 585 569 586 - try: 587 - cid = json.loads(mappings[0]["extra_data"])["cid"] 588 - except (json.JSONDecodeError, AttributeError, KeyError): 570 + cid = cid_from_json(mappings[0]["extra_data"]) 571 + if not cid: 589 572 self.log.exception("Skipping '%s': failed to parse CID from extra_data") 590 573 return 591 574