streamio-cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
File without changes
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Streamio AB
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Streamio::Exporter
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'streamio-exporter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install streamio-exporter
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/bin/streamio ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'rubygems'
5
+ gem 'streamio-cli'
6
+ rescue LoadError
7
+ end
8
+
9
+ require 'streamio-cli'
@@ -0,0 +1,133 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'fileutils'
4
+ require 'thor'
5
+ require 'streamio'
6
+ require 'ruby-progressbar'
7
+ require 'streamio-cli/version'
8
+
9
+ module Streamio
10
+ class SlowDownloadError < StandardError
11
+ end
12
+
13
+ class CLI < Thor
14
+ desc "export", "export data from target account"
15
+ method_option :username, :desc => 'api username', :aliases => '-u', :required => true
16
+ method_option :password, :desc => 'api password', :aliases => '-p', :required => true
17
+ method_option :include_transcodings, :aliases => '-i', :type => :boolean
18
+ def export
19
+ configure_streamio_gem
20
+ download_videos
21
+ download_audios
22
+ rescue SocketError
23
+ puts "[ERROR]"
24
+ puts "Could not connect to the internet, please check your connection and try again."
25
+ exit
26
+ end
27
+
28
+ private
29
+ def configure_streamio_gem
30
+ Streamio.configure do |c|
31
+ c.username = options[:username]
32
+ c.password = options[:password]
33
+ end
34
+ end
35
+
36
+ def download_videos
37
+ number_of_items = Video.count
38
+ current_item = 0
39
+ requests_needed = (number_of_items / 100) + 1
40
+
41
+ requests_needed.times do |i|
42
+ Video.all(:skip => i * 100, :limit => 100).each do |video|
43
+ current_item += 1
44
+ puts "\nVideo #{current_item} / #{number_of_items} - #{video.title} - #{video.id}"
45
+
46
+ path = FileUtils.mkdir_p("streamio-export/videos/#{video.id}").first
47
+ progress_bar_title = "Original (#{bytes_to_megabytes(video.original_video['size'])})"
48
+ File.open("#{path}/#{video.id}.json", "w:utf-8") { |file| file.write(video.attributes.to_json) }
49
+ download("http://#{video.original_video['http_uri']}", path, progress_bar_title)
50
+
51
+ next unless options[:include_transcodings]
52
+
53
+ video.transcodings.each do |transcoding|
54
+ progress_bar_title = "#{transcoding['title']} (#{bytes_to_megabytes(transcoding['size'])})"
55
+ download("http://#{transcoding['http_uri']}", path, progress_bar_title)
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ def download_audios
62
+ number_of_items = Audio.count
63
+ current_item = 0
64
+ requests_needed = (number_of_items / 100) + 1
65
+
66
+ requests_needed.times do |i|
67
+ Audio.all(:skip => i * 100, :limit => 100).each do |audio|
68
+ current_item += 1
69
+ puts "\nAudio #{current_item} / #{number_of_items}: #{audio.title}"
70
+
71
+ path = FileUtils.mkdir_p("streamio-export/audios/#{audio.id}").first
72
+ progress_bar_title = "Original (#{bytes_to_megabytes(audio.original_file['size'])})"
73
+ File.open("#{path}/#{audio.id}.json", "w:utf-8") { |file| file.write(audio.attributes.to_json) }
74
+ download("http://#{audio.original_file['http_uri']}", progress_bar_title)
75
+ end
76
+ end
77
+ end
78
+
79
+ def bytes_to_megabytes(bytes)
80
+ megabytes = bytes / 1048576.0
81
+ "%.1fMB" % [megabytes]
82
+ end
83
+
84
+ def download(url, path, progress_bar_title)
85
+ uri = URI.parse(url)
86
+ filename = "#{path}/#{File.basename(uri.path)}"
87
+ http = Net::HTTP.new(uri.host, uri.port)
88
+ size = http.request_head(uri.path)['Content-Length'].to_i
89
+ bytes_loaded = File.exist?(filename) ? File.size(filename) : 0
90
+
91
+ if size == bytes_loaded
92
+ puts " #{progress_bar_title}: Already downloaded..."
93
+ return
94
+ end
95
+
96
+ progress_bar = ProgressBar.create(
97
+ :title => " #{progress_bar_title}",
98
+ :starting_at => 0,
99
+ :total => size,
100
+ :format => "%t: |%B| %P%"
101
+ )
102
+
103
+ waiting_for_speed_test = true
104
+ start_time = nil
105
+ http.request_get(uri.path) do |response|
106
+ File.open(filename, "w:binary") do |file|
107
+ response.read_body do |data|
108
+ progress_bar.progress += data.length
109
+ file << data
110
+
111
+ if waiting_for_speed_test
112
+ start_time ||= Time.now
113
+ bytes_loaded_since_connection = progress_bar.progress
114
+ if bytes_loaded_since_connection > 524288
115
+ waiting_for_speed_test = false
116
+ seconds_taken = Time.now - start_time
117
+ bytes_per_second = bytes_loaded_since_connection / seconds_taken
118
+ raise SlowDownloadError if bytes_per_second < 65536
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ rescue SlowDownloadError
125
+ http.finish if http.started?
126
+ progress_bar.stop
127
+ puts " Slow download detected - retrying!"
128
+ retry
129
+ end
130
+ end
131
+ end
132
+
133
+ Streamio::CLI.start
@@ -0,0 +1,5 @@
1
+ module Streamio
2
+ module Exporter
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: streamio-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Backeus
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: streamio
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.2
30
+ - !ruby/object:Gem::Dependency
31
+ name: thor
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.16.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.16.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: ruby-progressbar
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.2
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.2
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.2
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.2
78
+ description:
79
+ email:
80
+ - david.backeus@streamio.com
81
+ executables:
82
+ - streamio
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - lib/streamio-cli/version.rb
87
+ - lib/streamio-cli.rb
88
+ - bin/streamio
89
+ - README.md
90
+ - LICENSE
91
+ - CHANGELOG.md
92
+ homepage: https://github.com/streamio/streamio-exporter
93
+ licenses: []
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ segments:
105
+ - 0
106
+ hash: -340215124816893152
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ segments:
114
+ - 0
115
+ hash: -340215124816893152
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.24
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Command line interface for exporting data from Streamio accounts.
122
+ test_files: []