파이썬 beem을 이용한 팔로우/언팔로우 (custom json 예시)
파이썬 beem을 이용해서 특정 계정을 팔로우(follow)하거나 언팔로우(unfollow)하는 방법입니다. 포스팅키를 필요로 하며, custom json 오퍼레이션을 통해서 이루어집니다.
예를 들어 @joviansummer가 @jswit 계정을 팔로우한다면 아래와 같이 코드를 작성할 수 있습니다. 스팀 기준이며, 하이브나 블러트에서도 Steem 클래스 대신 Hive 또는 Blurt 클래스를 사용하면 동일하게 작업 가능할 것입니다.
import json
import beem
from beem import Steem
from beem.account import Account
from beem.transactionbuilder import TransactionBuilder
from beembase.operations import Custom_json
node_list = ["https://api.steemit.com"]
steem_id = 'joviansummer'
posting_key = '5XXXXX...'
target_id = 'jswit'
steem = Steem(node=node_list)
account = Account(steem_id, blockchain_instance=steem)
# 이미 팔로우하고 있는지 확인
following = account.get_following()
if len(following) > 0 and target_id in following:
print(target_id, ": 이미 팔로우하고 있습니다.")
exit()
else:
print(target_id, ": 팔로우하겠습니다.")
tx = TransactionBuilder(blockchain_instance=steem)
tx.appendOps(Custom_json(**{
'required_auths': [],
'required_posting_auths': [steem_id],
'id': 'follow',
'json': json.dumps(['follow', {
'follower': steem_id,
'following': target_id,
'what': ['blog']
}])
}))
tx.appendWif(posting_key)
signed_tx = tx.sign()
broadcast_tx = tx.broadcast(trx_id=True)
만약 반대로 언팔로우를 한다면, 아래와 같이 변경됩니다. tx.appendOps()에 들어가는 Custom_json() 함수의 인자 구성에서 "what" 부분이 빈 리스트([])가 됩니다. 위의 팔로우하는 코드에서는 이 부분이 ['blog']였습니다.
# 팔로우하고 있는지 확인
following = account.get_following()
if len(following) > 0 and target_id in following:
print(target_id, ": 언팔로우하겠습니다.")
else:
print(target_id, ": 팔로우하고 있지 않습니다.")
exit()
tx = TransactionBuilder(blockchain_instance=steem)
tx.appendOps(Custom_json(**{
'required_auths': [],
'required_posting_auths': [steem_id],
'id': 'follow',
'json': json.dumps(['follow', {
'follower': steem_id,
'following': target_id,
'what': []
}])
}))
tx.appendWif(posting_key)
signed_tx = tx.sign()
broadcast_tx = tx.broadcast(trx_id=True)
Account 클래스에서 follow(), unfollow() 함수를 제공하기 때문에 이것을 사용하면 더 간단할 것 같긴 합니다만, custom json 사용 예시를 기록해 두는 것이 좋을 것 같아서 좀 복잡하지만 일단은 위의 코드를 기록해 둡니다.
@joviansummer의 스팀 프로젝트
스팀 증인노드를 운영중입니다. @jswit에 증인투표해 주시면 감사하겠습니다.
(https://steemitwallet.com/~witnesses)
Upvoted! Thank you for supporting witness @jswit.
