ak_wordpress.py 6.6 KB

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