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