steemit API 삽질기

in #kr6 years ago

steemit API 를 이용하기 위해 진행한 삽질에 대해 공유해 드립니다.

steemit에서 제공하는 문서로만 진행을 할 경우 몇군데서 매끄럽게 진행을 하지 못하고 있는것으로 보이고 있으며, 구글링을 해도 명확하게 답을 얻지 못하고 있어, API를 이용하기 위해 삽질한 과정을 공유해 드립니다.

사용 언어는 python이며, 운영체제는 Linux (cent os)에서 진행했습니다.

기본적인 steemit API 메뉴얼은
[http://steem.readthedocs.io/en/latest/index.html]
에서 확인이 가능합니다.

매뉴얼 상으로는 python 3.6 버전을 지원하고 있습니다.

2018.03.08일 부터 python 2.7 support가 되는것으로 보이지만, 아직 몇몇 군데에서 오류가 확인되고 있으니 참고 하시기 바랍니다.

아직은 python 3.6 에서 설치를 진행하시는게 정신 건강상 이롭습니다.

virtualenv 를 이용하여 환경 구축

[anhm@localhost ~]$ python3 -m virtualenv venv
Using base prefix '/usr/local'
New python executable in /home/anhm/venv/bin/python3
Also creating executable in /home/anhm/venv/bin/python
Installing setuptools, pip, wheel...done.
[anhm@localhost ~]$ source venv/bin/activate
(venv) [anhm@localhost ~]$ which pip
~/venv/bin/pip

steem api 설치하기

(venv) [anhm@localhost ~]$ pip install steem

기본 확인하기

(venv) [anhm@localhost ~]$ steempy
Traceback (most recent call last):
  File "/home/anhm/venv/bin/steempy", line 7, in <module>
    steem.cli.legacy()
  File "/home/anhm/venv/lib/python3.6/site-packages/steem/cli.py", line 83, in legacy
    version=pkg_resources.require("steem")[0].version
  File "/home/anhm/venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 999, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/home/anhm/venv/lib/python3.6/site-packages/pkg_resources/__init__.py", line 890, in resolve
    raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.ContextualVersionConflict: (toml 0.9.3 (/home/anhm/venv/lib/python3.6/site-packages), Requirement.parse('toml==0.9.3.1'), {'steem'})

위와 같이 에러가 발생하는데, 원인은 toml의 버전이 맞지 않아서 그렇습니다.
요구하는 버전이 0.9.3.1 버전인데, pip을 버전설정을 하더라도 0.9.3.1 버전으로는 쉽게 되지 않기 때문에, 설치되어 있는 steem의 API코드를 수정하도록 하겠습니다.

(venv) [anhm@localhost ~]$ cd ~/venv/lib/python3.6/site-packages/steem-0.18.103.dist-info
(venv) [anhm@localhost steem-0.18.103.dist-info]$ vi METADATA
Requires-Dist: toml (==0.9.3.1)

해당 부분은

Requires-Dist: toml (==0.9.3)

으로 변경합니다.

이후 다시한번 확인을 해보면 에러없이 정상적으로 출력이 되는것을 확인할 수 있습니다.

(venv) [anhm@localhost ~]$ steempy
usage: steempy [-h] [--node NODE] [--no-broadcast] [--no-wallet] [--unsigned]
               [--expires EXPIRES] [--verbose VERBOSE] [--version]
               {set,config,info,changewalletpassphrase,addkey,parsewif,delkey,getkey,listkeys,listaccounts,upvote,downvote,transfer,powerup,powerdown,powerdownroute,convert,balance,interest,permissions,allow,disallow,newaccount,importaccount,updatememokey,approvewitness,disapprovewitness,sign,broadcast,orderbook,buy,sell,cancel,resteem,follow,unfollow,setprofile,delprofile,witnessupdate,witnesscreate}
               ...

기본 접속 프로그램 작성

메뉴얼에서는 steempy CLI을 이용하는 방법을 소개하고 있으나, 여기에서는 가장 기본이 되는 프로그램을 작성하여 테스트를 진행하도록 하겠습니다.

문서 상으로는
http://steem.readthedocs.io/en/latest/steem.html#quick-start]
에 해당이 됩니다.

해당 문서를 확인해 보면, 다음과 같이 기본 코드를 작성하도록 되어 있습니다.

from steem import Steem
s = Steem(keys=['<private_posting_key>', '<private_active_key>'])

이 코드상에서 가장 큰 문제는, private_posting_keyprivate_active_key 가 무엇이냐인데, 해당 정보는

https://steemit.com/@{account}/permissions

에서 확인이 가능 합니다. (지갑-권한 항목)

2018-03-11.png

이곳으로 들어오면 아래 이미지와 같은 형태로 나오게 되는데, 오른쪽 초록색 버튼을 누르면 개인키를 볼수 있습니다.

즉 각가의 항목은 다음과 같습니다.

  • private_posting_key: 첫번째 항목의 개인키
  • private_active_key: 두번째 항복의 개인키

참고로 STM으로 시작하면 공개키라고 보시면 됩니다.

그러면 다음과 같이 작성을 해주고 실행을 해보면

from steem import Steem

private_posting_key = 'P....................................................'
private_active_key = 'A...................................................'

s = Steem(keys=[private_posting_key, private_active_key])

아무 에러 없이 동작하는 것을 확인할 수 있습니다.

혹시라도 다음과 같은 에러가 발생한다면 key를 잘못된키 를 적은 상태 입니다.

Traceback (most recent call last):
  File "/home/anhm/venv/lib/python3.6/site-packages/steem/wallet.py", line 85, in setKeys
    key = PrivateKey(wif)
  File "/home/anhm/venv/lib/python3.6/site-packages/steembase/account.py", line 309, in __init__
    self._pubkeyhex, self._pubkeyuncompressedhex = self.compressedpubkey()
  File "/home/anhm/venv/lib/python3.6/site-packages/steembase/account.py", line 318, in compressedpubkey
    order = ecdsa.SigningKey.from_string(secret, curve=ecdsa.SECP256k1).curve.generator.order()
  File "/home/anhm/venv/lib/python3.6/site-packages/ecdsa/keys.py", line 149, in from_string
    assert len(string) == curve.baselen, (len(string), curve.baselen)
AssertionError: (33, 32)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "t.py", line 9, in <module>
    s = Steem(keys=[private_posting_key, private_active_key])
  File "/home/anhm/venv/lib/python3.6/site-packages/steem/steem.py", line 51, in __init__
    **kwargs
  File "/home/anhm/venv/lib/python3.6/site-packages/steem/commit.py", line 93, in __init__
    self.wallet = Wallet(self.steemd, **kwargs)
  File "/home/anhm/venv/lib/python3.6/site-packages/steem/wallet.py", line 62, in __init__
    self.setKeys(kwargs["keys"])
  File "/home/anhm/venv/lib/python3.6/site-packages/steem/wallet.py", line 87, in setKeys
    raise InvalidWifError
steembase.exceptions.InvalidWifError

매소드 호출하기

steemit의 가장 기본적인 정보인 get_account 정보를 가져올 경우 다음과 같이 코드를 작성하시면 됩니다.

from steem import Steem

private_posting_key = 'P....................................................'
private_active_key = 'A...................................................'

s = Steem(keys=[private_posting_key, private_active_key])

print(s.get_account('anhm'))

이후 작성한 코드를 실행하면 다음과 같이 에러가 발생할 수 있습니다.

(venv) [anhm@localhost ~]$ python get_account.py
WARNING:urllib3.connectionpool:Retrying (Retry(total=19, connect=None, read=None, redirect=0, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f6c8d193320>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /
WARNING:urllib3.connectionpool:Retrying (Retry(total=18, connect=None, read=None, redirect=0, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f6c8d193400>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /
WARNING:urllib3.connectionpool:Retrying (Retry(total=17, connect=None, read=None, redirect=0, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f6c8d193470>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /

해당 오류가 발생할 경우 다음과 같이 코드를 수정해 주면 됩니다.

from steem import Steem

private_posting_key = 'P....................................................'
private_active_key = 'A...................................................'

s = Steem(keys=[private_posting_key, private_active_key], nodes=['https://api.steemit.com'])

print(s.get_account('anhm'))

해당 프로그램을 실행하면 다음과 같이 정상적으로 정보를 불러오는것을 확인할 수 있습니다.

(venv) [anhm@localhost ~]$ python get_account.py
{'id': 2...

참고내용

steempy config 관련

steempy config값을 설정하기 위해서는 다음과 같이 설정을 하고 있습니다.

(venv) [anhm@localhost ~]$ steempy set default_account anhm  
(venv) [anhm@localhost ~]$ steempy config
+-----------------+-------+
| Key             | Value |
+-----------------+-------+
| default_account | anhm  |
+-----------------+-------+

하지만 설정한 항목을 지우는 옵션은 확인하지를 못했습니다.

그래서 직접 DB로 접근하여 지워주면 되는데, DB의 경로는

(venv) [anhm@localhost ~]$ sqlite3 ~/.local/share/steem/steem.sqlite
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> .schema
CREATE TABLE config (id INTEGER PRIMARY KEY AUTOINCREMENT,key STRING(256),value STRING(256));
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE keys (id INTEGER PRIMARY KEY AUTOINCREMENT,pub STRING(256),wif STRING(256));
sqlite> select * from config;
1|default_account|anhm

와 같습니다.
즉, sqlite3로 접속하여 쿼리를 이용해 주면 됩니다.

Sort:  

steempy api 쓸때 metadata나 이런거좀 수정하지 않게 업데이트가 되면 참 좋을텐데 말이죠... 저도 이렇게 에러나서 당황했었습니다 ㅋㅋㅋ

순간 먼가를 잘못한줄 알고 계속 문서만 보다가 하다 에러 메시지 보고 아... 했죠.

Coin Marketplace

STEEM 0.28
TRX 0.12
JST 0.033
BTC 66924.37
ETH 3085.49
USDT 1.00
SBD 3.71