{"id":716,"date":"2024-12-23T11:45:19","date_gmt":"2024-12-23T03:45:19","guid":{"rendered":"http:\/\/139.224.70.39\/?p=716"},"modified":"2024-12-23T11:45:19","modified_gmt":"2024-12-23T03:45:19","slug":"python%e8%87%aa%e5%8a%a8%e5%8f%91%e6%96%87%e7%ab%a0","status":"publish","type":"post","link":"http:\/\/139.224.70.39\/?p=716","title":{"rendered":"python\u81ea\u52a8\u53d1\u6587\u7ae0"},"content":{"rendered":"<h2>\u53d1\u7eaf\u6587\u5b57\u7684\u6587\u7ae0<\/h2>\n<pre class=\"prettyprint lang-py linenums:1\">from wordpress_xmlrpc import Client, WordPressPost\r\nfrom wordpress_xmlrpc.methods.posts import GetPosts, NewPost\r\nfrom wordpress_xmlrpc.methods.users import GetUserInfo\r\nimport collections\r\ncollections.Iterable=collections.abc.Iterable\r\n# \u5b98\u65b9\u6587\u6863  https:\/\/python-wordpress-xmlrpc.readthedocs.io\/en\/latest\/\r\n\r\nwp = Client('http:\/\/s.shutu.icu\/xmlrpc.php', 'name', 'password')\r\nwp.call(GetPosts())\r\nwp.call(GetUserInfo())\r\npost = WordPressPost()\r\npost.title = 'My new title'\r\npost.content = 'This is the body of my new post.'\r\npost.post_status = \"publish\"                          # \u72b6\u6001 publish\u662f\u53d1\u5e03\uff0cdraft\u662f\u8349\u7a3f\r\npost.terms_names = {\r\n    'post_tag': ['test', 'firstpost'],               # \u6807\u7b7e\uff0c\u65e0\u5219\u65b0\u589e\r\n    'category': ['Introductions', 'Tests']}          # \u5206\u7c7b\r\nwp.call(NewPost(post))<\/pre>\n<h2>\u53d1\u5e03\u56fe\u7247<\/h2>\n<pre class=\"prettyprint lang-py linenums:1\">from wordpress_xmlrpc.methods.media import UploadFile\r\nfrom wordpress_xmlrpc import Client\r\nfrom wordpress_xmlrpc import WordPressPost\r\nfrom wordpress_xmlrpc.methods.posts import NewPost\r\n \r\n# Your credentials\r\nurl = 'http:\/\/\u4f60\u7684\u57df\u540d.\u540e\u7f00\u540d\/xmlrpc.php'\r\nusername = '\u4f60\u7684wordpress\u7684\u8d26\u53f7\u540d'\r\npassword = '\u4f60\u7684wordpress\u7684\u8d26\u53f7\u5bc6\u7801'\r\n \r\nclient = Client(url, username, password)\r\n \r\n# Define your image and its properties\r\ndata = {\r\n    'name': 'big_image.jpg',\r\n    'type': 'image\/jpeg',  # mimetype\r\n}\r\n \r\n# Read the binary file and let the XMLRPC library encode it into base64\r\nwith open('big_image.jpg', 'rb') as img:\r\n    data['bits'] = img.read()\r\n \r\nresponse = client.call(UploadFile(data))\r\nattachment_id = response['id']\r\n \r\ndef send_post(title, content, attachment_id):\r\n    post = WordPressPost()\r\n    post.title = title\r\n    post.content = content\r\n    post.post_status = 'publish'\r\n    post.thumbnail = attachment_id\r\n    post_id = client.call(NewPost(post))\r\n \r\n# Call the function\r\nsend_post(&quot;Your Title&quot;, &quot;Your Content&quot;, attachment_id)<\/pre>\n<h2>\u53d1\u5e03\u591a\u56fe<\/h2>\n<pre class=\"prettyprint lang-py linenums:1\">from wordpress_xmlrpc import Client, WordPressPost\r\nfrom wordpress_xmlrpc.methods.posts import NewPost\r\nfrom wordpress_xmlrpc.methods.media import UploadFile\r\n# from wordpress_xmlrpc.compat import xmlrpc_client\r\nimport os\r\n \r\nimage_content_list=['\u56fe\u72471','\u56fe\u72472','\u56fe\u72473','\u56fe\u72474']\r\ndef create_gallery_post(images_folder, wordpress_url, wordpress_username, wordpress_password):\r\n    # \u767b\u5f55\u5230 WordPress\r\n    client = Client(wordpress_url, wordpress_username, wordpress_password)\r\n \r\n    # \u521b\u5efa WordPress \u6587\u7ae0\r\n    post = WordPressPost()\r\n    post.title = &quot;\u6279\u91cf\u53d1\u5e03\u56fe\u7247&quot;\r\n    post.content = &quot;\u7ec8\u4e8e\u89e3\u51b3\u6279\u91cf\u4e0a\u4f20\u56fe\u7247\u7684\u95ee\u9898\u4e86&quot;\r\n    post.excerpt = '\u89e3\u51b3\uff01'\r\n \r\n    # \u904d\u5386\u56fe\u7247\u6587\u4ef6\u5939\r\n    kk=0\r\n    for filename in os.listdir(images_folder):\r\n        if filename.endswith(&quot;.jpg&quot;) or filename.endswith(&quot;.jpeg&quot;) or filename.endswith(&quot;.png&quot;):\r\n            image_path = os.path.join(images_folder, filename)\r\n \r\n            # \u4e0a\u4f20\u56fe\u7247\u5230 WordPress\r\n            data = {\r\n                'name': filename,\r\n                'type': 'image\/jpeg',  # \u6216\u8005\u5176\u4ed6\u56fe\u7247\u683c\u5f0f\r\n            }\r\n            with open(image_path, 'rb') as img:\r\n                data['bits'] = img.read()\r\n                response = client.call(UploadFile(data))\r\n                # response = client.call(client.get_method('wp.uploadFile'), data, img)\r\n                attachment_id = response['id']\r\n                img_url = response['url']\r\n            print(attachment_id)\r\n            print(img_url)\r\n            print(type(attachment_id))#&lt;class 'str'&gt;\r\n \r\n            # \u6dfb\u52a0\u56fe\u7247\u5185\u5bb9\u548c\u56fe\u7247url\u5230\u6587\u7ae0\u5185\u5bb9\u4e2d\r\n            post.content +=image_content_list[kk]+'\\n'+f'&lt;img src=&quot;{img_url}&quot; alt=&quot;{filename}&quot; \/&gt;'\r\n            kk=kk+1\r\n \r\n    # \u53d1\u5e03\u56fe\u5e93\u6587\u7ae0\u5230 WordPress\r\n    post.post_status = 'publish'\r\n    client.call(NewPost(post))\r\n \r\n    print(&quot;\u56fe\u5e93\u6587\u7ae0\u53d1\u5e03\u6210\u529f\uff01&quot;)\r\n \r\n# \u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\r\nimages_folder = '.\/jpg'  # \u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\r\n \r\n# WordPress \u7ad9\u70b9\u4fe1\u606f\r\nwordpress_url = 'http:\/\/\u4f60\u7684\u57df\u540d.\u540e\u7f00\u540d\/xmlrpc.php'\r\nwordpress_username = '\u4f60\u7684wordpress\u7684\u8d26\u53f7\u540d'\r\nwordpress_password = '\u4f60\u7684wordpress\u7684\u8d26\u53f7\u5bc6\u7801'\r\n \r\n# \u521b\u5efa\u56fe\u5e93\u6587\u7ae0\uff08\u542b\u591a\u5f20\u56fe\u7247\u7684\u6587\u7ae0\uff09\u5e76\u53d1\u5e03\u5230 WordPress\r\ncreate_gallery_post(images_folder, wordpress_url, wordpress_username, wordpress_password)\r\n <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53d1\u7eaf\u6587\u5b57\u7684\u6587\u7ae0 from wordpress_xmlrpc import Client, WordPressP&#8230;<\/p>\n<div class=\"read-more-link\"><a href=\"http:\/\/139.224.70.39\/?p=716\">Read More<\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13,7],"tags":[],"class_list":["post-716","post","type-post","status-publish","format-standard","hentry","category-python","category-wordpress"],"acf":[],"_links":{"self":[{"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/posts\/716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.224.70.39\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=716"}],"version-history":[{"count":2,"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions"}],"predecessor-version":[{"id":718,"href":"http:\/\/139.224.70.39\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions\/718"}],"wp:attachment":[{"href":"http:\/\/139.224.70.39\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.224.70.39\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=716"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.224.70.39\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}