chakin-rb 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/lib/chakin-rb/chakin.rb +31 -11
- data/lib/chakin-rb/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b84417581a3b637fd8983910da40f5b606854c11
|
4
|
+
data.tar.gz: 34be171f2ae48889ff6a8656bfe8a125a4a647d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89ea491cc2ff13af0389e2f01d044ba2369b4bafa8e9e548c586d4494234cf16d083e13ea034c3ab4f75d1f9fac5acc376f769e4c32f1424301dc76e958b2e4c
|
7
|
+
data.tar.gz: b82efb6ccbd6c7aafb7fc638e9f7fbd959c64621854b512995b7ba2127a1d230859c9ef5e482f05e9b8ac299468bccd15fcbd80a2b7e9125bdbc0818fcf566aa
|
data/.gitignore
CHANGED
data/lib/chakin-rb/chakin.rb
CHANGED
@@ -6,6 +6,12 @@ require 'net/http'
|
|
6
6
|
require 'fileutils'
|
7
7
|
|
8
8
|
module Chakin
|
9
|
+
class HttpRedirect < StandardError
|
10
|
+
attr_reader :new_url
|
11
|
+
def initialize(new_url)
|
12
|
+
@new_url = new_url
|
13
|
+
end
|
14
|
+
end
|
9
15
|
class Error < StandardError; end
|
10
16
|
module Downloader
|
11
17
|
def load_datasets(path = File.join(__dir__, 'datasets.csv'))
|
@@ -26,33 +32,47 @@ module Chakin
|
|
26
32
|
url = row['URL']
|
27
33
|
raise 'The word vector you specified was not found. Please specify correct name.' if url.nil?
|
28
34
|
|
29
|
-
|
35
|
+
|
30
36
|
file_name = url.split('/')[-1]
|
31
37
|
|
32
38
|
FileUtils.mkdir_p(save_dir) unless File.exist?(save_dir)
|
33
39
|
|
34
40
|
save_path = File.join(save_dir, file_name)
|
41
|
+
begin
|
42
|
+
download_file(save_path, url)
|
43
|
+
rescue Chakin::HttpRedirect => e
|
44
|
+
download_file(save_path, e.new_url)
|
45
|
+
end
|
46
|
+
save_path
|
47
|
+
end
|
48
|
+
|
49
|
+
def download_file(save_path, url)
|
50
|
+
progressbar = ProgressBar.create
|
35
51
|
|
36
52
|
f = File.open(save_path, 'wb')
|
37
53
|
begin
|
38
54
|
my_uri = URI.parse(url)
|
55
|
+
http = Net::HTTP.new(my_uri.host, my_uri.port)
|
39
56
|
|
40
|
-
|
41
|
-
http.
|
42
|
-
|
43
|
-
progressbar.total = total_size
|
57
|
+
if my_uri.instance_of?(URI::HTTPS)
|
58
|
+
http.use_ssl = true
|
59
|
+
end
|
44
60
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
61
|
+
http.request_get(my_uri.path) do |resp|
|
62
|
+
total_size = resp.content_length
|
63
|
+
progressbar.total = total_size
|
64
|
+
|
65
|
+
if resp.code == "302"
|
66
|
+
raise HttpRedirect.new(resp.header['Location'])
|
67
|
+
end
|
68
|
+
resp.read_body do |segment|
|
69
|
+
progressbar.progress += segment.size
|
70
|
+
f.write(segment)
|
49
71
|
end
|
50
72
|
end
|
51
73
|
ensure
|
52
74
|
f.close
|
53
75
|
end
|
54
|
-
|
55
|
-
save_path
|
56
76
|
end
|
57
77
|
|
58
78
|
def search(lang = '')
|
data/lib/chakin-rb/version.rb
CHANGED