flatiron-video-uploader 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f43e7133ae6b290c0162b2461ff59bc625e57a4
4
- data.tar.gz: 72f2a10f1f83d54c99645a940c6f6d970f3fe880
3
+ metadata.gz: cbf49b7dad7ca4f46930c7cee3a4e412bebeba79
4
+ data.tar.gz: a543bc4193426d29add0b8cdbd0aad803db531db
5
5
  SHA512:
6
- metadata.gz: b252d295d0356610f1922ef1dc211cb12415fa5502b3f3b86764c2519fc5f70147bc0422a857ca44951168665f2d1922d2d0d01385ae2067e2f945620e135370
7
- data.tar.gz: 7c4d2a49d288740f11c5e086e4b78f0e8565c9d3b6e9e2c166df352093fc7027fa69394172927c7c6cb78c73153dfccd1ca376c8f3f14faed55cce50bca1a066
6
+ metadata.gz: db45b8fcb6248657b56be6e8a6f0269100846a43cd31f64ffdb422e29b57d4e7edbe1b98312db71ec20dcc700c5033d1bf73506b5f5e37eed025075affe8c9a4
7
+ data.tar.gz: 81e34fefc754cac995134160804437dc3470b20932ded44e75810255b37de9f851879d44927fa30f5eaa83036ca8e3fc557bf985ceedbda86fbbf34b0b02dddd
@@ -1,3 +1,39 @@
1
+ PART_SIZE=1024*1024*50
2
+ class File
3
+ def each_part(current_part=1,part_size=PART_SIZE)
4
+ (current_part-1).times do
5
+ read(part_size)
6
+ end
7
+ yield read(part_size) until eof?
8
+ end
9
+ end
10
+
11
+ def mark_as_complete(s3,input_opts,lock_hash)
12
+ input_opts = input_opts.merge({
13
+ :upload_id => lock_hash["upload_id"]
14
+ })
15
+
16
+ parts_resp = s3.list_parts(input_opts)
17
+
18
+ input_opts = input_opts.merge(
19
+ :multipart_upload => {
20
+ :parts =>
21
+ parts_resp.parts.map do |part|
22
+ { :part_number => part.part_number,
23
+ :etag => part.etag }
24
+ end
25
+ }
26
+ )
27
+
28
+ begin
29
+ tries ||=3
30
+ mpu_complete_response = s3.complete_multipart_upload(input_opts)
31
+ return mpu_complete_response
32
+ rescue Exception => e
33
+ retry unless (tries-=1).zero?
34
+ end
35
+ end
36
+
1
37
  Faraday.default_adapter = :httpclient
2
38
  module FlatironVideoUploader
3
39
  class Runner
@@ -125,7 +161,7 @@ module FlatironVideoUploader
125
161
  end
126
162
  youtube = client.discovered_api("youtube","v3")
127
163
 
128
- s3 = Aws::S3::Resource.new(
164
+ s3 = Aws::S3::Client.new(
129
165
  # credentials: Aws::Credentials.new('AKIAJY4YSCUKNRWWASIA', 'lXp+rsDPaHcoaEay3XcjfAnE1l1W8BRWOrdLfsPH'),
130
166
  credentials: Aws::Credentials.new(token_response['amazon_s3_key'], token_response['amazon_s3_secret']),
131
167
  region: 'us-east-1'
@@ -144,33 +180,37 @@ module FlatironVideoUploader
144
180
  }
145
181
 
146
182
  puts "\n\nOK, about to upload the video. This will take a while"
147
- video_to_upload = Google::APIClient::UploadIO.new(file, 'video/*')
148
-
149
- video_to_upload.chunk_size =10000000
150
- progressbar = ProgressBar.create(:title => "Youtube Upload", :total =>video_to_upload.length)
151
- videos_insert_response = client.execute(
152
- :api_method => youtube.videos.insert,
153
- :body_object => body,
154
- :media => video_to_upload,
155
- :retries => 0,
156
- :parameters => {
157
- :uploadType => 'resumable',
158
- :part => body.keys.join(','),
159
- }
160
- )
161
- while !videos_insert_response.resumable_upload.complete? do
162
- progressbar.progress += video_to_upload.chunk_size
163
- videos_insert_response = client.execute(videos_insert_response.resumable_upload)
183
+ begin
184
+ video_to_upload = Google::APIClient::UploadIO.new(file, 'video/*')
185
+
186
+ video_to_upload.chunk_size =10000000
187
+ progressbar = ProgressBar.create(:title => "Youtube Upload", :total =>video_to_upload.length)
188
+ videos_insert_response = client.execute(
189
+ :api_method => youtube.videos.insert,
190
+ :body_object => body,
191
+ :media => video_to_upload,
192
+ :retries => 0,
193
+ :parameters => {
194
+ :uploadType => 'resumable',
195
+ :part => body.keys.join(','),
196
+ }
197
+ )
198
+ while !videos_insert_response.resumable_upload.complete? do
199
+ progressbar.progress += video_to_upload.chunk_size
200
+ videos_insert_response = client.execute(videos_insert_response.resumable_upload)
201
+ end
202
+ progressbar.finish
203
+ puts "Uploaded wooooooooo. The id is #{videos_insert_response.data.id}"
204
+ puts "\n\n"
205
+ puts "#"*30
206
+ puts "Place this in todays schedule on github\n\n\n"
207
+ command = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/#{videos_insert_response.data.id}?rel=0&modestbranding=1\" frameborder=\"0\" allowfullscreen></iframe><p><a href=\"https://www.youtube.com/watch?v=#{videos_insert_response.data.id}\">#{video_title}</a></p>"
208
+ puts command
209
+ puts "\n\n"
210
+ puts "#"*30
211
+ rescue Exception => e
212
+ exit
164
213
  end
165
- progressbar.finish
166
- puts "Uploaded wooooooooo. The id is #{videos_insert_response.data.id}"
167
- puts "\n\n"
168
- puts "#"*30
169
- puts "Place this in todays schedule on github\n\n\n"
170
- command = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/#{videos_insert_response.data.id}?rel=0&modestbranding=1\" frameborder=\"0\" allowfullscreen></iframe><p><a href=\"https://www.youtube.com/watch?v=#{videos_insert_response.data.id}\">#{video_title}</a></p>"
171
- puts command
172
- puts "\n\n"
173
- puts "#"*30
174
214
  TerminalNotifier.notify("Finished Uploading Video to Youtube. Click to copy embed ", :title => "Video Uploader", :execute => `echo '#{command}' | pbcopy`)
175
215
  # rescue Google::APIClient::TransmissionError => e
176
216
  # binding.pry
@@ -189,9 +229,113 @@ module FlatironVideoUploader
189
229
  movie_transcode_thread.join
190
230
 
191
231
  puts "\nDone transcoding. Let's upload to S3"
232
+ bucket = 'flatiron-school-learn-videos'
233
+ key = "lecture-videos/#{videos_insert_response.data.id}.mp4"
234
+ lock_file = File.open("/tmp/flatiron-uploader.lock","a+")
235
+ lock_file.sync=true
236
+ exit unless lock_file.flock( File::LOCK_NB | File::LOCK_EX )
237
+
238
+ file_name = "/tmp/transcoded.mp4"
239
+ File.open('/tmp/transcoded.mp4', 'rb') do |file|
240
+ if file.size > PART_SIZE
241
+ puts "File size over #{PART_SIZE} bytes, using multipart upload..."
242
+ input_opts = {
243
+ bucket: bucket,
244
+ key: key,
245
+ }
246
+ input_opts_with_acl = input_opts.merge({acl: "public-read"})
247
+ lock_hash = {}
248
+ total_parts = file.size.to_f / PART_SIZE
249
+ current_part=1
250
+ begin
251
+ existing_file = lock_file.read
252
+ lock_hash = JSON.parse existing_file
253
+ if lock_hash["file_name"]==file_name && lock_hash["file_size"]==file.size && lock_hash["key"]==key && lock_hash["total_parts"] == total_parts
254
+ binding.pry
255
+ current_part = lock_hash["current_part"]
256
+ else
257
+ input_opts_with_upload = input_opts.merge({
258
+ :upload_id => lock_hash["upload_id"],
259
+ :key => lock_hash["key"],
260
+ })
261
+ s3.abort_multipart_upload(input_opts_with_upload)
262
+ binding.pry
263
+ lock_file.truncate(0)
264
+ mpu_create_response = s3.create_multipart_upload(input_opts_with_acl)
265
+ lock_hash = {"total_parts"=> total_parts,
266
+ "current_part" => current_part,
267
+ "file_name" =>file_name,
268
+ "file_size" => file.size,
269
+ "key" => key,
270
+ "upload_id" =>mpu_create_response.upload_id,
271
+ }
272
+ lock_file.puts lock_hash.to_json
273
+ end
274
+ rescue StandardError => e
275
+ mpu_create_response = s3.create_multipart_upload(input_opts_with_acl)
276
+ lock_hash = {"total_parts"=> total_parts,
277
+ "current_part" => current_part,
278
+ "file_name" =>file_name,
279
+ "file_size" => file.size,
280
+ "key" => key,
281
+ "upload_id" =>mpu_create_response.upload_id,
282
+ }
283
+ lock_file.puts lock_hash.to_json
284
+ end
285
+ file.each_part(current_part) do |part|
286
+
287
+ begin
288
+ tries ||=3
289
+ part_response = s3.upload_part({
290
+ body: part,
291
+ bucket: bucket,
292
+ key: key,
293
+ part_number: current_part,
294
+ upload_id: lock_hash["upload_id"],
295
+ })
296
+
297
+ percent_complete = (current_part.to_f / total_parts.to_f) * 100
298
+ percent_complete = 100 if percent_complete > 100
299
+ percent_complete = sprintf('%.2f', percent_complete.to_f)
300
+ puts "percent complete: #{percent_complete}"
301
+ current_part = current_part + 1
302
+ lock_hash["current_part"]=current_part
303
+ lock_file.truncate(0)
304
+ lock_file.puts lock_hash.to_json
305
+ rescue Exception => e
306
+ if e.class == Interrupt
307
+ puts "LEaving"
308
+ lock_file.truncate(0)
309
+ lock_file.puts lock_hash.to_json
310
+ File.delete(lock_file.path)
311
+ exit
312
+ end
313
+ puts "trying again"
314
+ # input_opts = input_opts.merge({
315
+ # :upload_id => mpu_create_response.upload_id,
316
+ # })
317
+ lock_file.truncate(0)
318
+ lock_file.puts lock_hash.to_json
319
+
320
+ retry unless (tries-=1).zero?
321
+ puts "Couldn't get it to work, exiting"
192
322
 
193
- obj = s3.bucket('flatiron-school-learn-videos').object("lecture-videos/#{videos_insert_response.data.id}.mp4")
194
- obj.upload_file('/tmp/transcoded.mp4', acl:'public-read')
323
+ File.delete(lock_file.path)
324
+ exit
325
+ end
326
+ end
327
+
328
+ mark_as_complete(s3,input_opts,lock_hash)
329
+ File.delete(lock_file.path)
330
+ else
331
+ s3.put_object(bucket: bucket, key: key, body: file, acl: 'public-read')
332
+ end
333
+ end
334
+
335
+
336
+ # obj = s3.bucket('flatiron-school-learn-videos').object("lecture-videos/#{videos_insert_response.data.id}.mp4")
337
+ # obj.initiate_multipart_upload(options)
338
+ # obj.upload_file('/tmp/transcoded.mp4', acl:'public-read')
195
339
  raw_upload_thread.join unless raw_upload_thread.nil?
196
340
  puts "All Done!"
197
341
  TerminalNotifier.notify("All Done!", :title =>"Video Uploader")
@@ -199,3 +343,4 @@ module FlatironVideoUploader
199
343
  end
200
344
  end
201
345
  end
346
+
@@ -1,3 +1,3 @@
1
1
  module FlatironVideoUploader
2
- VERSION = "0.0.14"
2
+ VERSION = "0.0.15"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flatiron-video-uploader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Burgess
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-11 00:00:00.000000000 Z
11
+ date: 2016-02-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler