Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/opt/local/bin/ruby
- require "xmlrpc/client"
- require 'xmlrpc/base64'
- require 'rubygems'
- module BlogBlender
- class Utils
- def self.post_template
- {
- 'title' => '',
- 'description' => '',
- 'pubDate' => Date.today.to_s,
- 'categories' => ['Blog']
- }.dup
- end
- def self.sanitize_filename(filename)
- # NOTE: File.basename doesn't work right with Windows paths on Unix
- # get only the filename, not the whole path
- filename.gsub!(/^.*(\\|\/)/, '')
- # Finally, replace all non alphanumeric, underscore or periods with underscore
- filename.gsub!(/[^\w\.\-]/, '_')
- end
- end
- class Client
- def initialize(server, urlPath, blogid, username, password)
- @client = XMLRPC::Client.new(server, urlPath)
- @blogid = blogid
- @username = username
- @password = password
- end
- def new_post(content, publish)
- @ok, @params = @client.call2('metaWeblog.newPost', @blogid, @username, @password, content, publish)
- if @ok
- system "osascript -e 'say \"Blog post was successful it was given id: #{@params}\"'"
- end
- end
- def get_post(postid)
- @ok, @params = @client.call2('metaWeblog.getPost', postid, @username, @password)
- end
- def edit_post(postid, content, publish)
- @ok, @params = @client.call2('metaWeblog.editPost', postid, @username, @password, content, publish)
- end
- # This is a modified version of a method found in the TextMate blogging bundle.
- def upload_image(file_path)
- # The packet we will be constructing
- data = {}
- full_path = file_path
- upload_name, alt = upload_name_for_path(full_path)
- data['name'] = upload_name
- data['bits'] = XMLRPC::Base64.new(IO.read(full_path))
- begin
- result = client.newMediaObject(@blogid, @username, @password, data)
- url = result['url']
- if url
- # Generate the img tag from the returned url.
- height_width = ""
- if sips_hw = %x{sips -g pixelWidth -g pixelHeight #{e_sh full_path}}
- height = $1 if sips_hw.match(/pixelHeight:[ ]*(\d+)/)
- width = $1 if sips_hw.match(/pixelWidth:[ ]*(\d+)/)
- if height && width
- height_width = %Q{ height="#{height}" width="#{width}"}
- end
- end
- print %Q{<img src="#{url}" alt="${1:#{CGI::escapeHTML alt}}"#{height_width}#{ENV['TM_XHTML']}>}
- end
- rescue XMLRPC::FaultException => e
- "puts error uploading image: #{e}.to_s"
- end
- end
- end
- end
- begin
- @blogpost = BlogBlender::Utils.post_template
- client = BlogBlender::Client.new('locusfoc.us', '/xmlrpc', '1', 'usernamegoeshere', 'passwordgoeshere')
- ARGV.each do | file |
- ext = File.extname(file)
- # upload_file_name = BlogBlender::Utils.sanitize_filename(File.basename(file))
- # puts upload_file_name
- @blogpost['title'] = File.basename(file, ext)
- @blogpost['description']= open(file) { |f| "<div style=\"white-space: pre\">#{f.read}</div>" }
- end
- unless ARGV.empty?
- client.new_post(@blogpost, true)
- else
- puts "no files specified."
- end
- rescue XMLRPC::FaultException => e
- puts "XMLRPC Error: "
- puts e.faultCode
- puts e.faultString
- rescue StandardError => e
- puts e.to_s
- end
Add Comment
Please, Sign In to add comment