-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordpress.py
53 lines (43 loc) · 1.82 KB
/
wordpress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
from wordpress_xmlrpc.methods.taxonomies import GetTerms
from wordpress_xmlrpc.methods.users import GetAuthors
def get_config(key, **kwargs):
try:
if 'default' in kwargs:
return os.environ.get(key, kwargs['default'])
else:
return os.environ[key]
except:
raise KeyError('Please set the %s environment variable' % key)
class WordPress(object):
def __init__(self):
site = get_config('WORDPRESS_SITE')
user = get_config('WORDPRESS_USER')
password = get_config('WORDPRESS_PASSWORD')
default_author = get_config('WORDPRESS_DEFAULT_AUTHOR', default=None)
self.wp = Client('http://%s/xmlrpc.php' % site, user, password)
self.authors = {a.display_name:a for a in self.wp.call(GetAuthors())}
self.default_author = self.get_author(default_author)
self.categories = [c.name for c in self.wp.call(GetTerms('category'))]
def separate_tags_and_categories(self, tags):
terms = {'category':[], 'post_tag':[]}
for tag in tags:
where = 'category' if tag in self.categories else 'post_tag'
terms[where].append(tag)
return terms
def create(self, title, content, date, author, tags, publish):
post = WordPressPost()
post.title = title
post.content = content
post.date = date
post.terms_names = self.separate_tags_and_categories(tags)
user = self.get_author(author, self.default_author)
if user:
post.user = user.id
if publish:
post.post_status = 'publish'
return self.wp.call(NewPost(post))
def get_author(self, name, default=None):
return self.authors.get(name, default)