root/lang/ruby/misc/vox-upload-photo-atom.rb

Revision 6307, 2.7 kB (checked in by drry, 2 years ago)

lang/ruby/misc/vox-upload-photo.rb
lang/ruby/misc/amazon_isbn/amazon_isbn.rb
lang/ruby/misc/vox-upload-photo-atom.rb: fixed typos. removed an unused lib.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env ruby -Ku
2=begin
3Vox Photo Uploading Tool
4
5~/.vox.com.yaml
6---
7mail: "login@email"
8pass: "password"
9name: "cho45" # http://[name].vox.com/
10
11License:: Creative Commons by
12=end
13
14require "rubygems"
15require "xml/libxml"
16require "pit"
17require "pathname"
18require "time"
19require "yaml"
20require "uri"
21require "net/http"
22require "webrick/httputils"
23Net::HTTP.version_1_2
24
25
26def dh(res)
27        puts res.body
28        res.each do |k,v|
29                puts "#{k}: #{v}"
30        end
31        p res
32end
33
34# like hpricot
35def XML(str)
36        parser = XML::Parser.new
37        parser.string = str
38        parser.parse
39end
40
41config = Pit.get("vox.com", :require => {
42        "username" => "your email in Vox",
43        "password" => "your password in Vox",
44        "nickname" => "your subdomain in Vox"
45})
46uri  = URI("http://#{config['nickname']}.vox.com/library/posts/atom.xml")
47
48headers = {
49        "Authorization" => "Basic " + ["#{config["username"]}:#{config["password"]}"].pack("m"),
50        "Content-Type" => "application/atom+xml",
51}
52
53ns  = [
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
60doc = XML(Net::HTTP.get(uri.host, uri.request_uri, uri.port))
61
62post_uri = URI(doc.find("/atom:feed/atom:link[@rel='service.post']", ns).first[:href])
63
64puts "Uploading photo..."
65ARGV.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
101end
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
Note: See TracBrowser for help on using the browser.