[개발이야기#050] 스팀잇에 파일 업로드 하는 소스코드

안녕하세요 가야태자 @talkit 입니다.

7년전 소스코드가 동작을 합니다.

imagehoster-python-client/imagehoster at master · emre/imagehoster-python-client

이미지를 자동으로 업로드 하고 싶은게 제 생각이었고, 해당 내용은 위코드 입니다.

위코드가 그런데

image (1409×165)

위 그림에서 보시듯이 6년 전 소스 코드입니다.

그런데 잘 됩니다.

위 이미지가 제가 테스트해본 이미지 입니다. ^^

(steemit) C:\dev\pythonWorkspace\steemit>python image_upload.py File uploaded: https://cdn.steemitimages.com/DQmaeDdW6WwfTxmeGJoPfW1iPFmnqU8yoTzLUfznjMUhYBh/image

제가 또 전에 말씀 드렸듯이 python은 초보 입니다. 그래서 조금 변경 했습니다.

소스코드

``` import hashlib

import os

import struct

from binascii import hexlify

import ecdsa

import requests

from steem.commit import PrivateKey

class ImageUploader:

API_BASE_URL = "https://steemitimages.com"

CHALLENGE = b"ImageSigningChallenge"

def init(self, username, posting_wif):

self.username = username

self.posting_wif = posting_wif

def checksum(self, filename):

"""

This function creates a binary sha256 digest with the

image concatenated with the imageSigningChallenge.

"""

sha256 = hashlib.sha256()

image_data = open(filename, 'rb').read()

sha256.update(self.CHALLENGE)

sha256.update(image_data)

return sha256.digest(), image_data

def upload(self, image_path, image_name=None):

private_key = PrivateKey(self.posting_wif)

sk = ecdsa.SigningKey.from_string(bytes(private_key), curve=ecdsa.SECP256k1)

digest, image_content = self.checksum(image_path)

# 서명 및 Hex 변환

signature = sk.sign_digest(digest)

signature_in_hex = hexlify(

struct.pack("<B", 31) + signature

).decode("ascii")

# 디버깅 출력

print("Digest (hex):", digest.hex())

print("Signature (hex):", signature_in_hex)

files = {image_name or 'image': image_content}

url = "%s/%s/%s" % (

self.API_BASE_URL,

self.username,

signature_in_hex

)

print("Upload URL:", url)

r = requests.post(url, files=files)

return r.json()

def main():

# 사용자 정보와 업로드할 이미지 파일 경로

username = "your_userid"  # 스팀잇 사용자명

posting_wif = "your_postingkey"  # 스팀잇 포스팅 WIF 키

image_path = "your_image_file_path"  # 업로드할 이미지 파일 경로

# ImageUploader 객체 생성

image_uploader = ImageUploader(username, posting_wif)

# 이미지 업로드

resp = image_uploader.upload(image_path)

# 업로드 결과 출력

if 'url' in resp:

print("File uploaded: %s" % resp["url"])

else:

print("Error:", resp)

if name == 'main':

main() ```

사용방법

제 블로그를 따라하셨으면 steemit이라는 가상환경이 있을 겁니다.

해당 가상환경을 켜십시오.

conda activate steemit

위 파일을 저는 image_upload.py 로 저장 했습니다.

이미지 파일도 test.png로 만드시고 ^^

위 코드 중에 ```     username = "your_userid"  # 스팀잇 사용자명

posting_wif = "your_postingkey"  # 스팀잇 포스팅 WIF 키

image_path = "your_image_file_path"  # 업로드할 이미지 파일 경로 ```

위 부분을 자신의 코드에 맞게 수정 하시구요.

python image_upload.py

위와 같이 실행하면 끝입니다.

(steemit) C:\dev\pythonWorkspace\steemit>python image_upload.py File uploaded: https://cdn.steemitimages.com/DQmaeDdW6WwfTxmeGJoPfW1iPFmnqU8yoTzLUfznjMUhYBh/image

위와 같이 결과로 뱉어 주는데 저 뱉어 주는 URL을 가저와서 이미지와 치환을 하면 앞으로 이미지는 스팀잇에 올리고 md는 옵시디안에 넣는 방법으로 가능할 것 같습니다.

@anpigon 님 Obsidian 플러그인은 어떻게 만드나요 ^^

그런데 옵시디안이 nodejs라서 안될수도 있을 것 같네요 T.T

감사합니다.



Posted through the ECblog app (https://blog.etain.club)

댓글

이 블로그의 인기 게시물

[개발이야기#047] 인공지능 이야기 내글을 요약해서 다른곳에 보내 보자.

[개발이야기#053] SubVersion(SVN)을 사용하는 방법 - 호스팅 방법

[개발이야기#055] SubVersion(SVN)을 사용하는 방법 - SVN 용어 설명