file_convert 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +20 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +3 -0
- data/README.md +3 -0
- data/bin/file_convert +26 -0
- data/config/config.sample.yml +15 -0
- data/file_convert.gemspec +28 -0
- data/lib/file_convert.rb +29 -28
- data/lib/file_convert/bucket.rb +22 -0
- data/lib/file_convert/configuration.rb +14 -0
- data/lib/file_convert/docs.rb +19 -0
- data/lib/file_convert/drive.rb +66 -0
- data/spec/pdf_to_txt_spec.rb +4 -0
- data/spec/spec_helper.rb +8 -0
- metadata +24 -18
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MGFkNzZhZDFkMzY0ZGJkMWRkOTk2NTVmZDE4ZmZiZjRjMWIyOTAyNQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OWZiOThlMGE0MjQ1NWYyYjA4YjJkZDJjMzJhYmYxMWY0ZjkzYzk0Mw==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NGI2YzY4MDczZGE2NzhmNzE1YTA4YmFiYTFlMDc0ZTIwNzU4ODcwM2M5M2Ux
|
10
|
+
MzVmYWEwZTg3YThjYjM4MjAwMmQ2ZGEzYjQ5NTY2OGNmNTg5NmNmMDZmZTBm
|
11
|
+
MTU0MDlkNTJjNDQ2YmRiNzQ1NTBkMmE0ZmU0NTIxYzA1ZWI1Mzg=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZTYwNzBlNjc4ZmM4ODAxMjljZWNlM2Q5NTM2NTU1Y2M3OGEyMWI0ODBlMWMy
|
14
|
+
YjliNTRiMmE2NDdkYzM4MzI2MzlkZjM1NTNiZDg5NDJiNDJiZTJmZTI2NWIx
|
15
|
+
MjU1N2MyNjdiNDQwYTc4NTdlNzBmZDgwYzZhZjM2YWFkNGQ0ZDk=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create 1.9.3-p392@file_convert
|
data/Gemfile
ADDED
data/README.md
ADDED
data/bin/file_convert
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'file_convert'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
puts 'Usage: file_convert config.yml file.doc'
|
7
|
+
|
8
|
+
@configurations ||= YAML.load_file(ARGV[0])
|
9
|
+
|
10
|
+
puts @configurations.inspect
|
11
|
+
|
12
|
+
FileConvert.configure do |config|
|
13
|
+
config.client_id = @configurations['CLIENT_ID']
|
14
|
+
config.client_secret = @configurations['CLIENT_SECRET']
|
15
|
+
config.oauth_scope = @configurations['OAUTH_SCOPE']
|
16
|
+
config.redirect_uri = @configurations['REDIRECT_URI']
|
17
|
+
config.user_name = @configurations['USER_NAME']
|
18
|
+
config.password = @configurations['PASSWORD']
|
19
|
+
config.s3_key = @configurations['S3_KEY']
|
20
|
+
config.s3_secret = @configurations['S3_SECRET']
|
21
|
+
config.s3_bucket = @configurations['S3_BUCKET']
|
22
|
+
config.tmp_folder = @configurations['TMP_FOLDER']
|
23
|
+
end
|
24
|
+
|
25
|
+
converter = FileConvert.new
|
26
|
+
puts converter.to_txt_from_s3 ARGV[1]
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# Get your credentials from the APIs Console
|
2
|
+
CLIENT_ID: '***'
|
3
|
+
CLIENT_SECRET: '***'
|
4
|
+
OAUTH_SCOPE: 'https://www.googleapis.com/auth/drive'
|
5
|
+
REDIRECT_URI: 'urn:ietf:wg:oauth:2.0:oob'
|
6
|
+
|
7
|
+
# Your google account
|
8
|
+
USER_NAME: '***@***.com'
|
9
|
+
PASSWORD: 'password'
|
10
|
+
|
11
|
+
S3_KEY: ***
|
12
|
+
S3_SECRET: ***
|
13
|
+
S3_BUCKET: www
|
14
|
+
|
15
|
+
TMP_FOLDER: '/tmp/'
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'file_convert'
|
7
|
+
s.version = '0.0.3'
|
8
|
+
s.executables << 'file_convert'
|
9
|
+
s.date = '2013-03-18'
|
10
|
+
s.summary = 'convert files to different formats'
|
11
|
+
s.description = 'wrapper around google drive to convert files to different formats'
|
12
|
+
s.authors = ['Sina Jahan']
|
13
|
+
s.email = 'info@sinajahan.com'
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split($/)
|
16
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
|
20
|
+
s.homepage = 'http://rubygems.org/gems/file_convert'
|
21
|
+
|
22
|
+
s.add_dependency('google-api-client', '~> 0.6.2')
|
23
|
+
s.add_dependency('mime-types', '~> 1.21')
|
24
|
+
s.add_dependency('gdata', '~> 1.1.2')
|
25
|
+
s.add_dependency('aws-s3', '~> 0.6.3')
|
26
|
+
|
27
|
+
s.add_development_dependency('rspec')
|
28
|
+
end
|
data/lib/file_convert.rb
CHANGED
@@ -1,38 +1,39 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'tempfile'
|
3
3
|
|
4
|
-
class FileConvert
|
5
|
-
def self.configure(&block)
|
6
|
-
yield(configuration)
|
7
|
-
configuration
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.configuration
|
11
|
-
@config ||= FileConvert::Configuration.new
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
def initialize
|
16
|
-
@drive = Drive.new FileConvert.configuration
|
17
|
-
@docs = Docs.new FileConvert.configuration
|
18
|
-
@bucket = Bucket.new FileConvert.configuration
|
19
|
-
end
|
20
|
-
|
21
|
-
def to_txt_from_s3(s3_file_key)
|
22
|
-
s3_file = @bucket.download s3_file_key
|
23
|
-
convert_txt_url = @drive.get_convert_txt_url s3_file.path
|
24
|
-
@docs.download_and_read(convert_txt_url)
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.get_a_temp_file(key = '')
|
28
|
-
Tempfile.new(['file_convert', key], configuration.tmp_folder)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
4
|
require 'file_convert/drive'
|
33
5
|
require 'file_convert/docs'
|
34
6
|
require 'file_convert/bucket'
|
35
7
|
require 'file_convert/configuration'
|
36
8
|
|
9
|
+
module FileConvert
|
10
|
+
class Converter
|
11
|
+
def self.configure(&block)
|
12
|
+
yield(configuration)
|
13
|
+
configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.configuration
|
17
|
+
@config ||= Configuration.new
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@drive = Drive.new Converter.configuration
|
23
|
+
@docs = Docs.new Converter.configuration
|
24
|
+
@bucket = Bucket.new Converter.configuration
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_txt_from_s3(s3_file_key)
|
28
|
+
s3_file = @bucket.download s3_file_key
|
29
|
+
convert_txt_url = @drive.get_convert_txt_url s3_file.path
|
30
|
+
@docs.download_and_read(convert_txt_url)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.get_a_temp_file(key = '')
|
34
|
+
Tempfile.new(['file_convert', key], configuration.tmp_folder)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
37
38
|
|
38
39
|
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'aws/s3'
|
2
|
+
|
3
|
+
module FileConvert
|
4
|
+
class Bucket
|
5
|
+
def initialize(config)
|
6
|
+
AWS::S3::Base.establish_connection!(
|
7
|
+
access_key_id: config.s3_key,
|
8
|
+
secret_access_key: config.s3_secret
|
9
|
+
)
|
10
|
+
|
11
|
+
@bucket = AWS::S3::Bucket.find('clearfit-thumbnails-development')
|
12
|
+
end
|
13
|
+
|
14
|
+
def download(key)
|
15
|
+
temp_file = Converter.get_a_temp_file key
|
16
|
+
file_with_key = @bucket[key]
|
17
|
+
raise "There is no file with the key [#{key}]" if file_with_key.nil?
|
18
|
+
File.open(temp_file, 'w') { |file| file.write(file_with_key.value) }
|
19
|
+
temp_file
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module FileConvert
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :client_id
|
4
|
+
attr_accessor :client_secret
|
5
|
+
attr_accessor :oauth_scope
|
6
|
+
attr_accessor :redirect_uri
|
7
|
+
attr_accessor :user_name
|
8
|
+
attr_accessor :password
|
9
|
+
attr_accessor :s3_key
|
10
|
+
attr_accessor :s3_secret
|
11
|
+
attr_accessor :s3_bucket
|
12
|
+
attr_accessor :tmp_folder
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'gdata/client'
|
2
|
+
require 'gdata/http'
|
3
|
+
require 'gdata/auth'
|
4
|
+
|
5
|
+
module FileConvert
|
6
|
+
class Docs
|
7
|
+
def initialize(config)
|
8
|
+
@client = GData::Client::Spreadsheets.new
|
9
|
+
@client.clientlogin(config.user_name, config.password)
|
10
|
+
end
|
11
|
+
|
12
|
+
def download_and_read(url)
|
13
|
+
temp_file = Converter.get_a_temp_file
|
14
|
+
content = @client.get(url)
|
15
|
+
File.open(temp_file, 'w') { |file| file.write(content.body) }
|
16
|
+
File.read(temp_file)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'google/api_client'
|
2
|
+
require 'launchy'
|
3
|
+
require 'yaml'
|
4
|
+
require 'mime/types'
|
5
|
+
|
6
|
+
module FileConvert
|
7
|
+
class Drive
|
8
|
+
def initialize(config)
|
9
|
+
# Get your credentials from the APIs Console
|
10
|
+
client_id = config.client_id
|
11
|
+
client_secret = config.client_secret
|
12
|
+
oauth_scope = config.oauth_scope
|
13
|
+
redirect_uri = config.redirect_uri
|
14
|
+
|
15
|
+
# Create a new API @client & load the Google Drive API
|
16
|
+
@client = Google::APIClient.new
|
17
|
+
@drive = @client.discovered_api('drive', 'v2')
|
18
|
+
|
19
|
+
# Request authorization
|
20
|
+
@client.authorization.client_id = client_id
|
21
|
+
@client.authorization.client_secret = client_secret
|
22
|
+
@client.authorization.scope = oauth_scope
|
23
|
+
@client.authorization.redirect_uri = redirect_uri
|
24
|
+
|
25
|
+
uri = @client.authorization.authorization_uri
|
26
|
+
Launchy.open(uri)
|
27
|
+
|
28
|
+
# Exchange authorization code for access token
|
29
|
+
$stdout.write 'Enter authorization code: '
|
30
|
+
@client.authorization.code = $stdin.gets.chomp
|
31
|
+
@client.authorization.fetch_access_token!
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_convert_txt_url(file_path)
|
35
|
+
file = @drive.files.insert.request_schema.new({
|
36
|
+
title: file_path,
|
37
|
+
description: 'A test resume document',
|
38
|
+
})
|
39
|
+
|
40
|
+
mime = MIME::Types.type_for(file_path).first.to_s
|
41
|
+
media = Google::APIClient::UploadIO.new(file_path, mime)
|
42
|
+
|
43
|
+
needs_ocr = mime == 'application/pdf'
|
44
|
+
|
45
|
+
result = @client.execute(
|
46
|
+
:api_method => @drive.files.insert,
|
47
|
+
:body_object => file,
|
48
|
+
:media => media,
|
49
|
+
:parameters => {
|
50
|
+
'uploadType' => 'multipart',
|
51
|
+
convert: true,
|
52
|
+
ocr: (true if needs_ocr),
|
53
|
+
ocrLanguage: (:en if needs_ocr),
|
54
|
+
useContentAsIndexableText: (true if needs_ocr),
|
55
|
+
'alt' => 'json'}.reject { |k, v| v.nil? })
|
56
|
+
|
57
|
+
file_id = result.data.id
|
58
|
+
|
59
|
+
result = @client.execute(
|
60
|
+
:api_method => @drive.files.get,
|
61
|
+
:parameters => {'fileId' => file_id})
|
62
|
+
|
63
|
+
result.data.export_links['text/plain']
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: file_convert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.3
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Sina Jahan
|
@@ -14,7 +13,6 @@ dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: google-api-client
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: mime-types
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: gdata
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ~>
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :runtime
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ~>
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -62,7 +55,6 @@ dependencies:
|
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: aws-s3
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
59
|
- - ~>
|
68
60
|
- !ruby/object:Gem::Version
|
@@ -70,7 +62,6 @@ dependencies:
|
|
70
62
|
type: :runtime
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
66
|
- - ~>
|
76
67
|
- !ruby/object:Gem::Version
|
@@ -78,7 +69,6 @@ dependencies:
|
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: rspec
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
73
|
- - ! '>='
|
84
74
|
- !ruby/object:Gem::Version
|
@@ -86,40 +76,56 @@ dependencies:
|
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
80
|
- - ! '>='
|
92
81
|
- !ruby/object:Gem::Version
|
93
82
|
version: '0'
|
94
83
|
description: wrapper around google drive to convert files to different formats
|
95
84
|
email: info@sinajahan.com
|
96
|
-
executables:
|
85
|
+
executables:
|
86
|
+
- file_convert
|
97
87
|
extensions: []
|
98
88
|
extra_rdoc_files: []
|
99
89
|
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- .rvmrc
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- README.md
|
96
|
+
- bin/file_convert
|
97
|
+
- config/config.sample.yml
|
98
|
+
- file_convert.gemspec
|
100
99
|
- lib/file_convert.rb
|
100
|
+
- lib/file_convert/bucket.rb
|
101
|
+
- lib/file_convert/configuration.rb
|
102
|
+
- lib/file_convert/docs.rb
|
103
|
+
- lib/file_convert/drive.rb
|
104
|
+
- spec/pdf_to_txt_spec.rb
|
105
|
+
- spec/spec_helper.rb
|
101
106
|
homepage: http://rubygems.org/gems/file_convert
|
102
107
|
licenses: []
|
108
|
+
metadata: {}
|
103
109
|
post_install_message:
|
104
110
|
rdoc_options: []
|
105
111
|
require_paths:
|
106
112
|
- lib
|
107
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
-
none: false
|
109
114
|
requirements:
|
110
115
|
- - ! '>='
|
111
116
|
- !ruby/object:Gem::Version
|
112
117
|
version: '0'
|
113
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
-
none: false
|
115
119
|
requirements:
|
116
120
|
- - ! '>='
|
117
121
|
- !ruby/object:Gem::Version
|
118
122
|
version: '0'
|
119
123
|
requirements: []
|
120
124
|
rubyforge_project:
|
121
|
-
rubygems_version:
|
125
|
+
rubygems_version: 2.0.3
|
122
126
|
signing_key:
|
123
|
-
specification_version:
|
127
|
+
specification_version: 4
|
124
128
|
summary: convert files to different formats
|
125
|
-
test_files:
|
129
|
+
test_files:
|
130
|
+
- spec/pdf_to_txt_spec.rb
|
131
|
+
- spec/spec_helper.rb
|