motion_vj 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWUwMzVjNGM0YWFjMzEwNmMxMGJmMGZmOTlkMjA0MDVhMzBkNWFmMw==
5
+ data.tar.gz: !binary |-
6
+ MTgwODhmYzliNjRiNTM2MjBkNzdjZWQxODFiZDZkZTdkMGJjNTM3ZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MjJlMTEyM2I1MjM3NGYwODU3NTY5OGJmMTA2NjdlZjlhOTEwYmZmZjAzN2Q4
10
+ YTcwZjczZGIzMzI0N2FiMmNiOGFhMjlkZjQ3MDMyZTc1OTI5Y2VmYzlkNGY1
11
+ NDdiODljZjg5NmJjZGJjYzcwMWE4MTc0YmUxY2I1YTEwZmFkZWU=
12
+ data.tar.gz: !binary |-
13
+ ZTQ0NWJiOGFkZGYxOThlNjQ3YTk0NGRkM2E5MTdhZGJjM2VjNTFhZWZlZjJm
14
+ Mjg4Y2VjNjc0MjNlMzJlYmI3OTI5MWEzNjc2MTdkMGFlN2QwNGNjMjU3ZjIz
15
+ YmY4MTQ3ODY4N2NlMGIzNDk5YWFhNWQ0ODUwNjE3NDdhODNmYzU=
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .env
11
+ .tags
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ motion_vj
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p547
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in motion_vj.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Roque Pinel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # MotionVj
2
+
3
+ This gem uploads [motion](http://www.lavrsen.dk/foswiki/bin/view/Motion/WebHome) videos to Dropbox.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and make the executable available:
8
+
9
+ $ gem install motion_vj
10
+
11
+ ## Usage
12
+
13
+ Once installed, a Dropbox token is needed to start. Run `motionvj --get-token` and follow the instructions.
14
+
15
+ For more details, run `motionvj --help`:
16
+
17
+ ```
18
+ Usage: motionvj [options]
19
+ Required:
20
+ -c, --config-file [CONFIG_FILE] Path to the configuration file
21
+ Optional:
22
+ -t, --get-token Get the Dropbox token
23
+ Common:
24
+ -h, --help Show this message
25
+ -v, --version Show version
26
+ ```
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/repinel/motion_vj. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "motion_vj"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/exe/motionvj ADDED
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'motion_vj'
4
+
5
+ require 'dotenv'
6
+ require 'fileutils'
7
+ require 'optparse'
8
+
9
+ class App
10
+ def self.parse!(argv)
11
+ options = {}
12
+ opt_parser = OptionParser.new do |opts|
13
+ opts.banner = 'Usage: motionvj [options]'
14
+
15
+ opts.separator 'Required:'
16
+
17
+ opts.on('-c', '--config-file [CONFIG_FILE]', 'Path to the configuration file') do |config_filepath|
18
+ options[:config_filepath] = config_filepath
19
+ end
20
+
21
+ opts.separator 'Optional:'
22
+
23
+ opts.on('-t', '--get-token', 'Get the Dropbox token') do
24
+ options[:get_token] = true
25
+ end
26
+
27
+ opts.separator 'Common:'
28
+
29
+ opts.on_tail('-h', '--help', 'Show this message') do
30
+ puts opts
31
+ exit
32
+ end
33
+
34
+ opts.on_tail('-v', '--version', 'Show version') do
35
+ puts MotionVj::VERSION
36
+ exit
37
+ end
38
+ end
39
+
40
+ begin
41
+ opt_parser.parse!(argv)
42
+ rescue OptionParser::InvalidOption => e
43
+ $stderr.puts 'Invalid option.'
44
+ abort opt_parser.to_s
45
+ end
46
+
47
+ if options[:config_filepath].nil? || options[:config_filepath].to_s.strip.empty?
48
+ $stderr.puts 'The configuration file is required.'
49
+ abort opt_parser.to_s
50
+ end
51
+
52
+ options
53
+ end
54
+
55
+ def self.start(options)
56
+ Dotenv.load(options[:config_filepath])
57
+ self.check_config
58
+
59
+ trap('INT') do
60
+ self.remove_pid_file
61
+ exit 2
62
+ end
63
+
64
+ self.create_pid_file
65
+
66
+ if options[:get_token]
67
+ MotionVj.get_dropbox_token(self.get_env('DP_APP_KEY'), self.get_env('DP_APP_SECRET'))
68
+ else
69
+ MotionVj.start(get_env('DP_APP_TOKEN'))
70
+ sleep
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def self.check_config
77
+ %w( MOTION_CMD VIDEOS_DIR VIDEO_EXTENTION PID_FILE DB_VIDEOS_DIR ).each { |key| self.get_env(key) }
78
+ end
79
+
80
+ def self.get_env(key)
81
+ value = ENV[key]
82
+ abort "The environment variable '#{key}' is blank or defined." if value.to_s.strip.empty?
83
+ value
84
+ end
85
+
86
+ def self.read_pid
87
+ pid = nil
88
+ if File.exist?(ENV['PID_FILE'])
89
+ File.open(ENV['PID_FILE'], 'r') do |f|
90
+ pid = f.gets.to_i rescue nil
91
+ end
92
+ end
93
+ pid
94
+ end
95
+
96
+ def self.create_pid_file
97
+ pid = self.read_pid
98
+ if pid && !pid.zero?
99
+ pid_exist = !!Process.kill(0, pid) rescue false
100
+ abort "motionvj is already running. Please check the PID in '#{ENV['PID_FILE']}'." if pid_exist
101
+ end
102
+
103
+ File.open(ENV['PID_FILE'], 'w') do |f|
104
+ f.puts Process.pid
105
+ end
106
+ end
107
+
108
+ def self.remove_pid_file
109
+ pid = self.read_pid
110
+ FileUtils.rm_f(ENV['PID_FILE']) if pid && pid === Process.pid
111
+ end
112
+ end
113
+
114
+ options = App.parse!(ARGV)
115
+ App.start(options)
@@ -0,0 +1,40 @@
1
+ require 'dropbox_sdk'
2
+
3
+ module MotionVj
4
+ class Client
5
+ attr_reader :db_client
6
+
7
+ def initialize(token)
8
+ @db_client = DropboxClient.new(token)
9
+ end
10
+
11
+ def file_exist?(filename)
12
+ begin
13
+ metadata = db_client.metadata(File.join(ENV['DB_VIDEOS_DIR'], filename))
14
+ metadata && !metadata['is_dir'] && metadata['bytes'] && metadata['bytes'] > 0
15
+ rescue DropboxError => e
16
+ false
17
+ end
18
+ end
19
+
20
+ def upload(filepath)
21
+ open(filepath) do |file|
22
+ metadata = db_client.put_file(File.join(ENV['DB_VIDEOS_DIR'], File.basename(filepath)), file, true)
23
+ metadata && !metadata['is_dir'] && metadata['bytes'] && metadata['bytes'] > 0
24
+ end
25
+ end
26
+
27
+ def self.get_token(app_key, app_secret)
28
+ flow = DropboxOAuth2FlowNoRedirect.new(app_key, app_secret)
29
+ authorize_url = flow.start
30
+
31
+ puts "1. Go to: #{authorize_url}"
32
+ puts '2. Click "Allow" (you might have to log in first)'
33
+ puts '3. Copy the authorization code'
34
+ print 'Enter the authorization code here: '
35
+ code = MotionVj::Helpers::Input.gets_until_not_blank(:code)
36
+
37
+ flow.finish(code).first
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,16 @@
1
+ module MotionVj
2
+ module Helpers
3
+ module Input
4
+ def self.gets_until_not_blank(input_name = :input)
5
+ loop do
6
+ value = $stdin.gets.to_s.strip
7
+ if value.empty?
8
+ print("Please provide a valid #{input_name}:")
9
+ else
10
+ return value
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module MotionVj
2
+ module Helpers
3
+ autoload(:Input, 'motion_vj/helpers/input')
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ module MotionVj
2
+ module Logger
3
+ def self.info(msg)
4
+ $stdout.puts format(msg, 'INFO')
5
+ end
6
+
7
+ def self.error(msg)
8
+ $stderr.puts format(msg, 'ERROR')
9
+ end
10
+
11
+ private
12
+
13
+ def self.format(msg, type)
14
+ "[motionvj][#{Time.now.utc.strftime("%Y-%m-%d %H:%M:%S %Z")}][#{type}]: #{msg}"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module MotionVj
2
+ VERSION = "0.1.0"
3
+ end
data/lib/motion_vj.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'motion_vj/client'
2
+ require 'motion_vj/logger'
3
+ require 'motion_vj/version'
4
+ require 'motion_vj/helpers'
5
+
6
+ require 'fileutils'
7
+ require 'listen'
8
+
9
+ module MotionVj
10
+ def self.get_dropbox_token(app_key, app_secret)
11
+ token = MotionVj::Client.get_token(app_key, app_secret)
12
+ puts "Your Dropbox access token for this app is: #{token}"
13
+ end
14
+
15
+ def self.start(token)
16
+ listener = Listen.to(ENV['VIDEOS_DIR'], only: /\.#{Regexp.escape ENV['VIDEO_EXTENTION']}$/) do |modified_filepaths, _, removed_filepaths|
17
+ # handle addded files
18
+ unless modified_filepaths.empty?
19
+ client = MotionVj::Client.new(token)
20
+ motion_lsof = `lsof -c #{ENV['MOTION_CMD']}`
21
+
22
+ modified_filepaths.each do |filepath|
23
+ file_basename = File.basename filepath
24
+
25
+ if motion_lsof =~ /#{Regexp.escape file_basename}/
26
+ Logger.error "Added file still in use."
27
+ next
28
+ end
29
+
30
+ # if not being updated && not already uploaded
31
+ if !client.file_exist?(file_basename)
32
+ begin
33
+ if client.upload(filepath)
34
+ FileUtils.rm_f(filepath)
35
+ Logger.info "'#{file_basename}' was uploaded."
36
+ else
37
+ Logger.error "Could not upload '#{file_basename}'."
38
+ end
39
+ rescue => e
40
+ Logger.error "Could not upload '#{file_basename}'."
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ # handle removed files
47
+ removed_filepaths.each do |filepath|
48
+ Logger.info "'#{File.basename(filepath)}' deleted."
49
+ end
50
+ end
51
+ listener.start
52
+ end
53
+ end
data/motion_vj.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'motion_vj/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "motion_vj"
8
+ spec.version = MotionVj::VERSION
9
+ spec.authors = ["Roque Pinel"]
10
+ spec.email = ["repinel@gmail.com"]
11
+
12
+ spec.summary = %q{Motion video jockey.}
13
+ spec.description = %q{Motion video jockey that uploads to Dropbox.}
14
+ spec.homepage = "https://github.com/repinel/motion_vj"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'dotenv', "~> 2.0"
23
+ spec.add_dependency 'dropbox-sdk', "~> 1.6"
24
+ spec.add_dependency 'listen', "~> 3.0"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.10"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.8"
29
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion_vj
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Roque Pinel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dropbox-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: listen
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '5.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '5.8'
97
+ description: Motion video jockey that uploads to Dropbox.
98
+ email:
99
+ - repinel@gmail.com
100
+ executables:
101
+ - motionvj
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .ruby-gemset
107
+ - .ruby-version
108
+ - .travis.yml
109
+ - CODE_OF_CONDUCT.md
110
+ - Gemfile
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/setup
116
+ - exe/motionvj
117
+ - lib/motion_vj.rb
118
+ - lib/motion_vj/client.rb
119
+ - lib/motion_vj/helpers.rb
120
+ - lib/motion_vj/helpers/input.rb
121
+ - lib/motion_vj/logger.rb
122
+ - lib/motion_vj/version.rb
123
+ - motion_vj.gemspec
124
+ homepage: https://github.com/repinel/motion_vj
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.4.8
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Motion video jockey.
148
+ test_files: []