-
Notifications
You must be signed in to change notification settings - Fork 19
/
push.py
executable file
·130 lines (88 loc) · 3.4 KB
/
push.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
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
#!/usr/bin/env python
# This tool updates a given Page on your Wordpress site with
# a bibliography produced by zot_bib_web.
# insert <!--zot_bib_web COLLID1 --> into your page where you would like the
# bibliography to be inserted.
# COLLID1 is the ID (hex, 8 digits) of the top-level collection.
# All sub-collections to this will be rendered.
# To use a more complex configuration, define settings.py.
# (C) 2013, 2017 David Reitter, The Pennsylvania State University
# Released under the GNU General Public License, V.3 or later.
#############################################################################
def noop(*args,**kwargs):
pass
def push_wordpress(url, blogID, user, password, postID):
global wp_url, wp_username, wp_password, wp_blogid, post_id
wp_url = url
wp_username = user
wp_password = password
wp_blogid = blogID
post_id = postID
#############################################################################
# Configuration example
# push_wordpress(url='https://example.com/wp/xmlrpc.php', blogID=0, user='pubpushername', password='pass', postID=200)
# If no collection is given, we read from what is specified as outfile in settings.py, or fro mzotero-bib.html
try:
import __builtin__
except ImportError:
# Python 3
import builtins as __builtin__
__builtin__.shortcut = noop
__builtin__.user_collection = noop
__builtin__.group_collection = noop
__builtin__.exclude_collection = noop
__builtin__.rename_collection = noop
__builtin__.exclude_items = noop
__builtin__.push_wordpress = push_wordpress
try:
from settings import *
except ImportError:
pass
#############################################################################
import datetime, xmlrpclib
import codecs
import sys
status = 'publish'
server = xmlrpclib.ServerProxy(wp_url)
from subprocess import call
def get_bibliography (coll):
global outputfile
if coll:
outputfile = 'zotero-bib.html'
# to do: why call as a sub-process when we can just import it?
call(["./zot.py", coll, '-o', outputfile, '--div'])
if outputfile:
file = codecs.open(outputfile, "r", "utf-8")
if file:
return file.read()
return ""
# let's get the
rawpost = server.wp.getPost(wp_blogid, wp_username, wp_password, post_id)
import re
m = re.match(r'(.*)(<!--\s*zot_bib_web\s*(\w*)\s*(\w*)\s*-->)(.*)', rawpost['post_content'], re.DOTALL|re.IGNORECASE)
if m:
newpost = m.group(1) + m.group(2)
coll = m.group(3)
# catchall = m.group(4) (legacy - ignored)
contents = get_bibliography(coll)
if contents:
newpost += contents + "\n<!--zot_bib_end_of_bibliography-->"
m2 = re.match(r'.*<!--\s*zot_bib_end_of_bibliography\s*-->(.*)', m.group(5), re.DOTALL|re.IGNORECASE)
if m2:
newpost += m2.group(1)
else:
newpost += m.group(5) # all of the remainder
if rawpost['post_content'].strip() == newpost.strip():
print("Content unchanged")
else:
data = { 'post_content' : newpost}
post_id = server.wp.editPost(wp_blogid, wp_username, wp_password, post_id, data)
print "post update ",
if post_id:
print "successful."
else:
print "NOT successful."
sys.exit(1)
else:
print "No shortcode found in post %s."%post_id
print " Need <!--zot_bib_web--> or <!--zot_bib_web COLLECTIONID--> or <!--zot_bib_web COLLECTIONID -->"