ngdrive 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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3a4afdda6630b12086cdd0d88508ec56a53ffc9643ed67a4723942920193a58b
4
+ data.tar.gz: e75b8ccf10c32323272a63c0df8903c3dbce267270300823b65fdb1db27cd6fe
5
+ SHA512:
6
+ metadata.gz: 448dfc9db3622061745d34279d44f4f7adf01dac6b853eac2340866f86db310d14af590b0c960c25b98a389fa03bda95f3a3f0e315b50ccb363585f4f2de7e14
7
+ data.tar.gz: 90afcfbaefdc9a3dbeb27548ef233afb073bead9eedc755f7578d68ab80ff76a5b684a288aa3020a1b9b0f8526af046457cef7e838fde312d5d4089fa5d80a4d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at nchikkam@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in ngdrive.gemspec
8
+ gemspec
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # ngdrive
2
+
3
+ A simple ngdrive utitily to download files from gdrive folders recursively into local machine.
4
+
5
+ Google allows/supports accessing google drive files via API.
6
+ Below steps are pre-requisites to do the same.
7
+
8
+ 1. First a gmail account is needed.
9
+ 2. An app must be created via console.developers.google.com eg: app-name: 'gdrive-syncher'. This can be 'Desktop App'.
10
+ 3. credentials.json file from above app must be placed in the config/ folder.
11
+ 4. 'Google Drive API' must be enabled for the above app via 'APIs & Services' -> 'Enable API & Services'.
12
+ 5. Above app must be registered with:
13
+ - OAuth consent screen
14
+ - App Info:
15
+ - app name: gdrive-syncher
16
+ - support email: email given in point 1 above
17
+ - app logo optional
18
+ - App Domain:
19
+ - application home page: https://gmail.com
20
+
21
+ - Authorized domains:
22
+ - gmail.com
23
+ - Developer contact information
24
+ - email given in point 1 above
25
+
26
+ - Scope
27
+ - Google Drive API
28
+
29
+ - Test users
30
+ - email given in point 1 must be added as test user
31
+
32
+ ## Installation
33
+
34
+ Add this line to your application's Gemfile:
35
+
36
+ ```ruby
37
+ gem 'googledrive'
38
+ ```
39
+
40
+ And then execute:
41
+
42
+ $ bundle
43
+
44
+ Or install it yourself as:
45
+
46
+ $ gem install googledrive
47
+
48
+ ## Usage
49
+
50
+ ```ruby
51
+ require 'googledrive'
52
+ gd = Ngdrive::GdriveUtil.new
53
+ gd.download_files
54
+ ```
55
+ Above lines will pickup the files/folders present in the gdrive.folder_id value in settings.yml and download all files
56
+ recursively into ./downloads folder along with their respective folder names.
57
+
58
+ - Notes:
59
+ - google drive maintains a session (via token) valid for a certain amount of time. This session is based on a token
60
+ and will expire after the duration.
61
+ - each time a request is made to google drive via API, it needs this token to allow google drive to continue
62
+ accessing.
63
+ - First time when 'Ngdrive::GdriveUtil.new' is run, the gem does below steps:
64
+
65
+ - it prompts user with a url to copy and enter in the browser. This is OAuthentication request asking the user
66
+ to allow the access to drive via gmail account. This consent is mandatory as per google rules and
67
+ regulations.
68
+ - When prompted with url on console, user has to enter the url in the browser, allow access, continue.
69
+ - once confirmed, google returns a url with format similar to below:
70
+ - 'https://....code=<<abce...xyz>>&aa=...' code received between << and >> is what is needed as initial token.
71
+ - users have to copy this and enter in the console to allow gem to create config/token.yaml to keep the token
72
+ for subsequent calls.
73
+ - This token will expire after certain time, in which case, users have to do the url copy paste each time they
74
+ need to access drive via API.
75
+ - once token.yaml file is created, accessing and downloading googledrive files is as simple as running:
76
+ gd.download_files
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'ngdrive'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ngdrive
4
+ VERSION = '0.1.0'
5
+ end
data/lib/ngdrive.rb ADDED
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ngdrive/version'
4
+ require 'fileutils'
5
+ require 'google/apis/drive_v3'
6
+ require 'googleauth'
7
+ require 'googleauth/stores/file_token_store'
8
+ require 'json'
9
+ require 'net/http'
10
+
11
+ module Ngdrive
12
+ # google drive util to download files from folder recursively
13
+ class GdriveUtil # rubocop:disable Metrics/ClassLength
14
+ SCOPE = Google::Apis::DriveV3::AUTH_DRIVE_READONLY
15
+
16
+ attr_reader :drive_service
17
+
18
+ def initialize
19
+ @config = YAML.load_file('./settings.yml')
20
+ @drive_service = Google::Apis::DriveV3::DriveService.new
21
+ @drive_service.client_options.application_name = @config['app']['name']
22
+ @drive_service.authorization = authorize
23
+ end
24
+
25
+ def get_entries(folder_id)
26
+ all_files = fetch_files(folder_id)
27
+ folders = get_folders(all_files['items'])
28
+ files = get_files(all_files['items'])
29
+
30
+ entries = {}
31
+ entries[:files] = files.map { |x| { id: x['id'], name: x['title'] } }
32
+ folders.each do |folder|
33
+ title = folder['title']
34
+ entries[title] = get_entries(folder['id'])
35
+ end
36
+
37
+ entries
38
+ end
39
+
40
+ def root_folder_id
41
+ @config['gdrive']['folder_id']
42
+ end
43
+
44
+ def download_folder
45
+ @config['app']['download_dir']
46
+ end
47
+
48
+ def download_files
49
+ entries = get_entries(root_folder_id)
50
+ download!(entries)
51
+ end
52
+
53
+ def download!(root, path = [])
54
+ return unless root
55
+
56
+ folder = create_folder!(path)
57
+
58
+ root[:files].each do |file|
59
+ file_path = "#{folder}/#{file[:name]}"
60
+ drive_service.get_file(file[:id], download_dest: file_path)
61
+ end
62
+
63
+ folders = root.keys.reject { |x| x == :files }
64
+ folders.each do |folder_key|
65
+ download!(root[folder_key], path + [folder_key])
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def create_folder!(path)
72
+ folder = "#{download_folder}/#{path.join('/')}"
73
+ Dir.mkdir(folder) unless File.exist?(folder)
74
+ folder
75
+ end
76
+
77
+ def authorize # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
78
+ client_id = Google::Auth::ClientId.from_file(
79
+ @config['app']['credentials']
80
+ )
81
+ token_store = Google::Auth::Stores::FileTokenStore.new(
82
+ file: @config['app']['token']
83
+ )
84
+ authorizer = Google::Auth::UserAuthorizer.new(
85
+ client_id,
86
+ SCOPE,
87
+ token_store
88
+ )
89
+ user_id = 'default'
90
+ credentials = authorizer.get_credentials user_id
91
+ if credentials.nil?
92
+ url = authorizer.get_authorization_url(
93
+ base_url: @config['app']['oob_uri']
94
+ )
95
+ puts 'Allow Authorization (Pair User Credentials) on Consent Form ' \
96
+ 'Open the following URL in the browser and enter the ' \
97
+ "resulting code after authorization:\n" + url
98
+ code = gets
99
+ credentials = authorizer.get_and_store_credentials_from_code(
100
+ user_id: user_id, code: code, base_url: @config['app']['oob_uri']
101
+ )
102
+ end
103
+ credentials
104
+ end
105
+
106
+ def fetch_files(folder_id) # rubocop:disable Metrics/MethodLength
107
+ prefix = 'https://www.googleapis.com/drive/v2/files?q='
108
+ uri = URI("#{prefix}'#{folder_id}'+in+parents")
109
+ req = Net::HTTP::Get.new(uri)
110
+
111
+ token = drive_service.request_options.authorization.access_token
112
+ req['Authorization'] = "Bearer #{token}"
113
+
114
+ res = Net::HTTP.start(
115
+ uri.hostname,
116
+ uri.port,
117
+ use_ssl: uri.scheme == 'https'
118
+ ) do |http|
119
+ http.request(req)
120
+ end
121
+
122
+ resp = JSON.parse(res.body)
123
+ resp
124
+ end
125
+
126
+ def get_folders(collection)
127
+ collection.select(&method(:folder?))
128
+ end
129
+
130
+ def get_files(collection)
131
+ collection.reject(&method(:folder?))
132
+ end
133
+
134
+ def folder?(param)
135
+ param['mimeType'] == 'application/vnd.google-apps.folder'
136
+ end
137
+
138
+ def get_only_pdf_files(collection)
139
+ collection.select { |x| x['mimeType'] == 'application/pdf' }
140
+ end
141
+ end
142
+ end
data/ngdrive.gemspec ADDED
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'ngdrive/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'ngdrive'
9
+ spec.version = Ngdrive::VERSION
10
+ spec.authors = ['narayana']
11
+ spec.email = ['nchikkam@gmail.com']
12
+
13
+ spec.summary = 'Simple gem to download google drive files into local'
14
+ spec.description = 'Gem to download google drive files into local \
15
+ Inspiration: https://console.cloud.google.com/apis/'
16
+ spec.homepage = 'https://github.com'
17
+ spec.license = 'MIT'
18
+
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+
22
+ spec.metadata['homepage_uri'] = spec.homepage
23
+ spec.metadata['source_code_uri'] = 'https://github.com'
24
+ spec.metadata['changelog_uri'] = 'https://github.com'
25
+ else
26
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
27
+ 'public gem pushes.'
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been
32
+ # added into git.
33
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
34
+ `git ls-files -z`.split("\x0").reject do |f|
35
+ f.match(%r{^(test|spec|features)/})
36
+ end
37
+ end
38
+ spec.bindir = 'exe'
39
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
40
+ spec.require_paths = ['lib']
41
+
42
+ spec.add_development_dependency 'bundler', '~> 1.17'
43
+ spec.add_development_dependency 'google-apis-drive_v3', '~>0.19.0'
44
+ spec.add_development_dependency 'google_drive', '~>2.1.5'
45
+ spec.add_development_dependency 'rake', '~> 10.0'
46
+ spec.add_development_dependency 'rspec', '~> 3.0'
47
+
48
+ spec.add_development_dependency 'faraday', '~>1.10.0'
49
+ spec.add_development_dependency 'mechanize', '~>2.7.7'
50
+ spec.add_development_dependency 'nokogiri', '~> 1.12.5'
51
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ngdrive
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - narayana
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-06-10 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: google-apis-drive_v3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.19.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.19.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: google_drive
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.1.5
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.1.5
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.10.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.10.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: mechanize
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.7.7
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.7.7
111
+ - !ruby/object:Gem::Dependency
112
+ name: nokogiri
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 1.12.5
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.12.5
125
+ description: |-
126
+ Gem to download google drive files into local \
127
+ Inspiration: https://console.cloud.google.com/apis/
128
+ email:
129
+ - nchikkam@gmail.com
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - ".gitignore"
135
+ - ".rspec"
136
+ - ".travis.yml"
137
+ - CODE_OF_CONDUCT.md
138
+ - Gemfile
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - lib/ngdrive.rb
144
+ - lib/ngdrive/version.rb
145
+ - ngdrive.gemspec
146
+ homepage: https://github.com
147
+ licenses:
148
+ - MIT
149
+ metadata:
150
+ allowed_push_host: https://rubygems.org
151
+ homepage_uri: https://github.com
152
+ source_code_uri: https://github.com
153
+ changelog_uri: https://github.com
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubygems_version: 3.0.8
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: Simple gem to download google drive files into local
173
+ test_files: []