from flask import Flask, request
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
from wordpress_xmlrpc.exceptions import InvalidCredentialsError
app = Flask(__name__)
# WordPress登录信息
wordpress_url = 'https://kaifamiao.dev/xmlrpc.php'
wordpress_username = 'admin'
wordpress_password = '123'
category_id = 1 # 类别ID
@app.route('/')
def index():
return '''
<form method="post" action="/submit">
<textarea name="content"></textarea>
<input type="submit" value="Submit" />
</form>
'''
@app.route('/submit', methods=['POST'])
def submit():
content = request.form['content']
# 提取标题和内容
title = None
if content.startswith('#'):
lines = content.split('\n')
title = lines[0].lstrip('#').strip()
content = '\n'.join(lines[1:])
# 连接到WordPress
try:
wp = Client(wordpress_url, wordpress_username, wordpress_password)
except InvalidCredentialsError:
return 'Invalid WordPress credentials.'
# 创建新文章
post = WordPressPost()
post.title = title if title else 'New Post'
post.content = content
post.post_status = 'publish'
post.terms_names = {
'category': [category_id]
}
try:
wp.call(NewPost(post))
return 'Content submitted to WordPress successfully!'
except Exception as e:
return 'Failed to submit content to WordPress. Error: {}'.format(str(e))
if __name__ == '__main__':
app.run(port=1777)
powered by kaifamiao