| 1 | #!/usr/bin/env ruby -Ku |
|---|
| 2 | =begin |
|---|
| 3 | Vox Photo Uploading Tool |
|---|
| 4 | |
|---|
| 5 | ~/.vox.com.yaml |
|---|
| 6 | --- |
|---|
| 7 | mail: "login@email" |
|---|
| 8 | pass: "password" |
|---|
| 9 | name: "cho45" # http://[name].vox.com/ |
|---|
| 10 | |
|---|
| 11 | License:: Creative Commons by |
|---|
| 12 | =end |
|---|
| 13 | |
|---|
| 14 | require "rubygems" |
|---|
| 15 | require "xml/libxml" |
|---|
| 16 | require "pit" |
|---|
| 17 | require "pathname" |
|---|
| 18 | require "time" |
|---|
| 19 | require "yaml" |
|---|
| 20 | require "uri" |
|---|
| 21 | require "net/http" |
|---|
| 22 | require "webrick/httputils" |
|---|
| 23 | Net::HTTP.version_1_2 |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | def dh(res) |
|---|
| 27 | puts res.body |
|---|
| 28 | res.each do |k,v| |
|---|
| 29 | puts "#{k}: #{v}" |
|---|
| 30 | end |
|---|
| 31 | p res |
|---|
| 32 | end |
|---|
| 33 | |
|---|
| 34 | # like hpricot |
|---|
| 35 | def XML(str) |
|---|
| 36 | parser = XML::Parser.new |
|---|
| 37 | parser.string = str |
|---|
| 38 | parser.parse |
|---|
| 39 | end |
|---|
| 40 | |
|---|
| 41 | config = Pit.get("vox.com", :require => { |
|---|
| 42 | "username" => "your email in Vox", |
|---|
| 43 | "password" => "your password in Vox", |
|---|
| 44 | "nickname" => "your subdomain in Vox" |
|---|
| 45 | }) |
|---|
| 46 | uri = URI("http://#{config['nickname']}.vox.com/library/posts/atom.xml") |
|---|
| 47 | |
|---|
| 48 | headers = { |
|---|
| 49 | "Authorization" => "Basic " + ["#{config["username"]}:#{config["password"]}"].pack("m"), |
|---|
| 50 | "Content-Type" => "application/atom+xml", |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | ns = [ |
|---|
| 54 | "atom:http://www.w3.org/2005/Atom", |
|---|
| 55 | "privacy:http://www.sixapart.com/ns/atom/privacy", |
|---|
| 56 | "photo:http://sixapart.com/atom/photo#", |
|---|
| 57 | "media:http://search.yahoo.com/mrss/", |
|---|
| 58 | ] |
|---|
| 59 | |
|---|
| 60 | doc = XML(Net::HTTP.get(uri.host, uri.request_uri, uri.port)) |
|---|
| 61 | |
|---|
| 62 | post_uri = URI(doc.find("/atom:feed/atom:link[@rel='service.post']", ns).first[:href]) |
|---|
| 63 | |
|---|
| 64 | puts "Uploading photo..." |
|---|
| 65 | ARGV.each do |filename| |
|---|
| 66 | file = Pathname.new(filename) |
|---|
| 67 | mime_type = WEBrick::HTTPUtils.mime_type(file.to_s, WEBrick::HTTPUtils::DefaultMimeTypes) |
|---|
| 68 | Net::HTTP.start(post_uri.host, post_uri.port) do |http| |
|---|
| 69 | res = http.post(post_uri.request_uri, <<-EOS.gsub(/^\t+/, ""), headers.merge("Slug"=>file.basename.to_s)) |
|---|
| 70 | <?xml version="1.0" ?> |
|---|
| 71 | <entry xmlns="http://www.w3.org/2005/Atom"> |
|---|
| 72 | <title>#{file.basename}</title> |
|---|
| 73 | <updated>#{Time.now.xmlschema}</updated> |
|---|
| 74 | <content mode="base64" type="#{mime_type}">#{[file.read].pack("m")}</content> |
|---|
| 75 | </entry> |
|---|
| 76 | EOS |
|---|
| 77 | if res.code == "201" |
|---|
| 78 | uri = URI(res["Location"]) |
|---|
| 79 | Net::HTTP.start(uri.host, uri.port) do |http| |
|---|
| 80 | res = http.get(uri.request_uri, headers) |
|---|
| 81 | dh res if $DEBUG |
|---|
| 82 | begin |
|---|
| 83 | doc = XML(res.body) |
|---|
| 84 | puts doc.find("/atom:entry/atom:link[@type='text/html']", ns).first[:href] |
|---|
| 85 | doc.find("/atom:entry/privacy:privacy/*", ns).each do |i| |
|---|
| 86 | puts "Publish to %s" % i[:name].to_s |
|---|
| 87 | end |
|---|
| 88 | rescue => e |
|---|
| 89 | p e |
|---|
| 90 | end |
|---|
| 91 | if $DEBUG |
|---|
| 92 | res = http.send_request("DELETE", uri.request_uri, nil, headers) |
|---|
| 93 | dh res |
|---|
| 94 | end |
|---|
| 95 | end |
|---|
| 96 | else |
|---|
| 97 | puts "Error:" |
|---|
| 98 | dh res |
|---|
| 99 | end |
|---|
| 100 | end |
|---|
| 101 | end |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | #-- error on binary upload |
|---|
| 105 | #file = Pathname.new("2150944907_5be2ee34ac.jpg") |
|---|
| 106 | #Net::HTTP.start(uri.host, uri.port) do |http| |
|---|
| 107 | # p headers.merge("Content-Type" => "image/jpeg") |
|---|
| 108 | # res = http.post(uri.request_uri, file.read, headers.merge("Content-Type" => "image/jpeg")) |
|---|
| 109 | # dh res |
|---|
| 110 | # uri = URI(res["Location"]) |
|---|
| 111 | #end |
|---|
| 112 | |
|---|