root/publicize-posts/publicize-posts.rb

Revision 1502, 2.8 kB (checked in by jm3, 1 year ago)

user agent and socket error

  • Property svn:executable set to *
Line 
1 #!/usr/local/bin/ruby
2
3 # open source ruby program to publicize your [imported] delicious links
4 #   by jm3 / john manoogian III / jm3@jm3.net
5 #
6 # usage: export your delicious links, run the cleanup script > out, then run this
7
8 # TODO:
9 # make user name and password command line arguments
10 # port the preprocessing shell script code to ruby so this can all just be one file
11
12 require 'net/https'
13 require 'optparse'
14 require 'cgi'
15
16 # change these:
17 API_USERNAME = "you"         
18 API_PASSWORD = "secret"
19 DEFAULT_TAG = "stuff" # superstitiously applied to links found without tags
20
21 DELAY_BETWEEN_POSTS = 2 # in seconds
22 API_ENDPOINT = "api.del.icio.us"
23
24 # unix -> ISO 8601 dates
25 def convert_date( d )
26   t = Time.at( d.to_i )
27   return t.strftime( "%Y-%m-%dT%H:%M:%SZ" )
28 end
29
30 # remove old tag shit; you probably don't need this if you're not me
31 def convert_tags( tags )
32   tags = tags.gsub( /,/, ' ')
33   tags = tags.gsub /(import2|imported)/, ''
34   tags = tags.gsub /^ /, ''
35   tags = tags.gsub /\s+/, ' '
36   tags = tags.gsub /^$/, DEFAULT_TAG
37 end
38
39 def submit_post( post_args )
40   return if ! post_args
41
42   resp = href = "";
43   begin     
44     https = Net::HTTP.new( API_ENDPOINT, 443)
45     https.use_ssl = true
46     https.verify_mode = OpenSSL::SSL::VERIFY_NONE
47     https.start do |http|
48       req = Net::HTTP::Get.new("/v1/posts/add?" + post_args,
49         { "User-Agent" => "jm3/ruby_link_publicizer" })
50       req.basic_auth(API_USERNAME, API_PASSWORD)
51       response = https.request(req)
52       resp = response.body
53     end     
54     if resp =~ /something went wrong/ || ! ( resp =~ /result code="done"/ )
55       puts "error publicizing as user: " + API_USERNAME + ",\n" + post_args
56       exit
57     end
58   rescue SocketError
59     raise "socket error and such!"
60   end
61 end
62
63 def build_post_request_args( url, description, date, tags, note )
64   return if ! url
65
66   puts "publicizing: " + description # output for debugging / status
67
68   url = CGI::escape( url )
69   description = CGI::escape( description )
70   tags = CGI::escape( tags )
71   note = note ? CGI::escape( note ) : nil
72
73   return "url=" + url + "&description=" + description + (note ? "&extended=" + note : "") + "&tags=" + tags + "&dt=" + date + "&replace=yes" + "&shared=yes"
74 end
75
76 def publicize_links( export_file )
77   return if ! export_file
78   File.open( export_file ).grep /post/ do |line|
79
80     if line =~ /href="([^"]+)" add_date="(\d+)" tags="([^"]+)">([^<]+)</
81       url = "#$1"
82       date = convert_date( "#$2" )
83       tags = convert_tags( "#$3" )
84       description = "#$4"
85     end
86
87     if line =~ /<note>(.*)/
88       note = "#$1"
89     end
90
91     submit_post( build_post_request_args( url, description, date, tags, note ))
92     sleep DELAY_BETWEEN_POSTS
93
94   end
95 end
96
97 if ! ARGV[0] or ! FileTest.file?( ARGV[0] )
98   puts "usage: publicize_links.rb YOUR_EXPORTED_LINKS"
99   exit
100 end
101
102 publicize_links ARGV[0]
103
Note: See TracBrowser for help on using the browser.