Guest User

Untitled

a guest
Feb 28th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #!/opt/local/bin/ruby
  2. require "xmlrpc/client"
  3. require 'xmlrpc/base64'
  4. require 'rubygems'
  5.  
  6. module BlogBlender
  7.  
  8. class Utils
  9. def self.post_template
  10. {
  11. 'title' => '',
  12. 'description' => '',
  13. 'pubDate' => Date.today.to_s,
  14. 'categories' => ['Blog']
  15. }.dup
  16. end
  17.  
  18. def self.sanitize_filename(filename)
  19. # NOTE: File.basename doesn't work right with Windows paths on Unix
  20. # get only the filename, not the whole path
  21. filename.gsub!(/^.*(\\|\/)/, '')
  22.  
  23. # Finally, replace all non alphanumeric, underscore or periods with underscore
  24. filename.gsub!(/[^\w\.\-]/, '_')
  25. end
  26. end
  27.  
  28. class Client
  29. def initialize(server, urlPath, blogid, username, password)
  30. @client = XMLRPC::Client.new(server, urlPath)
  31. @blogid = blogid
  32. @username = username
  33. @password = password
  34. end
  35.  
  36. def new_post(content, publish)
  37. @ok, @params = @client.call2('metaWeblog.newPost', @blogid, @username, @password, content, publish)
  38. if @ok
  39. system "osascript -e 'say \"Blog post was successful it was given id: #{@params}\"'"
  40. end
  41. end
  42.  
  43. def get_post(postid)
  44. @ok, @params = @client.call2('metaWeblog.getPost', postid, @username, @password)
  45. end
  46.  
  47. def edit_post(postid, content, publish)
  48. @ok, @params = @client.call2('metaWeblog.editPost', postid, @username, @password, content, publish)
  49. end
  50.  
  51. # This is a modified version of a method found in the TextMate blogging bundle.
  52. def upload_image(file_path)
  53. # The packet we will be constructing
  54. data = {}
  55. full_path = file_path
  56. upload_name, alt = upload_name_for_path(full_path)
  57. data['name'] = upload_name
  58. data['bits'] = XMLRPC::Base64.new(IO.read(full_path))
  59.  
  60. begin
  61. result = client.newMediaObject(@blogid, @username, @password, data)
  62. url = result['url']
  63. if url
  64.  
  65. # Generate the img tag from the returned url.
  66. height_width = ""
  67. if sips_hw = %x{sips -g pixelWidth -g pixelHeight #{e_sh full_path}}
  68. height = $1 if sips_hw.match(/pixelHeight:[ ]*(\d+)/)
  69. width = $1 if sips_hw.match(/pixelWidth:[ ]*(\d+)/)
  70. if height && width
  71. height_width = %Q{ height="#{height}" width="#{width}"}
  72. end
  73. end
  74. print %Q{<img src="#{url}" alt="${1:#{CGI::escapeHTML alt}}"#{height_width}#{ENV['TM_XHTML']}>}
  75.  
  76. end
  77.  
  78. rescue XMLRPC::FaultException => e
  79. "puts error uploading image: #{e}.to_s"
  80. end
  81. end
  82. end
  83. end
  84.  
  85. begin
  86. @blogpost = BlogBlender::Utils.post_template
  87. client = BlogBlender::Client.new('locusfoc.us', '/xmlrpc', '1', 'usernamegoeshere', 'passwordgoeshere')
  88. ARGV.each do | file |
  89. ext = File.extname(file)
  90. # upload_file_name = BlogBlender::Utils.sanitize_filename(File.basename(file))
  91. # puts upload_file_name
  92. @blogpost['title'] = File.basename(file, ext)
  93. @blogpost['description']= open(file) { |f| "<div style=\"white-space: pre\">#{f.read}</div>" }
  94. end
  95.  
  96. unless ARGV.empty?
  97. client.new_post(@blogpost, true)
  98. else
  99. puts "no files specified."
  100. end
  101.  
  102. rescue XMLRPC::FaultException => e
  103. puts "XMLRPC Error: "
  104. puts e.faultCode
  105. puts e.faultString
  106. rescue StandardError => e
  107. puts e.to_s
  108. end
Add Comment
Please, Sign In to add comment