Skip to content

Instantly share code, notes, and snippets.

@pinei
Created December 4, 2023 23:37
Show Gist options
  • Select an option

  • Save pinei/e07ef4cbea63babe83d0d9bfa0d1f12d to your computer and use it in GitHub Desktop.

Select an option

Save pinei/e07ef4cbea63babe83d0d9bfa0d1f12d to your computer and use it in GitHub Desktop.
Bluesky Firehose Python

Bluesky Firehose

Usa o websocket do Bluesky para captura de posts em realtime

Cada post pode ser texto puro, resposta a outro post, pode conter imagens, link externo, etc.

Ref.: https://atproto.com/specs/event-stream

Tipos atribuidos a variável 'record':

  • atproto.xrpc_client.models.app.bsky.feed.post.Main
  • atproto.xrpc_client.models.dot_dict.DotDict

Trabalho futuro

  • Identificar a estrutura de dados de cada tipo de post
from atproto import CAR, AtUri
from atproto.firehose import FirehoseSubscribeReposClient, parse_subscribe_repos_message
from atproto.firehose.models import MessageFrame
from atproto.xrpc_client import models
from atproto.xrpc_client.models import get_or_create, ids, is_record_type
client = FirehoseSubscribeReposClient()
def process_commit(commit):
car = CAR.from_bytes(commit.blocks)
for op in commit.ops:
uri = AtUri.from_str(f'at://{commit.repo}/{op.path}')
if op.action == 'create':
if not op.cid:
continue
create_info = {'uri': str(uri), 'cid': str(op.cid), 'author': commit.repo}
record_raw_data = car.blocks.get(op.cid)
if not record_raw_data:
continue
record = get_or_create(record_raw_data, strict=False)
if uri.collection == ids.AppBskyFeedPost and is_record_type(record, ids.AppBskyFeedPost):
print(type(record))
print({'record': record, **create_info})
def on_message_handler(message) -> None:
commit = parse_subscribe_repos_message(message)
# we need to be sure that it's commit message with .blocks inside
if not isinstance(commit, models.ComAtprotoSyncSubscribeRepos.Commit):
return
process_commit(commit)
client.start(on_message_handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment