22
33# Dropzone Destination Info
44# Name: Posterous
5- # Description: Share stuff on Posterous.
5+ # Description: Share stuff on Posterous. Holding down option allows you to enter a post title.
66# Handles: NSFilenamesPboardType
77# Events: Clicked, Dragged
8+ # KeyModifiers: Option
89# Creator: Aptonic Software
910# URL: http://aptonic.com
1011# IconURL: http://aptonic.com/destinations/icons/posterous.png
1415require "rexml/document"
1516
1617def dragged
17- $dz. determinate ( false )
18+ $dz. determinate ( true )
1819
1920 file_path = $items[ 0 ]
21+ filename = File . basename ( file_path )
22+ title_text = ""
2023
21- $dz. begin ( "Uploading..." )
24+ if ENV [ 'KEY_MODIFIERS' ] == "Option"
25+ output = `./CocoaDialog standard-inputbox --title "Enter Title" --e --informative-text "Enter the title for your post:"`
26+ button , title_text = output . split ( "\n " )
27+
28+ if button == "2" or title_text == nil
29+ $dz. finish ( "Cancelled" )
30+ $dz. url ( false )
31+ return
32+ end
33+ end
34+
35+ $dz. begin ( "Uploading #{ filename } ..." )
36+
37+ last_output = 0
38+ is_receiving_xml = false
39+ xml_output = ""
2240
23- output = IO . popen ( "/usr/bin/curl -u #{ ENV [ 'USERNAME' ] } :#{ ENV [ 'PASSWORD' ] } -F 'media=@#{ file_path } ' http://posterous.com/api/newpost 2>&1" ) . read
41+ title_text . gsub! ( '"' , '\"' )
42+ title_text . gsub! ( '$' , '\$' )
43+ file_path = file_path . gsub ( '"' , '\"' )
44+
45+ IO . popen ( "/usr/bin/curl -# -u #{ ENV [ 'USERNAME' ] } :#{ ENV [ 'PASSWORD' ] } -F \" title=#{ title_text } \" -F 'media=@#{ file_path } ' http://posterous.com/api/newpost 2>&1 | tr -u \" \r \" \" \n \" " ) do |f |
46+ while line = f . gets do
47+ if line =~ /%/ and not is_receiving_xml
48+ line_split = line . split ( " " )
49+ file_percent_raw = line_split [ 1 ]
50+ if file_percent_raw != nil
51+ file_percent = file_percent_raw . to_i
52+ if last_output != file_percent
53+ $dz. percent ( file_percent )
54+ $dz. determinate ( false ) if file_percent == 100
55+ end
56+ last_output = file_percent
57+ end
58+ else
59+ if line =~ /xml/ or is_receiving_xml
60+ is_receiving_xml = true
61+ xml_output += line
62+ else
63+ handle_errors ( line )
64+ end
65+ end
66+ end
67+ end
2468
2569 begin
2670 url = ""
27- doc = REXML ::Document . new ( output )
71+ doc = REXML ::Document . new ( xml_output )
2872 root = doc . root
29- status = root . attributes [ "status " ]
73+ status = root . attributes [ "stat " ]
3074
3175 if status == "ok"
32- doc . elements . each ( "rsp/mediaurl" ) { |e | url = e . text }
76+ doc . elements . each ( "rsp/post/url" ) { |e | url = e . text }
3377 else
34- $dz. error ( "TwitPic Upload Error" , root . elements [ 1 ] . attributes [ "msg" ] )
78+ $dz. error ( "Posterous Upload Error" , root . elements [ 1 ] . attributes [ "msg" ] )
3579 end
3680
3781 $dz. finish ( "URL is now on clipboard" )
@@ -40,9 +84,18 @@ def dragged
4084 $dz. finish ( "Upload Failed" )
4185 $dz. url ( false )
4286 end
43-
87+ end
88+
89+ def handle_errors ( line )
90+ if line [ 0 ..4 ] == "curl:"
91+ if line [ 6 ..-1 ] =~ /Couldn't resolve/
92+ $dz. error ( "Posterous Upload Error" , "Please check your network connection." )
93+ else
94+ $dz. error ( "Posterous Upload Error" , line [ 6 ..-1 ] )
95+ end
96+ end
4497end
4598
4699def clicked
47- system ( "open http://posterous.com/ " )
100+ system ( "open http://posterous.com" )
48101end
0 commit comments