wistia-uploader 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/bin/wistia-uploader CHANGED
@@ -75,7 +75,7 @@ end.parse!
75
75
  # If there's anything left in ARGV after options parsing, assume it's a path
76
76
  # to a media file.
77
77
  if ARGV[0]
78
- options[:media_file] = ARGV[0]
78
+ options[:media_file] = ARGV
79
79
  end
80
80
 
81
81
  # Load defaults from ~/.wistia.conf, if present. Only valid loadable options
@@ -107,30 +107,72 @@ unless options.keys.to_set.superset?(required)
107
107
  end
108
108
 
109
109
  # Ok, time to upload!
110
- $stdout.write("Uploading '#{options[:media_file]}' ...") if !options[:quiet]
111
- $stdout.flush
112
110
  begin
113
- unless File.exists? options[:media_file]
114
- puts "ERROR: The file '#{options[:media_file]}' doesn't seem to exist."
111
+
112
+ if options[:media_file].is_a? Array
113
+ paths = options[:media_file]
114
+ # NOTE: Ignoring media name for multiple files!
115
+ if paths.length > 1
116
+ options[:media_name] = nil
117
+ end
118
+ else
119
+ paths = [options[:media_file]]
115
120
  end
116
121
 
117
- result = WistiaUploader.upload_media(
118
- options[:api_password], options[:project_id],
119
- options[:media_file], options[:media_name], options[:contact_id])
120
- puts " Done!" if !options[:quiet]
122
+ paths.each do |local_media|
123
+
124
+ puts "Uploading '#{local_media}'..." if !options[:quiet]
125
+ # Find the proper expansion of the path.
126
+ unless File.exists?(local_media) && !File.directory?(local_media)
127
+ puts "Detected possible shell glob. Computing paths."
128
+ paths = Dir.glob local_media
129
+ exit
130
+ end
121
131
 
122
- data = JSON.parse(result)
132
+ unless File.exists? local_media
133
+ puts "ERROR: The file '#{local_media}' doesn't seem to exist."
134
+ exit
135
+ end
123
136
 
124
- # Pretty-print the resultant media details.
125
- if !options[:quiet]
126
- puts "Media (id:#{data['id']}) successfully created."
127
- puts " bytes: #{data['filesize']}"
128
- puts " md5: #{data['md5']}"
129
- end
137
+ thread = WistiaUploader.upload_media(
138
+ options[:api_password], options[:project_id],
139
+ local_media, options[:media_name], options[:contact_id])
130
140
 
131
- # Spit out the JSON data returned from wistia.
132
- if options[:json]
133
- puts data
141
+ $stdout.sync
142
+
143
+ phases = ["-", "\\", "|", "/"]
144
+ phase = 0
145
+ while (!thread[:upload_status])
146
+ if thread[:progress] == nil
147
+ comp = 0
148
+ else
149
+ comp = (thread[:progress] * 50).round
150
+ end
151
+ if thread[:samples] && (thread[:samples].length > 0)
152
+ bps = (thread[:samples].inject{|sum,x| sum + x[0] }) / thread[:samples].length
153
+ else
154
+ bps = 0.0
155
+ end
156
+ unless options[:quiet]
157
+ $stdout.write("\r")
158
+ $stdout.write(" #{'%3d' % (2*comp)}% [#{'=' * comp}>#{' ' * (50 - comp)}] #{phases[phase]} #{thread[:last_pos]} bytes")
159
+ end
160
+ phase = (phase + 1) % 4
161
+ sleep 0.2
162
+ end
163
+ $stdout.write("\n") unless options[:quiet]
164
+
165
+ if thread[:upload_status] == :success
166
+ data = JSON.parse(thread[:body])
167
+ # Pretty-print the resultant media details.
168
+ puts " Media (id:#{data['id']}) successfully created." unless options[:quiet]
169
+ # Spit out the JSON data returned from wistia.
170
+ puts data if options[:json]
171
+ else
172
+ # Error state.
173
+ puts "An error occurred during upload!" unless options[:quiet]
174
+ exit 1
175
+ end
134
176
  end
135
177
  rescue
136
178
  puts "ERROR! Upload failed. Msg: #{$!}"
@@ -3,6 +3,21 @@ require 'net/http/post/multipart'
3
3
 
4
4
  UPLOAD_URL = "https://upload.wistia.com/"
5
5
 
6
+ # Hacktacular mostly-accurate patch for upload monitoring.
7
+ class WFile < File
8
+ def set_thread(thread)
9
+ @thread = thread
10
+ end
11
+ def read(*args)
12
+ if @thread
13
+ @thread[:progress] = ((1.0 * self.pos()) / self.size()).round(2)
14
+ @thread[:last_pos] = self.pos()
15
+ end
16
+ super(*args)
17
+ end
18
+ end
19
+
20
+
6
21
  class WistiaUploader
7
22
 
8
23
  def self.upload_media(api_pass, project, file, name=nil, contact=nil)
@@ -10,34 +25,48 @@ class WistiaUploader
10
25
  params[:contact_id] = contact if contact
11
26
  params[:name] = name if name
12
27
 
13
- code, body = self.post_file_to_wistia('', params, file)
14
- return body
28
+ return self.post_file_to_wistia('', params, file)
15
29
  end
16
30
 
17
31
  def self.post_file_to_wistia(path, data, file, timeout=nil)
18
- uri = URI(UPLOAD_URL + path)
32
+ Thread.new do
33
+ thread = Thread.current
34
+ thread[:progress] = 0.0
35
+ thread[:last_pos] = 0
19
36
 
20
- # Sanitize the params hash.
21
- data.reject! { |k,v| (v == nil) || v.empty? }
37
+ uri = URI(UPLOAD_URL + path)
22
38
 
23
- http = Net::HTTP.new(uri.host, uri.port)
24
- http.use_ssl = true if (uri.scheme == 'https')
39
+ # Sanitize the params hash.
40
+ data.reject! { |k,v| (v == nil) || v.empty? }
25
41
 
26
- # Set the connection timeouts, if specified
27
- if timeout
28
- http.ssl_timeout = 10
29
- http.open_timeout = 10
30
- http.read_timeout = 10
31
- end
42
+ http = Net::HTTP.new(uri.host, uri.port)
43
+ http.use_ssl = true if (uri.scheme == 'https')
44
+
45
+ # Set the connection timeouts, if specified
46
+ if timeout
47
+ http.ssl_timeout = 10
48
+ http.open_timeout = 10
49
+ http.read_timeout = 10
50
+ end
51
+
52
+ media = WFile.open(file)
53
+ media.set_thread(thread)
32
54
 
33
- media = File.open(file)
34
- req = Net::HTTP::Post::Multipart.new uri.request_uri, data.merge({
35
- 'file' => UploadIO.new(media, 'application/octet-stream', File.basename(file))
36
- })
37
- res = http.request(req)
38
- media.close
55
+ req = Net::HTTP::Post::Multipart.new uri.request_uri, data.merge({
56
+ 'file' => UploadIO.new(media, 'application/octet-stream', File.basename(file))
57
+ })
58
+ res = http.request(req)
59
+ media.close
39
60
 
40
- return [res.code, res.body]
61
+ thread[:code] = res.code
62
+ thread[:body] = res.body
63
+
64
+ if res.code == '200'
65
+ thread[:upload_status] = :success
66
+ else
67
+ thread[:upload_status] = :failed
68
+ end
69
+ end
41
70
  end
42
71
 
43
72
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "wistia-uploader"
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jason Lawrence"]
12
- s.date = "2012-10-10"
12
+ s.date = "2012-12-06"
13
13
  s.description = "A simple CLI uploader for Wistia users."
14
14
  s.email = "jason@wistia.com"
15
15
  s.executables = ["wistia-uploader"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wistia-uploader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-10 00:00:00.000000000 Z
12
+ date: 2012-12-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  version: '0'
144
144
  segments:
145
145
  - 0
146
- hash: -704925937519016139
146
+ hash: 3095705562316825160
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  none: false
149
149
  requirements: