pulse-downloader 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +14 -5
- data/lib/pulse/downloader/client.rb +20 -7
- data/lib/pulse/downloader/file_downloader.rb +33 -6
- data/lib/pulse/downloader/version.rb +1 -1
- data/lib/pulse/downloader/web_page_parser.rb +14 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bee880729f0af4203d680eb78a1c3f0375137f14b4647e707ae6d4445e0cdd21
|
4
|
+
data.tar.gz: 504e7020e0af6291d7b39ba06f03fd40b23baae1c39263a5e0906982fdb38212
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e32fb830552f65170cdf26a2a13789cb058843df064ef2da2f393ad91cb8e5b06c741885bd7d6d665021bb2eb17ef7157ade02cd03b9218bfe4f753a798100d9
|
7
|
+
data.tar.gz: 39186157cbe4cff6c91e7a9d9b2424af1720563f18c9ca4ceb438e1bbe854de743b1677525cc57495393a080b72bfcd2e5aeceb78f37bd0f0efb5bcdfd0a755e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Pulse::Downloader
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
This is a library to download a specific group of files linked to on an html page.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,19 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
```ruby
|
24
|
+
require 'pulse/downloader'
|
25
|
+
|
26
|
+
client = Pulse::Downloader::Client.new(
|
27
|
+
url: '',
|
28
|
+
file_type: 'zip',
|
29
|
+
save_data: true,
|
30
|
+
save_path: '',
|
31
|
+
read_from_save_path: false,
|
32
|
+
verify_ssl: true,
|
33
|
+
report_time: false
|
34
|
+
)
|
35
|
+
```
|
26
36
|
|
27
37
|
## Development
|
28
38
|
|
@@ -34,7 +44,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
34
44
|
|
35
45
|
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pulse-downloader. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/pulse-downloader/blob/master/CODE_OF_CONDUCT.md).
|
36
46
|
|
37
|
-
|
38
47
|
## License
|
39
48
|
|
40
49
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -4,14 +4,27 @@ module Pulse
|
|
4
4
|
include ::Pulse::Downloader::WebPageParser
|
5
5
|
include ::Pulse::Downloader::FileDownloader
|
6
6
|
|
7
|
-
attr_reader :
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
attr_reader :url,
|
8
|
+
:file_type,
|
9
|
+
:save_data,
|
10
|
+
:save_path,
|
11
|
+
:read_from_save_path,
|
12
|
+
:verify_ssl,
|
13
|
+
:report_time,
|
14
|
+
:start_time,
|
15
|
+
:end_time
|
16
|
+
|
17
|
+
# TODO: Validation
|
18
|
+
# TODO: Retry
|
19
|
+
# TODO: DNS
|
20
|
+
def initialize(url:, file_type:, save_data: false, save_path: '', read_from_save_path: false, verify_ssl: true, report_time: false)
|
21
|
+
@url = url
|
11
22
|
@file_type = file_type
|
12
23
|
@save_data = save_data
|
13
24
|
@save_path = save_path
|
14
25
|
@read_from_save_path = read_from_save_path
|
26
|
+
@verify_ssl = verify_ssl
|
27
|
+
@report_time = report_time
|
15
28
|
end
|
16
29
|
|
17
30
|
def call!
|
@@ -33,11 +46,11 @@ module Pulse
|
|
33
46
|
private
|
34
47
|
|
35
48
|
def get_micro_second_time
|
36
|
-
(Time.now.to_f *
|
49
|
+
(Time.now.to_f * 1000).to_i
|
37
50
|
end
|
38
51
|
|
39
|
-
def
|
40
|
-
|
52
|
+
def print_time
|
53
|
+
puts "Request time: #{end_time - start_time} ms."
|
41
54
|
end
|
42
55
|
end
|
43
56
|
end
|
@@ -1,19 +1,22 @@
|
|
1
1
|
module Pulse
|
2
2
|
module Downloader
|
3
3
|
module FileDownloader
|
4
|
-
# save_path are defined in client.rb
|
4
|
+
# save_path and verify_ssl are defined in client.rb
|
5
5
|
def download(file_path)
|
6
6
|
raise "save_path is undefined" if save_data && save_path == ''
|
7
7
|
|
8
|
-
start_time = get_micro_second_time
|
8
|
+
@start_time = get_micro_second_time
|
9
9
|
|
10
|
-
file_data = HTTParty.get(file_path)
|
10
|
+
file_data = HTTParty.get(compute_file_link(file_path), verify: verify_ssl)
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
@end_time = get_micro_second_time
|
13
|
+
|
14
|
+
if report_time
|
15
|
+
print_time
|
16
|
+
end
|
14
17
|
|
15
18
|
if save_data
|
16
|
-
File.open(
|
19
|
+
File.open(compute_save_path(file_path), 'wb') do |file|
|
17
20
|
file.write(file_data.body)
|
18
21
|
end
|
19
22
|
end
|
@@ -21,9 +24,33 @@ module Pulse
|
|
21
24
|
file_data
|
22
25
|
end
|
23
26
|
|
27
|
+
def fetch_save_paths
|
28
|
+
fetch_file_paths.map do |file_path|
|
29
|
+
"#{save_path}/#{compute_filename(file_path)}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
24
33
|
def compute_hash_of(data)
|
25
34
|
{ data: data }.hash
|
26
35
|
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def compute_save_path(url)
|
40
|
+
"#{save_path}/#{compute_filename(url)}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def compute_filename(file_path)
|
44
|
+
file_path.scan(/[\/]\S+/).last
|
45
|
+
end
|
46
|
+
|
47
|
+
def compute_file_link(file_path)
|
48
|
+
if file_path[0] == '/'
|
49
|
+
"#{url}/#{file_path}"
|
50
|
+
else
|
51
|
+
file_path
|
52
|
+
end
|
53
|
+
end
|
27
54
|
end
|
28
55
|
end
|
29
56
|
end
|
@@ -2,24 +2,30 @@ module Pulse
|
|
2
2
|
module Downloader
|
3
3
|
module WebPageParser
|
4
4
|
def fetch_file_paths
|
5
|
-
start_time = get_micro_second_time
|
5
|
+
@start_time = get_micro_second_time
|
6
6
|
|
7
|
-
response = HTTParty.get(
|
7
|
+
response = HTTParty.get(url, verify: verify_ssl)
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
@end_time = get_micro_second_time
|
10
|
+
|
11
|
+
if report_time
|
12
|
+
print_time
|
13
|
+
end
|
14
|
+
|
15
|
+
extract_file_urls(response)
|
12
16
|
end
|
13
17
|
|
14
18
|
private
|
15
19
|
|
16
|
-
def extract_file_urls(response
|
17
|
-
|
20
|
+
def extract_file_urls(response)
|
21
|
+
return [] if response.body.nil? || response.body.empty?
|
22
|
+
|
23
|
+
parse_html(response.body)
|
18
24
|
.css('a')
|
19
25
|
.to_a
|
20
26
|
.map { |link| link['href'] }
|
21
27
|
.compact
|
22
|
-
.select { |link| link.include?
|
28
|
+
.select { |link| link.include? file_type }
|
23
29
|
end
|
24
30
|
|
25
31
|
def parse_html(raw_html)
|