""" 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'