-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-post
executable file
·131 lines (111 loc) · 4 KB
/
update-post
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
# Copyright (C) 2018 Lubuntu Team
# Authored by: Simon Quigley <tsimonq2@lubuntu.me>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import yaml
from os import path
from babel import Locale
from markdown import markdown
from wordpress_xmlrpc import Client
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc.methods import taxonomies
def getdirectories(args):
directories = []
for directory in args:
if path.isdir(directory) and path.exists(directory+"/info.yaml") and path.exists(directory+"/post.md"):
directories.append(directory)
return directories
def loadwp():
user = open("user.yaml", "r")
userinfo = yaml.load(user, Loader=yaml.CSafeLoader)
wp = Client(url=userinfo["url"], username=userinfo["username"], password=userinfo["password"])
user.close()
return wp
def loadposts():
wp = loadwp()
offset = 0
increment = 10
wpposts = []
while True:
wppostlist = wp.call(posts.GetPosts({'number': increment, 'offset': offset}))
if len(wppostlist) == 0:
break
for post in wppostlist:
wpposts.append(post)
offset = offset + increment
return wpposts
def updatepost(infoyaml, md, post=None, title=None, slug=None):
wp = loadwp()
if not post:
post = WordPressPost()
post.id = wp.call(posts.NewPost(post))
if not title:
title = infoyaml["title"]
if not slug:
slug = infoyaml["slug"]
title = title.replace("POUND", "#")
if "l10n" in infoyaml:
l10nstr = ""
for lang in infoyaml["l10n"]:
try:
locale = Locale(lang)
except:
break
langname = locale.display_name
if l10nstr != "":
l10nstr = l10nstr + ", "
else:
l10nstr = "Translated into: "
l10nstr = l10nstr + "<a href=\"https://l10n.lubuntu.me/" + infoyaml["slug"] + "/" + lang + "/\">" + langname + "</a>"
message = "<!--more-->\n\n" + l10nstr
else:
message = "<!--more-->"
md = md.replace("NOTICE", message)
md = md.replace("<p>"+message+"</p>", message)
post.title = title
post.content = md
post.post_status = "publish"
tags = []
for category in wp.call(taxonomies.GetTerms('category')):
for posttype in infoyaml["type"]:
if category.name == posttype:
tags.append(category)
post.terms = tags
wp.call(posts.EditPost(post.id, post))
def main():
posts = loadposts()
for directory in getdirectories(sys.argv[1:]):
info = open(directory+"/info.yaml", "r", encoding="utf-8")
infoyaml = yaml.load(info, Loader=yaml.CSafeLoader)
info.close()
mdf = open(directory+"/post.md", "r", encoding="utf-8")
mdt = mdf.read()
md = markdown(text=mdt)
mdf.close()
post = None
for post in posts:
if post.title == infoyaml["title"].replace("POUND", "#").replace("COLON", ":"):
updatepost(post=post, title=post.title, infoyaml=infoyaml, md=md)
post = True
break
elif post.slug == infoyaml["slug"]:
updatepost(post=post, slug=post.slug, infoyaml=infoyaml, md=md)
post = True
break
if not post:
updatepost(infoyaml=infoyaml, md=md)
if __name__ == "__main__":
main()