gphotos 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: '0853ec2094d5927248106da01ecc98d512260580'
4
+ data.tar.gz: 25617bfccaf13e89f4aae4e8f6474b8b000df9fa
5
+ SHA512:
6
+ metadata.gz: 4d66d84a3fc21735599b1e2366605d1f147fc4998b1f5e0d9840c352fd7368bce03d93ae282a0f773bc020eda69e86e3a5611425d02fc42d4644931ffc13fd2e
7
+ data.tar.gz: 0cb52d5d5da6e8e70ed69420b3231fe255ba9af781632626ae88e1e78b18c28956ca28976421d9b49693722f04734509b4de4640f821fd71d5ac3df43e8792a2
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gphotos.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Zhong Jianxin
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.
@@ -0,0 +1,78 @@
1
+ # Gphotos
2
+
3
+ Use [Selenium WebDriver](http://www.seleniumhq.org/projects/webdriver/) to ease uploading files to [Google Photos](https://photos.google.com/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gphotos'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gphotos
20
+
21
+ ## Usage
22
+
23
+ Install [ChromeDriver](https://sites.google.com/a/chromium.org/chromedriver/). On Debian:
24
+
25
+ # apt-get install chromium-driver
26
+
27
+ Command line options:
28
+
29
+ Usage: gphotos [options] file...
30
+
31
+ Specific options:
32
+ -e, --email=EMAIL Set email to EMAIL
33
+ -p, --passwd=PASSWD Set passwd to PASSWD
34
+ -l, --list=FILE Read list of files to upload from FILE
35
+
36
+ Common options:
37
+ -h, --help Show this message
38
+ -V, --version Show version
39
+
40
+ Example:
41
+
42
+ $ gphotos -e foo@gmail.com -p bar /path/to/image.jpg /path/to/video.mp4
43
+
44
+ upload:
45
+ /path/to/image.jpg
46
+ /path/to/video.mp4
47
+
48
+ done:
49
+ 2 uploaded
50
+ 0 skipped
51
+ 0 not exist
52
+
53
+ Set email and password in config file, `~/.gphotos.yml`:
54
+
55
+ :email: foo@gmail.com
56
+ :passwd: foo
57
+
58
+ If you use password managers like [pass](https://www.passwordstore.org/), you can use `:passwd_exec` instead:
59
+
60
+ :passwd_exec: pass show foo@gmail.com
61
+
62
+ Cookies will be saved in `~/.gphotos.cookies`, so you don't need to login every time.
63
+
64
+ ## Development
65
+
66
+ 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.
67
+
68
+ 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).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/azuwis/ruby-gphotos.
73
+
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
78
+
@@ -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
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gphotos"
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
+ require "pry"
10
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Hide args from ps
4
+ $0 = $0
5
+
6
+ lib = File.expand_path('../../lib', __FILE__)
7
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
8
+
9
+ require "gphotos/app"
10
+
11
+ app = Gphotos::App.new(ARGV)
12
+ app.run
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gphotos/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gphotos"
8
+ spec.version = Gphotos::VERSION
9
+ spec.authors = ["Zhong Jianxin"]
10
+ spec.email = ["azuwis@gmail.com"]
11
+
12
+ spec.summary = %q{Use Selenium WebDriver to ease uploading files to Google Photos}
13
+ spec.homepage = "https://github.com/azuwis/ruby-gphotos"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+
27
+ spec.add_dependency 'selenium-webdriver'
28
+
29
+ spec.add_development_dependency 'pry'
30
+ end
@@ -0,0 +1,2 @@
1
+ require "gphotos/gphotos"
2
+ require "gphotos/version"
@@ -0,0 +1,111 @@
1
+ require 'gphotos'
2
+ require 'optparse'
3
+ require 'ostruct'
4
+ require 'yaml'
5
+
6
+ module Gphotos
7
+ class App
8
+
9
+ def self.parse(args)
10
+ options = OpenStruct.new
11
+
12
+ opt_parser = OptionParser.new do |opts|
13
+ opts.banner = "Usage: #{$0} [options] file..."
14
+ opts.separator "\nSpecific options:"
15
+
16
+ opts.on("-eEMAIL", "--email=EMAIL", "Set email to EMAIL") do |o|
17
+ options.email = o
18
+ end
19
+
20
+ opts.on("-pPASSWD", "--passwd=PASSWD", "Set passwd to PASSWD") do |o|
21
+ options.passwd = o
22
+ end
23
+
24
+ opts.on("-lFILE", "--list=FILE", "Read list of files to upload from FILE") do |o|
25
+ options.list = o
26
+ end
27
+
28
+ opts.separator "\nCommon options:"
29
+
30
+ opts.on_tail('-h', '--help', 'Show this message') do
31
+ puts opts
32
+ exit
33
+ end
34
+
35
+ opts.on_tail('-V', '--version', 'Show version') do
36
+ puts VERSION
37
+ exit
38
+ end
39
+ end
40
+
41
+ begin
42
+ opt_parser.parse!(args)
43
+ rescue OptionParser::InvalidOption
44
+ puts opt_parser
45
+ exit
46
+ end
47
+
48
+ if args.size == 0 and !options.list
49
+ puts opt_parser
50
+ exit
51
+ end
52
+
53
+ options
54
+ end
55
+
56
+ def self.load_config(file)
57
+ full_path = File.expand_path(file)
58
+ if File.exists?(full_path)
59
+ YAML.load_file(full_path)
60
+ else
61
+ {}
62
+ end
63
+ end
64
+
65
+ def initialize(args)
66
+ options = self.class.parse(args)
67
+ config = self.class.load_config('~/.gphotos.yml')
68
+ @options = OpenStruct.new(config.merge(options.to_h))
69
+ end
70
+
71
+ def run
72
+ files = []
73
+ files.concat(ARGV)
74
+ if @options.list
75
+ files.concat(open(@options.list).read.split("\n"))
76
+ end
77
+
78
+ gphotos = Gphotos.new(@options.email, @options.passwd, @options.passwd_exec)
79
+
80
+ puts 'upload:'
81
+ uploaded, skipped, not_exist = gphotos.upload(files) do |file, status|
82
+ if status == :uploaded
83
+ puts "#{file}"
84
+ else
85
+ puts "#{file} (#{status})"
86
+ end
87
+ end
88
+
89
+ if skipped.size > 0
90
+ puts
91
+ puts 'skipped:'
92
+ puts skipped.join("\n")
93
+ end
94
+
95
+ if not_exist.size > 0
96
+ puts
97
+ puts 'not_exist:'
98
+ puts not_exist.join("\n")
99
+ end
100
+
101
+ puts
102
+ puts 'done:'
103
+ puts "#{uploaded.size} uploaded"
104
+ puts "#{skipped.size} skipped"
105
+ puts "#{not_exist.size} not exist"
106
+
107
+ gphotos.quit
108
+ end
109
+
110
+ end
111
+ end
@@ -0,0 +1,101 @@
1
+ require 'selenium-webdriver'
2
+ require 'yaml'
3
+
4
+ module Gphotos
5
+ class Gphotos
6
+
7
+ def initialize(email, passwd, passwd_exec, options = {})
8
+ options = {:page_timeout => 20, :upload_timeout => 3600 }.merge(options)
9
+ profile = Selenium::WebDriver::Chrome::Profile.new
10
+ profile['profile.managed_default_content_settings.images'] = 2
11
+ @driver = Selenium::WebDriver.for(:chrome, :profile => profile)
12
+ @driver.manage.timeouts.implicit_wait = options[:page_timeout]
13
+ @wait = Selenium::WebDriver::Wait.new(:timeout => options[:upload_timeout])
14
+ @cookies = File.expand_path('~/.gphotos.cookies')
15
+ @workaround_applied = false
16
+ load_cookies(@cookies)
17
+ login(email, passwd, passwd_exec)
18
+ end
19
+
20
+ def load_cookies(file)
21
+ @driver.navigate.to 'https://photos.google.com/'
22
+ if File.exists?(file)
23
+ YAML.load_file(file).each do |cookie|
24
+ @driver.manage.add_cookie(cookie)
25
+ end
26
+ end
27
+ end
28
+
29
+ def login(email, passwd, passwd_exec)
30
+ @driver.navigate.to 'https://photos.google.com/albums'
31
+
32
+ if @driver.title.include?('Albums')
33
+ return
34
+ end
35
+
36
+ element = @driver.find_element(:id => 'Email')
37
+ element.send_keys(email)
38
+ element.submit
39
+
40
+ if !passwd and passwd_exec
41
+ passwd = %x{#{passwd_exec}}.strip
42
+ end
43
+
44
+ element = @driver.find_element(:id => 'Passwd')
45
+ element.send_keys(passwd)
46
+ element.submit
47
+
48
+ @driver.find_element(:css => 'input[type="file"]')
49
+ File.write(@cookies ,@driver.manage.all_cookies.to_yaml)
50
+ end
51
+
52
+ def upload(files, &block)
53
+ element = @driver.find_element(:css => 'input[type="file"]')
54
+ if !@workaround_applied
55
+ # XXX Get upload working
56
+ element.send_keys(Dir.tmpdir)
57
+ @workaround_applied = true
58
+ end
59
+
60
+ uploaded = []
61
+ skipped = []
62
+ not_exist = []
63
+ result = ''
64
+ files.each do |file|
65
+ file = File.expand_path(file)
66
+ if !File.exists?(file)
67
+ not_exist.push(file)
68
+ block.call(file, :not_exist)
69
+ next
70
+ end
71
+ current_result = ''
72
+ element.send_keys(file)
73
+ @wait.until do
74
+ alert = @driver.find_element(:css => 'div[role="alert"]')
75
+ begin
76
+ current_result = alert.attribute('innerText')
77
+ rescue Selenium::WebDriver::Error::StaleElementReferenceError
78
+ current_result = ''
79
+ end
80
+ current_result != '' and result != current_result
81
+ end
82
+
83
+ if current_result.include?('skipped') and result.split("\n")[0] != current_result.split("\n")[0]
84
+ skipped.push(file)
85
+ block.call(file, :skipped)
86
+ else
87
+ uploaded.push(file)
88
+ block.call(file, :uploaded)
89
+ end
90
+
91
+ result = current_result
92
+ end
93
+ [uploaded, skipped, not_exist]
94
+ end
95
+
96
+ def quit
97
+ @driver.quit
98
+ end
99
+
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module Gphotos
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gphotos
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Zhong Jianxin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: selenium-webdriver
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - azuwis@gmail.com
86
+ executables:
87
+ - gphotos
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - exe/gphotos
100
+ - gphotos.gemspec
101
+ - lib/gphotos.rb
102
+ - lib/gphotos/app.rb
103
+ - lib/gphotos/gphotos.rb
104
+ - lib/gphotos/version.rb
105
+ homepage: https://github.com/azuwis/ruby-gphotos
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.5.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Use Selenium WebDriver to ease uploading files to Google Photos
129
+ test_files: []