""" Create posts via wordpress API *A. Kopmann 6.2.2017 (ak)* """ from datetime import datetime import json from wordpress_xmlrpc import Client from wordpress_xmlrpc import WordPressPost, WordPressComment from wordpress_xmlrpc.methods.posts import GetPost, NewPost, EditPost from wordpress_xmlrpc.methods.comments import NewComment, EditComment from config import * # Use Wordpress account - not the mysql credentials wp = Client(wp_api_url, wp_user, wp_password) # # query post # def wordpress_get_post(wpid): """ Query post """ try: post = wp.call(GetPost(wpid)) #print post.title ret = 1 except: #print "Post seems to be not available" ret = 0 return ret # # create a post from a scopus query # def wordpress_post_by_scopus(data, category = []): """ Create a new post based on the Scopus information """ coredata = data['abstracts-retrieval-response']['coredata'] try: authors = data['abstracts-retrieval-response']['authors']['author'] except KeyError: print "Have not found authors in dataset" print " -> Is the connection to scopus broken???" exit() # decode date tsstring = coredata['prism:coverDate'].encode('utf-8') ts = datetime.strptime(tsstring, "%Y-%m-%d").timetuple() year = ts.tm_year # Display cover date and title print("%s -- %s" % (tsstring, coredata['dc:title'])) # define post structure post = WordPressPost() post.title = coredata['dc:title'].encode('utf-8') post.date = ts # set the name of the post different to the title post.slug = coredata['dc:identifier'].encode('utf-8') post.excerpt = authors[0]['ce:indexed-name'].encode('utf-8') if len(authors) > 2: post.excerpt += " et al." elif len(authors) == 2: post.excerpt += u', ' + authors[1]['ce:indexed-name'].encode('utf-8') post.excerpt += u', in ' + coredata['prism:publicationName'].encode('utf-8') + u'' if 'prism:volume' in coredata: post.excerpt += u', ' + coredata['prism:volume'].encode('utf-8') post.excerpt += u' (' + str(year).encode('utf-8') + u')' if 'prism:pageRange' in coredata: post.excerpt += u' ' + coredata['prism:pageRange'].encode('utf-8') if 'article-number' in coredata: post.excerpt += u', ' + coredata['article-number'].encode('utf-8') post.excerpt += u'.' post.content = u'

' + authors[0]['ce:indexed-name'].encode('utf-8') authors.pop(0) if len(authors) > 20: post.content += " et al." else: for author in authors: post.content += u', ' + author['ce:indexed-name'].encode('utf-8') post.content += u'

' post.content += u'

in ' + coredata['prism:publicationName'].encode('utf-8') + u'' if 'prism:volume' in coredata: post.content += u', ' + coredata['prism:volume'].encode('utf-8') post.content += u' (' + str(year).encode('utf-8') + u')' if 'prism:pageRange' in coredata: post.content += u' ' + coredata['prism:pageRange'].encode('utf-8') if 'article-number' in coredata: post.content += u', ' + coredata['article-number'].encode('utf-8') post.content += u'.' if 'prism:doi' in coredata: post.content += u' DOI:' + coredata['prism:doi'].encode('utf-8') post.content += u'

\n\n' if 'dc:description' in coredata: post.content += u'

Abstract

' + coredata['dc:description'] if 'authkeywords' in coredata: post.content += u'\nKeywords: ' + coredata['authkeywords'].encode('utf-8') post.content += u'
' if 'prism:doi' in coredata: link = u'http://dx.doi.org/' + coredata['prism:doi'].encode('utf-8') post.content += u'\n\n
Get it
' #print post.content post.id = wp.call(NewPost(post)) # Creates a new post and returns the id! if category == '': catlist = ['Publications'] else: catlist = ['Publications'] + category post.terms_names = { 'category': catlist # defined in WP + python script } # whoops, I forgot to publish it! post.post_status = 'publish' # alternative is draft here ! post.comment_status = 'closed' # allow comments - may be only for scopus wp.call(EditPost(post.id, post))# Update the before created post # need to update the database !!! return post.id # # Create comments for all citations # def wordpress_comment_by_scopus(wpid, data): """ Create a new comment based on Scopus data """ #print "Create Wordpress comment for post %d" % wpid #print json.dumps(data,sort_keys=True,indent=4, separators=(',', ': ')) # decode date tsstring = data['prism:coverDate'].encode('utf-8') ts = datetime.strptime(tsstring, "%Y-%m-%d").timetuple() year = ts.tm_year # Display cover date and title print("%s -- %s" % (tsstring, data['dc:title'])) # Create WP comment # define post structure comment = WordPressComment() comment.id = 0 comment.content = "" if 'dc:creator' in data and data['dc:creator'] is not None: comment.content = data['dc:creator'] + 'et al.: ' if 'prism:doi' in data and data['prism:doi'] is not None: comment.content +='' + data['dc:title'] + '' else: comment.content +='' + data['dc:title'] + '' comment.content += ' in ' + data['prism:publicationName'] if 'prism:volume' in data and data['prism:volume'] is not None: comment.content += ', ' + data['prism:volume'] comment.content += ' (' + str(year).encode('utf-8') + ')' if 'prism:pageRange' in data and data['prism:pageRange'] is not None: comment.content += ' ' + data['prism:pageRange'] if 'article-number' in data and data['article-number'] is not None: comment.content += ' ' + data['article-number'] comment.content += '.' # Enable comments post = WordPressPost() postorig = wp.call(GetPost(wpid)) post.id = wpid post.date = postorig.date post.title = postorig.title post.content = postorig.content post.comment_status = 'open' # allow comments - may be only for scopus wp.call(EditPost(wpid, post)) comment.id = wp.call(NewComment(wpid, comment)) # Warning: Date can only be specified in edit command comment.date_created = ts wp.call(EditComment(comment.id, comment))# Update the before created post # Close comments for scopus posts post.comment_status = 'closed' # allow comments - may be only for scopus wp.call(EditPost(wpid, post))# Update the before created post return comment.id