สวัสดีผู้อ่านทุกท่าน วันนี้เราจะไปเขียนโปรแกรม Python เชื่อมกับ Mastodon ง่าย ๆ ด้วย Mastodon.py กันครับ
แต่ก่อนอื่น เรามารู้จัก Mastodon กันก่อน
Mastodon เป็นซอฟต์แวร์เครือข่ายสังคมออนไลน์แบบ microblogging โดยเป็น Open Source ผู้ใช้สามารถโพสต์ข้อความ เนื้อหา รูปภาพรวมถึงวิดีโอได้ แถมผู้ใช้งานบนเซิร์ฟเวอร์สามารถสื่อสารกับอีกเซิร์ฟเวอร์อื่น ๆ ได้ ไม่ต้องสมัครใหม่ เพราะ Mastodon เป็นเครือข่ายรวม (federated network) รัฐบาลไม่สามารถบล็อกเนื้อหาดังกล่าวได้
เราสามารถเลือกสมัครกับเครือข่าย (instance) ที่เราชื่นชอบได้
สำหรับในประเทศไทย เครือข่ายที่นิยมกันคือ mastodon.in.th
Mastodon.py เป็นโมดูลสำหรับเชื่อมกับ Mastodon ผ่าน API เป็น MIT License และรองรับ Python 3 เท่านั้น
สามารถติดตั้งได้ด้วยคำสั่ง
Read More
แต่ก่อนอื่น เรามารู้จัก 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 !')
