twitter_images 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5396eb2858c323b0b9bc1b102349c954538adc4
4
- data.tar.gz: 5fddbe2b1d551eae8e85debc0f723303fe4c36e4
3
+ metadata.gz: aa9d6738e520524fa87bebe3509598b51ff6b61c
4
+ data.tar.gz: d7021a59087e612655ec50a740ac3d02a559f94f
5
5
  SHA512:
6
- metadata.gz: 95f5b0fbbc333e7edd30091f9f34d45f2f642a4f513faa71f638fc03e87283b2e0cb426af4ea00232ec13bc45f6f89193cb114ca9ff53ba23fb16eaa292ff002
7
- data.tar.gz: ca4e23a17947b2f7d3718127cbd64a0e48f37059b9a98112df8e19c824066137eab412c2d92a8afb990c64e328dbd9464d33d93f6e9d4fb9202e84894d07c5fe
6
+ metadata.gz: 131398180fdb31450b64b8c1303637ed67b8190272d5e86206458a594c7c5b8222841190d26f0c87412ec8ccb3bcff2b635a48a15b8d10ee9c7ea39d261fd705
7
+ data.tar.gz: 552d52a07c0525ae55a54777785383c0823823cf21455f9090d0f16f2e3a1d9b0938b1b19b709355527bb20e1c1d06450c101fb964d0872394066d1a2fd7bf86
data/bin/twitter_images CHANGED
@@ -3,6 +3,4 @@
3
3
  $LOAD_PATH.unshift(File.expand_path("../lib", __FILE__))
4
4
  require "twitter_images"
5
5
 
6
- configuration = TwitterImages::Configuration.new
7
- downloader = TwitterImages::Downloader.new(configuration)
8
- TwitterImages::CLI.new(configuration, downloader).run
6
+ TwitterImages::CLI.new.run
@@ -5,7 +5,8 @@ require "json"
5
5
  require "oauth"
6
6
  require "ruby-progressbar"
7
7
 
8
+ require "twitter_images/cli"
8
9
  require "twitter_images/configuration"
10
+ require "twitter_images/requester"
9
11
  require "twitter_images/downloader"
10
- require "twitter_images/cli"
11
12
  require "twitter_images/version"
@@ -1,15 +1,13 @@
1
1
  module TwitterImages
2
2
  class CLI
3
- attr_accessor :downloader, :configuration
3
+ attr_accessor :configuration
4
4
 
5
- def initialize(configuration, downloader)
6
- @configuration = configuration
7
- @downloader = downloader
5
+ def initialize()
6
+ @configuration = Configuration.new
8
7
  end
9
8
 
10
9
  def run
11
10
  configuration.prepare
12
- downloader.download
13
11
  end
14
12
  end
15
13
  end
@@ -1,90 +1,28 @@
1
1
  module TwitterImages
2
2
  class Configuration
3
- attr_accessor :search, :directory, :output, :consumer_key, :access_token,
4
- :address, :request, :http, :response
5
-
6
- def initialize()
7
- @search = search
8
- @directory = directory
9
- @output = output
10
- @consumer_key = consumer_key
11
- @access_token = access_token
12
- @address = address
13
- @request = request
14
- @http = http
15
- @response = response
16
- end
3
+ attr_accessor :search, :directory
17
4
 
18
5
  def prepare
19
- setup_credentials
20
- get_directory
21
- change_directory
6
+ set_directory
22
7
  get_search
23
- establish_connection
24
- end
25
-
26
- def establish_connection
27
- setup_address
28
- setup_http
29
- build_request
30
- issue_request
31
- get_output
8
+ Requester.new(@search).start
32
9
  end
33
10
 
34
11
  private
35
12
 
36
- def setup_credentials
37
- check_env
38
- @consumer_key = OAuth::Consumer.new(ENV["CONSUMER_KEY"], ENV["CONSUMER_SECRET"])
39
- @access_token = OAuth::Token.new(ENV["ACCESS_TOKEN"], ENV["ACCESS_SECRET"])
40
- end
41
-
42
- def check_env
43
- if ENV.key?("CONSUMER_KEY") &&
44
- ENV.key?("CONSUMER_SECRET") &&
45
- ENV.key?("ACCESS_TOKEN") &&
46
- ENV.key?("ACCESS_SECRET")
47
- return true
48
- else
49
- return "The credentials have not been correctly set up in your ENV"
50
- end
51
- end
52
-
53
- def setup_http
54
- # Set up Net::HTTP to use SSL, which is required by Twitter.
55
- @http = Net::HTTP.new(address.host, address.port)
56
- http.use_ssl = true
57
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
58
- end
59
-
60
- def build_request
61
- # Build the request and authorize it with OAuth.
62
- @request = Net::HTTP::Get.new(address.request_uri)
63
- request.oauth!(http, consumer_key, access_token)
64
- end
65
-
66
- def issue_request
67
- # Issue the request and return the response.
68
- http.start
69
- @response = http.request(request)
70
- end
71
-
72
- def get_output
73
- @output = JSON.parse(response.body)
74
- end
75
-
76
- def setup_address
77
- @address = URI("https://api.twitter.com/1.1/search/tweets.json?q=%23#{search}&mode=photos&count=100")
13
+ def set_directory
14
+ puts "Please enter the directory to save the images in: "
15
+ @directory = gets.chomp
16
+ raise StandardError, "Directory doesn't exist" unless directory_exists?
17
+ change_directory
78
18
  end
79
19
 
80
- def get_directory
81
- puts "Please enter the absolute path to the directory to save the images in: "
82
- @directory = gets.chomp
83
- raise StandardError, "Directory doesn't exist" unless Dir.exists?(@directory)
20
+ def directory_exists?
21
+ Dir.exist?(File.expand_path(@directory))
84
22
  end
85
23
 
86
24
  def change_directory
87
- Dir.chdir(@directory)
25
+ Dir.chdir(File.expand_path(@directory))
88
26
  end
89
27
 
90
28
  def get_search
@@ -1,34 +1,32 @@
1
1
  module TwitterImages
2
2
  class Downloader
3
- attr_accessor :images
4
- attr_reader :configuration
3
+ attr_accessor :response, :output, :images
5
4
 
6
- def initialize(configuration)
7
- @images = images
8
- @configuration = configuration
5
+ def initialize(response)
6
+ @response = response
9
7
  end
10
8
 
11
9
  def download
12
- get_images
10
+ get_output
11
+ parse_output
13
12
  save_images
14
13
  end
15
14
 
16
15
  private
17
16
 
18
- def get_images
19
- @images = configuration.output.inspect.scan(/http:\/\/pbs.twimg.com\/media\/\w+\.(?:jpg|png|gif)/)
20
- raise StandardError, "Couldn't find any images" unless (@images.count > 0)
17
+ def get_output
18
+ @output = JSON.parse(response.body)
21
19
  end
22
20
 
23
- def make_absolute(href, root)
24
- URI.parse(root).merge(URI.parse(href)).to_s
21
+ def parse_output
22
+ @images = output.inspect.scan(/https:\/\/pbs.twimg.com\/media\/\w+\.(?:jpg|png|gif)/)
23
+ raise StandardError, "Couldn't find any images" unless @images.count > 0
25
24
  end
26
25
 
27
26
  def save_images
28
27
  progressbar = ProgressBar.create(:total => images.count)
29
28
  images.each do |src|
30
- uri = make_absolute(src, configuration.search)
31
- File.open(File.basename(uri), 'wb'){ |f| f.write(open(uri).read) }
29
+ File.open(File.basename(src), "wb") { |f| f.write(open(src + ":large").read) }
32
30
  progressbar.increment
33
31
  end
34
32
  end
@@ -0,0 +1,61 @@
1
+ module TwitterImages
2
+ class Requester
3
+ attr_accessor :search, :consumer_key, :access_token, :address, :https,
4
+ :request, :response
5
+
6
+ def initialize(search)
7
+ @search = search
8
+ end
9
+
10
+ def start
11
+ set_address
12
+ setup_credentials
13
+ setup_https
14
+ build_request
15
+ issue_request
16
+ Downloader.new(@response).download
17
+ end
18
+
19
+ private
20
+
21
+ def set_address
22
+ @address = URI("https://api.twitter.com/1.1/search/tweets.json?q=%23#{search}&mode=photos&count=100")
23
+ end
24
+
25
+ def setup_credentials
26
+ check_env
27
+ @consumer_key = OAuth::Consumer.new(ENV["CONSUMER_KEY"], ENV["CONSUMER_SECRET"])
28
+ @access_token = OAuth::Token.new(ENV["ACCESS_TOKEN"], ENV["ACCESS_SECRET"])
29
+ end
30
+
31
+ def check_env
32
+ if ENV.key?("CONSUMER_KEY") &&
33
+ ENV.key?("CONSUMER_SECRET") &&
34
+ ENV.key?("ACCESS_TOKEN") &&
35
+ ENV.key?("ACCESS_SECRET")
36
+ return true
37
+ else
38
+ puts "The credentials have not been correctly set up in your ENV"
39
+ end
40
+ end
41
+
42
+ def setup_https
43
+ # Set up Net::HTTP to use SSL, which is required by Twitter.
44
+ @https = Net::HTTP.new(address.host, address.port)
45
+ https.use_ssl = true
46
+ https.verify_mode = OpenSSL::SSL::VERIFY_PEER
47
+ end
48
+
49
+ def build_request
50
+ # Build the request and authorize it with OAuth.
51
+ @request = Net::HTTP::Get.new(address.request_uri)
52
+ request.oauth!(https, consumer_key, access_token)
53
+ end
54
+
55
+ def issue_request
56
+ # Issue the request and return the response.
57
+ @https.start
58
+ @response = https.request(request)
59
+ end
60
+ end
61
+ end
@@ -1,3 +1,3 @@
1
1
  module TwitterImages
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitter_images
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Zabelin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-03 00:00:00.000000000 Z
11
+ date: 2016-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fileutils
@@ -171,6 +171,7 @@ files:
171
171
  - lib/twitter_images/cli.rb
172
172
  - lib/twitter_images/configuration.rb
173
173
  - lib/twitter_images/downloader.rb
174
+ - lib/twitter_images/requester.rb
174
175
  - lib/twitter_images/version.rb
175
176
  homepage: https://github.com/alexeyzab/twitter_images
176
177
  licenses: