Created
October 25, 2021 17:37
-
-
Save sungkhum/291d3d34f8e63b931a312c805ed19b1c to your computer and use it in GitHub Desktop.
Upload image to BitClout DeSo Diamond in Python for use in a post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jwt #pip install pyjwt | |
import os | |
import requests | |
import binascii | |
import json | |
from ecdsa import SigningKey, SECP256k1 | |
from io import BytesIO | |
''' SEEDHEX should always be kept private. It has access to your complete wallet. It's kinda like | |
seed phrase. This is why writing methods in backend isn't a good practice until we have derived keys. | |
You can only automate your own account and can't have user authorisation. It is recommended to use test account while using write methods. | |
You can find the seedHex of your account in your browser storage. Just open https://bitclout.com/ > Dev tools > Application > Storage > Local Storage > https://identity.bitclout.com > users > Select the public key with which you want to post > seedHex''' | |
SEEDHEX = 'YOURSEEDHEX' | |
#Your Public Key (Get this from your profile page) | |
PUBLIC_KEY = 'YOURPUBLICKEY' | |
image_url = 'URLOFIMAGE' | |
image_name = 'deso.png' | |
r = requests.get(image_url, stream=True) | |
if r.status_code == 200: | |
with open(image_name, 'wb') as f: | |
file_name = f.name | |
for chunk in r: | |
f.write(chunk) | |
private_key = bytes(SEEDHEX, 'utf-8') | |
private_key = binascii.unhexlify(private_key) | |
key = SigningKey.from_string(private_key, curve=SECP256k1) | |
key = key.to_pem() | |
encoded_jwt = jwt.encode({}, key, algorithm="ES256") | |
print(encoded_jwt) | |
url = "https://api.bitclout.com/api/v0/upload-image" | |
payload={'UserPublicKeyBase58Check': PUBLIC_KEY, | |
'JWT': encoded_jwt} | |
files=[ | |
('file',(image_name,open(image_name, "rb"),'image/png')) | |
] | |
response = requests.request("POST", url, data=payload, files=files) | |
print(response.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment