ak_wordpress.py 6.8 KB

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