123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- """ 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 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 """
-
- try:
- coredata = data['abstracts-retrieval-response']['coredata']
- 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 <em>' + coredata['prism:publicationName'].encode('utf-8') + u'</em>'
- 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'<p>' + 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'</p>'
- post.content += u'<p>in <em>' + coredata['prism:publicationName'].encode('utf-8') + u'</em>'
- 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'</p>\n\n'
- if 'dc:description' in coredata:
- post.content += u'<div class="accordion-inner"><h4>Abstract</h4>' + coredata['dc:description']
- if 'authkeywords' in coredata:
- post.content += u'\n<b>Keywords:</b> ' + coredata['authkeywords'].encode('utf-8')
- post.content += u'</div>'
- if 'prism:doi' in coredata:
- link = u'http://dx.doi.org/' + coredata['prism:doi'].encode('utf-8')
- post.content += u'\n\n<div class="accordion-inner"><a class="btn btn-primary" href="' + link + u'"><i class="icon-download icon-white"></i> Get it</a></div>'
- #print post.content
- post.id = wp.call(NewPost(post)) # Creates a new post and returns the id!
- try:
- taglist = []
- for tag in data['abstracts-retrieval-response']['idxterms']['mainterm']:
- #print tag['$'],type(tag['$'])
- taglist.append(tag['$'])
- except:
- pass
- print "Keywords:"
- print taglist
- if category == '':
- catlist = ['Publications']
- else:
- catlist = ['Publications'] + category
- post.terms_names = {
- 'post_tag': taglist,
- 'category': catlist # defined in WP + python script
- }
- # whoops, I forgot to publish it!
- #post.post_status = 'publish' # alternative is draft here !
- if (len(authors) > 25):
- post.post_status = 'draft' # alternative is draft here !
- print "Too many authors %d - might be a collaboration paper!?" % (len(authors))
- else:
- 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 +='<a href="http://dx.doi.org/' + data['prism:doi'] + '">' + data['dc:title'] + '</a>'
- else:
- comment.content +='<b>' + data['dc:title'] + '</b>'
- 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
|