13 มีนาคม 2558

Published มีนาคม 13, 2558 by with 0 comment

โพสต์ลง Wordpress ด้วย XML-RPC ในภาษา Python

สวัสดีผู้อ่านทุกท่านครับ ผมเชื่อว่ามีบางท่านที่ใช้ Wordpress ในการทำบล็อกและเว็บไซต์ เหมือนผมตอนนี้ที่ทำบล็อก Python 3 นี้ด้วย Wordpress แล้วมีคำถามเกิดขึ้นว่า เราจะสามารถโพสต์ลงหรือจัดการโพสต์ Wordpress ด้วยภาษา Python ได้ไหม คำตอบคือ มีคนทำไลบรารีนี้สำหรับนักพัฒนาในภาษา Python แล้วครับ

ไลบรารีนี้มีชื่อว่า python-wordpress-xmlrpc

  • รองรับทั้ง Python 2 และ Python 3

  • ใช้ XML-RPC ของ Wordpress ในการเข้าถึงข้อมูล

  • License: BSD

  • เข้ากันได้กับ WordPress 3.4 ขึ้นไป


ในการติดตั้งสามารถใช้คำสั่ง pip ในการติดตั้งได้ดังนี้
pip install python-wordpress-xmlrpc

หรือจะใช้ easy_install ในการติดตั้งด้วยคำสั่ง
easy_install python-wordpress-xmlrpc

ตัวอย่างการใช้งานเบื้องต้น

[python]
>>> from wordpress_xmlrpc import Client, WordPressPost
>>> from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
>>> from wordpress_xmlrpc.methods.users import GetUserInfo

>>> wp = Client('http://mysite.wordpress.com/xmlrpc.php', 'username', 'password')
>>> wp.call(GetPosts())
[<WordPressPost: hello-world (id=1)>]

>>> wp.call(GetUserInfo())
<WordPressUser: max>

>>> post = WordPressPost()
>>> post.title = 'My new title'
>>> post.content = 'This is the body of my new post.'
>>> post.terms_names = {
>>> 'post_tag': ['test', 'firstpost'],
>>> 'category': ['Introductions', 'Tests']
>>> }
>>> wp.call(NewPost(post))
5
[/python]

อ่านเอกสารการใช้งานได้ที่ http://python-wordpress-xmlrpc.readthedocs.org/en/latest/index.html
ติดตามบทความต่อไปนะครับ
ขอบคุณครับ

0 ความคิดเห็น:

แสดงความคิดเห็น

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