ak_wordpress.py 7.3 KB

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