Guest User

Untitled

a guest
Jan 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. %W{rubygems mechanize atom/pub net/http}.each { |r| require r }
  2. module Wordpress
  3. # A proof of concept class, displaying how to manage a WP blog through ruby
  4. class Blog
  5. attr_accessor :agent, :blog_uri, :username, :password, :logged_in
  6. def initialize blog_uri, username, password
  7. @username = username
  8. @password = password
  9. @blog_uri = blog_uri.gsub(/\/$/,"") # remove last slash if given
  10. @agent = Mechanize.new
  11. @agent.agent.http.retry_change_requests = true
  12. @current_page = @agent.get(blog_uri) # Will throw errors if page does not exist, or if blog_uri is invalid
  13. @logged_in = false
  14.  
  15. end
  16.  
  17. def logged_in?; @logged_in; end
  18.  
  19. def enable_remote_blogging
  20. login!
  21. page = agent.page.link_with(:text => 'Writing').click
  22. form = page.forms.first
  23. form.checkbox_with(:name => 'enable_app').check
  24. page = agent.submit(form, form.buttons.first)
  25. end
  26.  
  27. def login!
  28. unless logged_in?
  29. page = agent.get(login_uri)
  30. form = page.form('loginform')
  31. form.log = username
  32. form.pwd = password
  33. result = agent.submit(form, form.buttons.first)
  34. logged_in = true if result.link_with(:text => 'Log Out')
  35. end
  36. end
  37.  
  38. def login_uri; "#{blog_uri}/wp-login.php"; end
  39.  
  40. def post_collection_uri; "#{blog_uri}/wp-app.php/posts"; end
  41. def service_uri; "#{blog_uri}/wp-app.php/service"; end
  42. def new_post_uri; "#{blog_uri}/wp-admin/post-new.php"; end
  43.  
  44. def atom_publish_post post
  45. raise "Post cannot be nil" if post == nil
  46. raise "You can only publish valid Atom::Entry items" unless post.class == Atom::Entry
  47. Atom::Pub::Collection.new(:href => post_collection_uri).publish(post, :user => username, :pass => password)
  48. end
  49.  
  50. def publish_post hash
  51. login!
  52. page = agent.get(new_post_uri)
  53. form = page.form_with(:action => 'post.php')
  54.  
  55. # Loop all hash and set to form
  56. hash.each do |k,v|
  57. form[k.to_s] = v
  58. end
  59.  
  60. result = agent.submit(form,form.button_with(:name => 'publish'))
  61.  
  62. !!result.search("//span[@id='post-status-display']").text.match('Publish')
  63. end
  64.  
  65. def add_category opts = {}
  66. login!
  67. unless category_exists? opts
  68. page = agent.get("#{blog_uri}/wp-admin/edit-tags.php?taxonomy=category")
  69. form = page.form_with(:action => "edit-tags.php")
  70. form.send(:"tag-name",opts[:term])
  71. form.slug = opts[:scheme]
  72. form.description = opts[:description]
  73. agent.submit(form, form.buttons.first)
  74. end
  75. end
  76.  
  77. def category_exists? opts
  78. exists = false
  79. uri = URI.parse(blog_uri + '/wp-app.php/categories')
  80. content = Net::HTTP.start(uri.host) do |http|
  81. req = Net::HTTP::Get.new(uri.path)
  82. req.basic_auth username, password
  83. response = http.request(req)
  84. response.body
  85. end
  86. doc = Nokogiri::XML(content)
  87. doc.search("category").each do |cat|
  88. exists = true if cat.attr("term") == opts[:term]
  89. end
  90. exists
  91. end
  92. end
  93. end
Add Comment
Please, Sign In to add comment