クイックスタート
基本
Misskey.pyのメインのクラスは以下のクラスです。
- class misskey.Misskey(address: str = 'https://misskey.io', i: Optional[str] = None, session: Optional[requests.sessions.Session] = None)[ソース]
Misskey API client class.
- Args:
address (str): Instance address. You can also include the URL protocol. If not specified, it will be automatically recognized as
https
.i (
str
, optional): Misskey API token. If you have an API token, you can assign it at instantiation.session (
requests.Session
, optional): If you have prepared therequests.Session
class yourself, you can assign it here. Normally you do not need to specify it.- Raises:
MisskeyAuthorizeFailedException: Raises if token validation fails during instantiation.
クラスをインスタンス化するために、少なくとも、Misskeyインスタンスアドレスを指定してください。
必要な場合はURLプロトコルを含めることもできます。
from misskey import Misskey
mk = Misskey("mk.example.com")
mk_local = Misskey("http://localhost:3000") # URLプロトコルを含めた場合
各メソッドにエンドポイント名を記載しています。
使用するインスタンスのMisskey APIドキュメントと併用してご確認下さい。
例: i
→ Misskey APIエンドポイント /api/i
へHTTPリクエスト
トークンを加える
Misskeyインスタンスで使用できるトークンを持っている場合は、 Misskey.pyはインスタンス化時、もしくはプロパティに代入することで使用することができます。
インスタンス化時に代入
コンストラクタに i
という引数にトークンを代入します。
from misskey import Misskey
mk = Misskey("mk.example.com", i="xxxxxxxxxx")
print(mk.i()) # あなたのプロフィールが表示されます
トークンが有効でない場合は以下の例外が出されます。
- exception misskey.exceptions.MisskeyAuthorizeFailedException[ソース]
プロパティに代入
インスタンスの token
プロパティに代入します。
from misskey import Misskey
mk = Misskey("mk.example.com")
mk.token = "xxxxxxxxxx"
print(mk.i()) # あなたのプロフィールが表示されます
トークンが有効でない場合は以下の例外が出されます。
- exception misskey.exceptions.MisskeyAuthorizeFailedException[ソース]
投稿をしてみる
トークンを含めたインスタンスで、MisskeyにNoteを投稿してみましょう。
メソッド notes_create
を使用すると、Noteを作成することができます。
- Misskey.notes_create(text: Optional[str] = None, cw: Optional[str] = None, visibility: Union[misskey.enum.NoteVisibility, str] = NoteVisibility.PUBLIC, visible_user_ids: Optional[List[str]] = None, via_mobile: bool = False, local_only: bool = False, no_extract_mentions: bool = False, no_extract_hashtags: bool = False, no_extract_emojis: bool = False, file_ids: Optional[List[str]] = None, reply_id: Optional[str] = None, renote_id: Optional[str] = None, poll_choices: Optional[Union[List[str], Tuple[str]]] = None, poll_multiple: bool = False, poll_expires_at: Optional[Union[int, datetime.datetime]] = None, poll_expired_after: Optional[Union[int, datetime.timedelta]] = None) dict [ソース]
Create a note.
- Args:
text (
str
, optional): Specify the text.cw (
str
, optional): Specify the CW(Content Warning).visibility (
str
, default:public
): Post range. Specifies the enumeration inNoteVisibility
.visible_user_ids (
list
ofstr
, optional): Ifvisibility
isspecified
, specify the user ID in the list.via_mobile (
bool
, optional): Specify whether to post from mobile. It doesn't work with recent Misskey versions.local_only (
bool
, optional): Specifies whether to post only the instance you are using.no_extract_mentions (
bool
, optional): Specifies whether to detect mentions from the text.no_extract_hashtags (
bool
, optional): Specifies whether to detect hashtags from the text.no_extract_emojis (
bool
, optional): Specifies whether to detect emojis from the text.file_ids (
list
ofstr
, optional): Specify the file ID to attach in the list.reply_id (
str
, optional): Specify the Note ID of the reply destination.renote_id (
str
, optional): Specify the Note ID to renote.poll_choices (
list
ofstr
, optional): Specify the voting item. You can specify 2 or more and 10 or less.poll_multiple (
bool
, optional): Specifies whether to allow multiple votes. This is valid only whenpoll_choices
is specified.poll_expires_at (
datetime.datetime
, optional): Specify the expiration date of the vote. If not specified, it will be indefinite. Cannot be used withpoll_expired_after
.poll_expired_after (
datetime.timedelta
, optional): Specifies the validity period of the vote. If not specified, it will be indefinite. Cannot be used withpoll_expired_at
.- Endpoint:
notes/create
- Note:
token
must be set in the instance.You must specify at least either
text
orfiles_id
.- Returns:
dict: The dict of the posted result is returned.
- Raises:
MisskeyAPIException: Raise if the API request fails.
以下がサンプルコードです。
from misskey import Misskey
mk = Misskey("mk.example.com", i="xxxxxxxxxx")
new_note = mk.notes_create(text="Hello Misskey.py!")
print(new_note["createdNote"]["id"]) # 投稿されたNoteのIDが表示されます
コードを実行するとNoteが投稿されます。ブラウザなどで確認してみてください。