やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

Mastodon.pyを使ってみる

Mastodon APIPythonラッパ。

成果物

GitHubMastodon.py.201706081443

開発環境

参考

インストール

$ pip install Mastodon.py
Collecting Mastodon.py
  Downloading Mastodon.py-1.0.7-py2.py3-none-any.whl
Requirement already satisfied: requests in /.../root/tools/pyenv/3.6.1/venv/webapi/lib/python3.6/site-packages (from Mastodon.py)
Collecting six (from Mastodon.py)
  Using cached six-1.10.0-py2.py3-none-any.whl
Collecting dateutils (from Mastodon.py)
  Downloading dateutils-0.6.6.tar.gz
Requirement already satisfied: idna<2.6,>=2.5 in /.../root/tools/pyenv/3.6.1/venv/webapi/lib/python3.6/site-packages (from requests->Mastodon.py)
Requirement already satisfied: urllib3<1.22,>=1.21.1 in /.../root/tools/pyenv/3.6.1/venv/webapi/lib/python3.6/site-packages (from requests->Mastodon.py)
Requirement already satisfied: certifi>=2017.4.17 in /.../root/tools/pyenv/3.6.1/venv/webapi/lib/python3.6/site-packages (from requests->Mastodon.py)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /.../root/tools/pyenv/3.6.1/venv/webapi/lib/python3.6/site-packages (from requests->Mastodon.py)
Collecting argparse (from dateutils->Mastodon.py)
  Downloading argparse-1.4.0-py2.py3-none-any.whl
Collecting python-dateutil (from dateutils->Mastodon.py)
  Using cached python_dateutil-2.6.0-py2.py3-none-any.whl
Collecting pytz (from dateutils->Mastodon.py)
  Using cached pytz-2017.2-py2.py3-none-any.whl
Installing collected packages: six, argparse, python-dateutil, pytz, dateutils, Mastodon.py
  Running setup.py install for dateutils ... done
Successfully installed Mastodon.py-1.0.7 argparse-1.4.0 dateutils-0.6.6 python-dateutil-2.6.0 pytz-2017.2 six-1.10.0

コード

ClientIdとToken取得

import os.path
from mastodon import Mastodon

domain = "mstdn.jp"
api_base_url = "https://{0}".format(domain)
file_client = "./client/client_{0}_0.txt".format(domain)
file_token = "./client/token_{0}_0.txt".format(domain)
mail_address = "メールアドレス"
password = "パスワード"

if not os.path.isfile(file_client):
    Mastodon.create_app("test_app",
                        api_base_url = api_base_url,
                        to_file = file_client)
if not os.path.isfile(file_token):
    mastodon = Mastodon(
        client_id = file_client,
        api_base_url = api_base_url)
    mastodon.log_in(
        mail_address,
        password,
        to_file = file_token)

Tootしてみる

import os.path
from mastodon import Mastodon

domain = "mstdn.jp"
api_base_url = "https://{0}".format(domain)
file_client = "./client/client_{0}_0.txt".format(domain)
file_token = "./client/token_{0}_0.txt".format(domain)

mastodon = Mastodon(
    client_id=file_client,
    access_token=file_token,
    api_base_url=api_base_url)
mastodon.toot("""Mastodon.py で #Mastodon #API を 実行してみる。
http://qiita.com/ignis_fatuus/items/f4b7213ad887af2cf71c""")

所感

インストールが思いの外色々あったのが気になる。