get_them_all 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/gta CHANGED
@@ -49,7 +49,10 @@ class GtaRunner < Thor
49
49
  crawler.stop()
50
50
  end
51
51
 
52
- crawler.start()
52
+ deferrable = crawler.start()
53
+ deferrable.callback do
54
+ EM::stop_event_loop()
55
+ end
53
56
  end
54
57
  end
55
58
 
@@ -14,20 +14,43 @@ module GetThemAll
14
14
 
15
15
  req = @downloader.open_url(@url, "GET", nil, @referer)
16
16
  req.callback do |req|
17
-
18
- destpath = compute_filename(worker)
19
- download = @storage.write(destpath, req.response)
20
-
21
- download.callback do
17
+
18
+ destpath= @downloader.get_file_destpath_from_action(self)
19
+
20
+ file_exists = @storage.exist?(destpath)
21
+
22
+ # if the destination path does not already exists
23
+ # or if it does exists but we were asked to rename
24
+ # the file.
25
+ #
26
+ if !file_exists || @downloader.rename_duplicates
27
+
28
+ if file_exists
29
+ destpath = compute_unique_filename(destpath)
30
+ notify('action.download.renamed', worker, self, destpath)
31
+ end
32
+
33
+ download = @storage.write(destpath, req.response)
34
+
35
+ download.callback do
36
+ add_to_history()
37
+ set_deferred_status(:succeeded)
38
+
39
+ notify('action.download.success', worker, self, destpath)
40
+ end
41
+
42
+ download.errback do
43
+ notify('action.download.failure', worker, self)
44
+ end
45
+
46
+ else
47
+ # if we arrive here the destination exists
48
+ # and we don't want to rename it
22
49
  add_to_history()
23
50
  set_deferred_status(:succeeded)
24
-
25
- notify('action.download.success', worker, self, destpath)
51
+ notify('action.download.already_exists', worker, self, destpath)
26
52
  end
27
-
28
- download.errback do
29
- notify('action.download.failure', worker, self)
30
- end
53
+
31
54
  end
32
55
 
33
56
  req.timeout(5)
@@ -63,16 +86,13 @@ module GetThemAll
63
86
  end
64
87
  end
65
88
 
66
- def compute_filename(worker)
67
- destpath= @downloader.get_file_destpath_from_action(self)
68
-
89
+ def compute_unique_filename(destpath)
69
90
  # find an unused filename
70
- while @storage.exist?(destpath)
91
+ begin
71
92
  path, filename= File.dirname(destpath), File.basename(destpath).split(".")
72
93
  filename= "#{filename[0]}_#{random_string(2)}.#{filename[1]}"
73
94
  destpath= File.join(path, filename)
74
- notify('action.download.renamed', worker, self, destpath)
75
- end
95
+ end while @storage.exist?(destpath)
76
96
 
77
97
  destpath
78
98
  end
@@ -67,6 +67,11 @@ module GetThemAll
67
67
  log("File downloaded: #{destpath}")
68
68
  end
69
69
 
70
+ register_handler('action.download.already_exists') do |name, worker, action, destpath|
71
+ @download_files += 1
72
+ log("oo File already exists: #{destpath}")
73
+ end
74
+
70
75
  end
71
76
 
72
77
  def log(str)
@@ -51,6 +51,8 @@ module GetThemAll
51
51
  register_handler('action.download.success', &method(:work_completed))
52
52
  register_handler('action.download.failure', &method(:work_failed))
53
53
  register_handler('action.download.skipped', &method(:work_skipped))
54
+ register_handler('action.download.already_exists', &method(:work_skipped))
55
+
54
56
  end
55
57
 
56
58
 
@@ -45,6 +45,11 @@ module GetThemAll
45
45
 
46
46
  attr_reader :base_url, :storage, :history
47
47
 
48
+ ##
49
+ # If true a new filename will be generated for every file
50
+ # for which the destination already exists
51
+ attr_reader :rename_duplicates
52
+
48
53
  ##
49
54
  # Create and start the crawler.
50
55
  #
@@ -56,6 +61,8 @@ module GetThemAll
56
61
  # @option args [String] :folder_name The root path where
57
62
  # downloaded files will be saved (appended to the storage root).
58
63
  # @option args [Array] :extensions Array of Extension object.
64
+ # @option args [Boolean] :rename_duplicates If true a new name will be
65
+ # generated if the file exists.
59
66
  #
60
67
  # @option args [Hash] :storage Configure storage backend
61
68
  # :type is the backend name
@@ -73,6 +80,7 @@ module GetThemAll
73
80
  @start_url = args.delete(:start_url) || '/'
74
81
  @folder_name= args.delete(:folder_name)
75
82
  @login_request = args.delete(:login_request)
83
+ @rename_duplicates = args.delete(:rename_duplicates)
76
84
 
77
85
  # keep a pointer to each extension
78
86
  @extensions = args.delete(:extensions) || [ActionLogger]
@@ -186,7 +194,7 @@ module GetThemAll
186
194
  # Cleanly stop the engine and ensure the history file is
187
195
  # written.
188
196
  #
189
- def stop
197
+ def stop(&block)
190
198
  return if @stopping
191
199
 
192
200
  # first stop the exit timer, no longer needed once we are here
@@ -221,9 +229,9 @@ module GetThemAll
221
229
 
222
230
  notify('downloader.stopped', self)
223
231
 
224
- # and stop the reactor
225
- EM::stop_event_loop()
232
+ block.call if block
226
233
  end.resume
234
+
227
235
  end
228
236
 
229
237
  class AssertionFailed < RuntimeError; end
@@ -60,8 +60,12 @@ module GetThemAll
60
60
  else
61
61
  true
62
62
  end
63
+
63
64
  rescue Dropbox::FileNotFoundError
64
65
  false
66
+ rescue => err
67
+ show_error(err)
68
+ false
65
69
  end
66
70
 
67
71
  ##
@@ -85,6 +89,7 @@ module GetThemAll
85
89
 
86
90
  deferrable
87
91
  rescue => err
92
+ show_error(err)
88
93
  if retries < 4
89
94
  # puts "[#{retries}] Upload error, retrying: #{err}"
90
95
  retries += 1
@@ -104,6 +109,7 @@ module GetThemAll
104
109
  destpath = build_destpath(path)
105
110
  @session.download(destpath)
106
111
  rescue => err
112
+ show_error(err)
107
113
  raise ReadError, "cannot read file: #{err}"
108
114
  end
109
115
 
@@ -23,5 +23,13 @@ module GetThemAll
23
23
  File.join(@root, path)
24
24
  end
25
25
 
26
+ private
27
+ def show_error(err)
28
+ error(err.inspect)
29
+ err.backtrace.each do |line|
30
+ error(line)
31
+ end
32
+ end
33
+
26
34
  end
27
35
  end
@@ -1,3 +1,3 @@
1
1
  module GetThemAll
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: get_them_all
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-10-16 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor
16
- requirement: &70225302446980 !ruby/object:Gem::Requirement
16
+ requirement: &70163898376540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70225302446980
24
+ version_requirements: *70163898376540
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: em-http-request
27
- requirement: &70225302446460 !ruby/object:Gem::Requirement
27
+ requirement: &70163898375880 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70225302446460
35
+ version_requirements: *70163898375880
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: em-priority-queue
38
- requirement: &70225302445960 !ruby/object:Gem::Requirement
38
+ requirement: &70163898375200 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.0.2
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70225302445960
46
+ version_requirements: *70163898375200
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: hpricot
49
- requirement: &70225302445480 !ruby/object:Gem::Requirement
49
+ requirement: &70163898374580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.8.1
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70225302445480
57
+ version_requirements: *70163898374580
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: i18n
60
- requirement: &70225302445100 !ruby/object:Gem::Requirement
60
+ requirement: &70163898374060 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70225302445100
68
+ version_requirements: *70163898374060
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activesupport
71
- requirement: &70225302444480 !ruby/object:Gem::Requirement
71
+ requirement: &70163898373340 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 3.1.0
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70225302444480
79
+ version_requirements: *70163898373340
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: therubyracer
82
- requirement: &70225302443860 !ruby/object:Gem::Requirement
82
+ requirement: &70163898372680 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 0.9.8
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *70225302443860
90
+ version_requirements: *70163898372680
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: dropbox
93
- requirement: &70225302443460 !ruby/object:Gem::Requirement
93
+ requirement: &70163898372160 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *70225302443460
101
+ version_requirements: *70163898372160
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: girl_friday
104
- requirement: &70225302443000 !ruby/object:Gem::Requirement
104
+ requirement: &70163898371540 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,7 +109,7 @@ dependencies:
109
109
  version: '0'
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *70225302443000
112
+ version_requirements: *70163898371540
113
113
  description: Mass downloader useable as standalone or as a library
114
114
  email: []
115
115
  executables:
@@ -153,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
153
153
  version: '0'
154
154
  segments:
155
155
  - 0
156
- hash: 3586238315157463383
156
+ hash: -592874061905408396
157
157
  required_rubygems_version: !ruby/object:Gem::Requirement
158
158
  none: false
159
159
  requirements:
@@ -162,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
162
  version: '0'
163
163
  segments:
164
164
  - 0
165
- hash: 3586238315157463383
165
+ hash: -592874061905408396
166
166
  requirements: []
167
167
  rubyforge_project: get_them_all
168
168
  rubygems_version: 1.8.11