""" Create posts via wordpress API *A. Kopmann 6.2.2017 (ak)* """ from datetime import datetime import json from pprint import pprint 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 wordpress_xmlrpc.methods.taxonomies import GetTerms from config import * # Use Wordpress account - not the mysql credentials wp = Client(wp_api_url, wp_user, wp_password) # # Get category from slug name used in the configuration file # def wordpress_get_category(slug): """ Load taxonomy and search for the slug """ catlist = wp.call(GetTerms('category')) for cat in catlist: if cat.slug == slug: return cat # # 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 """ #print data['abstracts-retrieval-response'] try: coredata = data['abstracts-retrieval-response']['coredata'] authors = data['abstracts-retrieval-response']['authors']['author'] except KeyError: pprint(data) print "" print "Have not found authors in dataset" print " -> Is the connection to scopus broken???" return(0) # 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'] post.date = ts # set the name of the post different to the title post.slug = coredata['dc:identifier'] post.excerpt = authors[0]['ce:indexed-name'] if len(authors) > 2: post.excerpt += u' et al.' elif len(authors) == 2: post.excerpt += u', ' + authors[1]['ce:indexed-name'] post.excerpt += u', in ' + coredata['prism:publicationName'] + u'' if 'prism:volume' in coredata: post.excerpt += u', ' + coredata['prism:volume'] post.excerpt += u' (' + str(year).encode('utf-8') + u')' if 'prism:pageRange' in coredata: post.excerpt += u' ' + coredata['prism:pageRange'] if 'article-number' in coredata: post.excerpt += u', ' + coredata['article-number'] post.excerpt += u'.' post.content = u'

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

' post.content += u'

in ' + coredata['prism:publicationName'] + u'' if 'prism:volume' in coredata: post.content += u', ' + coredata['prism:volume'] post.content += u' (' + str(year).encode('utf-8') + u')' if 'prism:pageRange' in coredata: post.content += u' ' + coredata['prism:pageRange'] if 'article-number' in coredata: post.content += u', ' + coredata['article-number'] post.content += u'.' if 'prism:doi' in coredata: post.content += u' DOI:' + coredata['prism:doi'] 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'] post.content += u'
' if 'prism:doi' in coredata: link = u'http://dx.doi.org/' + coredata['prism:doi'] post.content += u'\n\n
Get it
' #print post.content #post.id = wp.call(NewPost(post)) # Creates a new post and returns the id! catlist = [] for slug in category: try: cat = wordpress_get_category(slug) catlist.append(cat) except: print "Slug %s not found in Wordpress" % slug exit post.terms = catlist print post.terms taglist = [] try: for tag in data['abstracts-retrieval-response']['authkeywords']['author-keyword']: try: print "Keyword: ", tag taglist.append(tag['$'].decode('utf-8','ignore')) except: print "Keyword contains special characters - droped!" pass except: # No keywords given pass post.terms_names = { 'category': ['Publications'], 'post_tag': taglist } print post.terms_names # whoops, I forgot to publish it! if len(authors) > sc_max_authors: post.post_status = 'draft' # check how to handle publication in wordpress print "Too many authors %d - set to draft" % (len(authors)) else: post.post_status = 'publish' # handled as a standard publication post.comment_status = 'closed' # allow comments - may be only for scopus # Todo: this can fail! Add proper error handling post.id = wp.call(NewPost(post)) # Creates a new post and returns the id! #wp.call(EditPost(post.id, post))# Update the before created post print post.id # 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