ak_wordpress.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. """ Create posts via wordpress API
  2. *A. Kopmann 6.2.2017 (ak)*
  3. """
  4. from datetime import datetime
  5. import json
  6. from wordpress_xmlrpc import Client
  7. from wordpress_xmlrpc import WordPressPost, WordPressComment
  8. from wordpress_xmlrpc.methods.posts import GetPost, NewPost, EditPost
  9. from wordpress_xmlrpc.methods.comments import NewComment, EditComment
  10. from config import *
  11. # Use Wordpress account - not the mysql credentials
  12. wp = Client(wp_api_url, wp_user, wp_password)
  13. #
  14. # query post
  15. #
  16. def wordpress_get_post(wpid):
  17. """ Query post """
  18. try:
  19. post = wp.call(GetPost(wpid))
  20. #print post.title
  21. ret = 1
  22. except:
  23. #print "Post seems to be not available"
  24. ret = 0
  25. return ret
  26. #
  27. # create a post from a scopus query
  28. #
  29. def wordpress_post_by_scopus(data, category = []):
  30. """ Create a new post based on the Scopus information """
  31. coredata = data['abstracts-retrieval-response']['coredata']
  32. try:
  33. authors = data['abstracts-retrieval-response']['authors']['author']
  34. except KeyError:
  35. print "Have not found authors in dataset"
  36. print " -> Is the connection to scopus broken???"
  37. exit()
  38. # decode date
  39. tsstring = coredata['prism:coverDate'].encode('utf-8')
  40. ts = datetime.strptime(tsstring, "%Y-%m-%d").timetuple()
  41. year = ts.tm_year
  42. # Display cover date and title
  43. print("%s -- %s" % (tsstring, coredata['dc:title']))
  44. # define post structure
  45. post = WordPressPost()
  46. post.title = coredata['dc:title'].encode('utf-8')
  47. post.date = ts
  48. # set the name of the post different to the title
  49. post.slug = coredata['dc:identifier'].encode('utf-8')
  50. post.excerpt = authors[0]['ce:indexed-name'].encode('utf-8')
  51. if len(authors) > 2:
  52. post.excerpt += " et al."
  53. elif len(authors) == 2:
  54. post.excerpt += u', ' + authors[1]['ce:indexed-name'].encode('utf-8')
  55. post.excerpt += u', in <em>' + coredata['prism:publicationName'].encode('utf-8') + u'</em>'
  56. if 'prism:volume' in coredata:
  57. post.excerpt += u', ' + coredata['prism:volume'].encode('utf-8')
  58. post.excerpt += u' (' + str(year).encode('utf-8') + u')'
  59. if 'prism:pageRange' in coredata:
  60. post.excerpt += u' ' + coredata['prism:pageRange'].encode('utf-8')
  61. if 'article-number' in coredata:
  62. post.excerpt += u', ' + coredata['article-number'].encode('utf-8')
  63. post.excerpt += u'.'
  64. post.content = u'<p>' + authors[0]['ce:indexed-name'].encode('utf-8')
  65. authors.pop(0)
  66. if len(authors) > 20:
  67. post.content += " et al."
  68. else:
  69. for author in authors:
  70. post.content += u', ' + author['ce:indexed-name'].encode('utf-8')
  71. post.content += u'</p>'
  72. post.content += u'<p>in <em>' + coredata['prism:publicationName'].encode('utf-8') + u'</em>'
  73. if 'prism:volume' in coredata:
  74. post.content += u', ' + coredata['prism:volume'].encode('utf-8')
  75. post.content += u' (' + str(year).encode('utf-8') + u')'
  76. if 'prism:pageRange' in coredata:
  77. post.content += u' ' + coredata['prism:pageRange'].encode('utf-8')
  78. if 'article-number' in coredata:
  79. post.content += u', ' + coredata['article-number'].encode('utf-8')
  80. post.content += u'.'
  81. if 'prism:doi' in coredata:
  82. post.content += u' DOI:' + coredata['prism:doi'].encode('utf-8')
  83. post.content += u'</p>\n\n'
  84. if 'dc:description' in coredata:
  85. post.content += u'<div class="accordion-inner"><h4>Abstract</h4>' + coredata['dc:description']
  86. if 'authkeywords' in coredata:
  87. post.content += u'\n<b>Keywords:</b> ' + coredata['authkeywords'].encode('utf-8')
  88. post.content += u'</div>'
  89. if 'prism:doi' in coredata:
  90. link = u'http://dx.doi.org/' + coredata['prism:doi'].encode('utf-8')
  91. 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>'
  92. #print post.content
  93. post.id = wp.call(NewPost(post)) # Creates a new post and returns the id!
  94. if category == '':
  95. catlist = ['Publications']
  96. else:
  97. catlist = ['Publications'] + category
  98. post.terms_names = {
  99. 'category': catlist # defined in WP + python script
  100. }
  101. # whoops, I forgot to publish it!
  102. post.post_status = 'publish' # alternative is draft here !
  103. post.comment_status = 'closed' # allow comments - may be only for scopus
  104. wp.call(EditPost(post.id, post))# Update the before created post
  105. # need to update the database !!!
  106. return post.id
  107. #
  108. # Create comments for all citations
  109. #
  110. def wordpress_comment_by_scopus(wpid, data):
  111. """ Create a new comment based on Scopus data """
  112. #print "Create Wordpress comment for post %d" % wpid
  113. #print json.dumps(data,sort_keys=True,indent=4, separators=(',', ': '))
  114. # decode date
  115. tsstring = data['prism:coverDate'].encode('utf-8')
  116. ts = datetime.strptime(tsstring, "%Y-%m-%d").timetuple()
  117. year = ts.tm_year
  118. # Display cover date and title
  119. print("%s -- %s" % (tsstring, data['dc:title']))
  120. # Create WP comment
  121. # define post structure
  122. comment = WordPressComment()
  123. comment.id = 0
  124. comment.content = ""
  125. if 'dc:creator' in data and data['dc:creator'] is not None:
  126. comment.content = data['dc:creator'] + 'et al.: '
  127. if 'prism:doi' in data and data['prism:doi'] is not None:
  128. comment.content +='<a href="http://dx.doi.org/' + data['prism:doi'] + '">' + data['dc:title'] + '</a>'
  129. else:
  130. comment.content +='<b>' + data['dc:title'] + '</b>'
  131. comment.content += ' in ' + data['prism:publicationName']
  132. if 'prism:volume' in data and data['prism:volume'] is not None:
  133. comment.content += ', ' + data['prism:volume']
  134. comment.content += ' (' + str(year).encode('utf-8') + ')'
  135. if 'prism:pageRange' in data and data['prism:pageRange'] is not None:
  136. comment.content += ' ' + data['prism:pageRange']
  137. if 'article-number' in data and data['article-number'] is not None:
  138. comment.content += ' ' + data['article-number']
  139. comment.content += '.'
  140. # Enable comments
  141. post = WordPressPost()
  142. postorig = wp.call(GetPost(wpid))
  143. post.id = wpid
  144. post.date = postorig.date
  145. post.title = postorig.title
  146. post.content = postorig.content
  147. post.comment_status = 'open' # allow comments - may be only for scopus
  148. wp.call(EditPost(wpid, post))
  149. comment.id = wp.call(NewComment(wpid, comment))
  150. # Warning: Date can only be specified in edit command
  151. comment.date_created = ts
  152. wp.call(EditComment(comment.id, comment))# Update the before created post
  153. # Close comments for scopus posts
  154. post.comment_status = 'closed' # allow comments - may be only for scopus
  155. wp.call(EditPost(wpid, post))# Update the before created post
  156. return comment.id