ianklatzco / atprototools

easy-to-use and ergonomic library for interacting with bluesky
MIT License
62 stars 11 forks source link

PR to add support for images accompanying a skoot #3

Closed MostWrong closed 1 year ago

MostWrong commented 1 year ago

This is very much a WIP, and for some reason the images aren't visible even though the JSON seems correct?

https://gist.github.com/MostWrong/fe9883cf9a7dc33f5a6ace55326acaa6

MostWrong commented 1 year ago

Also, here is the format for embedding images

https://atproto.com/lexicons/app-bsky-embed#appbskyembedimages

ianklatzco commented 1 year ago

Taking a look, I see the "images aren't visible"

image

ianklatzco commented 1 year ago

Here's some working image upload code from blueskies.bsky.social:

def poast(city_list, cid, size):
    city_name = city_list[0] + ", " + city_list[1]
    poastcontent = "blue skies today in {}".format(city_name)
    timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat().replace('+00:00', 'Z')
    headers = {"Authorization": "Bearer " + ATP_AUTH_TOKEN}
    data = {
        "collection":"app.bsky.feed.post",
        "$type": "app.bsky.feed.post",
        "repo":"did:plc:n5ddwqolbjpv2czaronz6q3d",
        "record":{
            "createdAt":timestamp,
            "text":poastcontent,
            "embed": {
                "$type": "app.bsky.embed.images",
                "images": [{
                    "image":{
                        "$type":"blob",
                        "ref":{
                            "$link":cid
                        },
                        "mimeType": "image/jpeg",
                        "size":size
                    },
                    "alt":"blue sky"
                }]
            },
            }
        }
    resp = requests.post(
        ATP_HOST + "/xrpc/com.atproto.repo.createRecord",
        json=data,
        headers = headers
    )
    return 

def upload_image(img_data):
    # upload blob
    # create record

    # upload blob
    # timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat().replace('+00:00', 'Z')
    headers = {
        "Authorization": "Bearer " + ATP_AUTH_TOKEN,
        "Content-Type": "image/jpeg"
        }
    data = img_data
    resp = requests.post(
        ATP_HOST + "/xrpc/com.atproto.repo.uploadBlob",
        # "http://localhost:6969",
        data=data,
        headers = headers
    )
    cid = resp.json().get('blob').get('ref').get("$link")
    size = resp.json().get("blob").get("size")
    return (cid,size)
ianklatzco commented 1 year ago

here's a valid upload out of the network pane:

curl 'https://bsky.social/xrpc/com.atproto.repo.uploadBlob' \
  -H 'authority: bsky.social' \
  -H 'accept: */*' \
  -H 'accept-language: en-US,en;q=0.9,es;q=0.8,pl;q=0.7' \
  -H 'authorization: Bearer lol' \
  -H 'cache-control: no-cache' \
  -H 'content-type: image/jpeg' \
  -H 'dnt: 1' \
  -H 'origin: https://staging.bsky.app' \
  -H 'pragma: no-cache' \
  -H 'referer: https://staging.bsky.app/' \
  -H 'sec-ch-ua: "Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "Windows"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: cross-site' \
  -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36' \
  --data-raw "lol"

returns

{
    "blob": {
        "$type": "blob",
        "ref": {
            "$link": "bafkreihcrabxisugjyiw6zclsdrrwihllaexxahrdxt5gtmyzpwzb5gpoi"
        },
        "mimeType": "image/jpeg",
        "size": 34923
    }
}
curl 'https://bsky.social/xrpc/com.atproto.repo.createRecord' \
  -H 'authority: bsky.social' \
  -H 'accept: */*' \
  -H 'accept-language: en-US,en;q=0.9,es;q=0.8,pl;q=0.7' \
  -H 'authorization: Bearer lol' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'dnt: 1' \
  -H 'origin: https://staging.bsky.app' \
  -H 'pragma: no-cache' \
  -H 'referer: https://staging.bsky.app/' \
  -H 'sec-ch-ua: "Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "Windows"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: cross-site' \
  -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36' \
  --data-raw '{"collection":"app.bsky.feed.post","repo":"did:plc:2yk6xmkpavh4x3eicoa4bjjq","record":{"text":"testpost","embed":{"$type":"app.bsky.embed.images","images":[{"image":{"$type":"blob","ref":{"$link":"bafkreihcrabxisugjyiw6zclsdrrwihllaexxahrdxt5gtmyzpwzb5gpoi"},"mimeType":"image/jpeg","size":34923},"alt":""}]},"createdAt":"2023-04-14T07:57:59.821Z","$type":"app.bsky.feed.post"}}' \
  --compressed

response

{
    "uri": "at://did:plc:2yk6xmkpavh4x3eicoa4bjjq/app.bsky.feed.post/3jtcwtmpvry2o",
    "cid": "bafyreif2km6wkgl2ifiaegd2dpr6ypqxlebj2m3gljlzeollf6gjvsi5km"
}
MostWrong commented 1 year ago

yeah that is where I'm stuck too - apparently the upload works and returns a blob, but when I dump that into the image thing it is not displaying

ianklatzco commented 1 year ago

Merged in 52882e64344e036d21ae14b45e36c661f8cf898a ! Thank you very much ^^

ianklatzco commented 1 year ago

@MostWrong Could you send one more PR adding yourself to the "Thanks" section in the readme? I'd do it myself but I want your username to show up in the GitHub "collaborators" section ^^

MostWrong commented 1 year ago

lol sure why not