Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nghiahsgs
Last active August 19, 2021 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nghiahsgs/b86b3bb68ffff81b6ca242e737853075 to your computer and use it in GitHub Desktop.
Save nghiahsgs/b86b3bb68ffff81b6ca242e737853075 to your computer and use it in GitHub Desktop.
auto-create-new-post-wordpress-python
import requests
import base64
import slugify
# https://github.com/WP-API/Basic-Auth => use API wpv2
url = 'http://newwp.nghiahsgs.com/wp-json/wp/v2/posts'
username = ''
password = ''
def base_64_encode(message):
# message = "admin:password"
message_bytes = message.encode('ascii')
base64_bytes = base64.b64encode(message_bytes)
base64_string = base64_bytes.decode("ascii")
return base64_string
def geneate_headers(username,password):
headers = {
'Authorization':'Basic %s'%base_64_encode('%s:%s'%(username,password)),
'Content-Type':'application/x-www-form-urlencoded',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
}
return headers
title = 'hôm nay đẹp trời và bài viết này sẽ có thumbnail'
content = '<h2>Lời bài hát Độ tộc 2</h2>'
content+='''
<iframe width="560" height="315" src="https://www.youtube.com/embed/Jk38OqdAQxc" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe>
'''
content += '<p>test</p>'
content += '<p>test2</p>'
slug = slugify.slugify(title)
headers = geneate_headers(username,password)
dataPost = {
'title':title,
'status':'publish',
'content':content,
'slug':slug,
'featured_media':7649#thumbnail
}
res = requests.post(url,data = dataPost,headers=headers).json()
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment