แต่ก่อนอื่น เรามารู้จัก Mastodon กันก่อน
Mastodon คืออะไร?

สำหรับในประเทศไทย เครือข่ายที่นิยมกันคือ mastodon.in.th
เขียน Python เชื่อมกับ Mastodon
สำหรับการเขียนโปรแกรม Python เชื่อมกับ Mastodon นั้นสามารถทำได้ง่าย ๆ โดยใช้ Mastodon.pyMastodon.py เป็นโมดูลสำหรับเชื่อมกับ Mastodon ผ่าน API เป็น MIT License และรองรับ Python 3 เท่านั้น
สามารถติดตั้งได้ด้วยคำสั่ง
pip install Mastodon.pyสำหรับการใช้งานสามารถเริ่มได้ง่าย ๆ ตามเอกสารนี้
from mastodon import Mastodon
# Register your app! This only needs to be done once (per server, or when
# distributing rather than hosting an application, most likely per device and server).
# Uncomment the code and substitute in your information:
'''
Mastodon.create_app(
'pytooterapp',
api_base_url = 'https://mastodon.social',
to_file = 'pytooter_clientcred.secret'
)
'''
# Then, log in. This can be done every time your application starts, or you can use the persisted information:
mastodon = Mastodon(client_id = 'pytooter_clientcred.secret',)
print(mastodon.auth_request_url())
# open the URL in the browser and paste the code you get
mastodon.log_in(
code=input("Enter the OAuth authorization code: "),
to_file="pytooter_usercred.secret"
)
# To post, create an actual API instance:
mastodon = Mastodon(access_token = 'pytooter_usercred.secret')
mastodon.toot('Tooting from Python using #mastodonpy !')

0 ความคิดเห็น:
แสดงความคิดเห็น
แสดงความคิดเห็นได้ครับ :)