csr_data_inventory 0.0.1
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 +7 -0
- data/lib/csr_data_inventory.rb +41 -0
- data/lib/download_request.rb +61 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f9ff97cb355f70a1598d06969ac5fea7aab3e691d67b770eb3eebeba8a32fb2f
|
4
|
+
data.tar.gz: fade09a5ae0526452fb1d5b7a60fe3d76795b184b56529ea95c05056bdbe57dc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2568aae3ef8facb79484ca7b02f6271fd96db9915d5e36a80f68781124b9fc1cbad6effeecef03cfadb3cce1a6a8f3cc1e88a2df3213e738b23973f2bdfab873
|
7
|
+
data.tar.gz: 144289409d88c0d26eeed2fc7a013ce9189cef3436a1782ba93f550aeb342ca83cabfb7e3c7b127be4bbb2fc4172bd14e1e9588fdd68e6cb4fdbc8b321e8531e
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative './download_request.rb'
|
2
|
+
class CSRDataInventory
|
3
|
+
def self.download
|
4
|
+
puts "Please enter your token:"
|
5
|
+
token = gets.strip
|
6
|
+
download_url = "https://datascience.uth.edu/databases/download_file_list?"
|
7
|
+
params_part = URI.encode_www_form([["token",token]])
|
8
|
+
download_url = download_url + params_part
|
9
|
+
download_folder = download_folder = ::File.join(Dir.pwd, "#{token}_file_list.txt")
|
10
|
+
download_request = DownloadRequest.new(download_url, download_folder)
|
11
|
+
download_request.get
|
12
|
+
if download_request.error.to_s == ""
|
13
|
+
puts " File list downloaded"
|
14
|
+
File.readlines("#{Dir.pwd}/#{token}_file_list.txt").each do |line|
|
15
|
+
path_tokens = line.strip.split
|
16
|
+
|
17
|
+
path = path_tokens[0]
|
18
|
+
file_size = path_tokens[1]
|
19
|
+
|
20
|
+
puts "Downloading file: #{path}"
|
21
|
+
download_url = "https://datascience.uth.edu/databases/download_file?"
|
22
|
+
params_part = URI.encode_www_form([["token",token], ["path", path]])
|
23
|
+
download_url = download_url + params_part
|
24
|
+
download_folder = ::File.join(Dir.pwd, path[14..-1])
|
25
|
+
if File.exists?(download_folder) and (File.size(download_folder).to_s == file_size)
|
26
|
+
puts "File exists"
|
27
|
+
next
|
28
|
+
end
|
29
|
+
download_request = DownloadRequest.new(download_url, download_folder)
|
30
|
+
download_request.get
|
31
|
+
if download_request.error.to_s == ""
|
32
|
+
puts "Successful"
|
33
|
+
else
|
34
|
+
puts "Failed: #{download_request.error.to_s}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
else
|
38
|
+
puts " #{download_request.error.to_s}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "openssl"
|
4
|
+
require "net/http"
|
5
|
+
require "uri"
|
6
|
+
require 'fileutils'
|
7
|
+
# Downloads a file to a specified folder.
|
8
|
+
class DownloadRequest
|
9
|
+
class << self
|
10
|
+
def get(*args)
|
11
|
+
new(*args).get
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :url, :error, :file_size
|
16
|
+
|
17
|
+
def initialize(url, download_folder)
|
18
|
+
@url = URI.parse(url)
|
19
|
+
puts @url.path
|
20
|
+
@http = Net::HTTP.new(@url.host, @url.port)
|
21
|
+
if @url.scheme == "https"
|
22
|
+
@http.use_ssl = true
|
23
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
24
|
+
end
|
25
|
+
@download_folder = download_folder
|
26
|
+
@file_size = 0
|
27
|
+
end
|
28
|
+
|
29
|
+
# Writes file segments to disk immediately instead of storing in memory
|
30
|
+
def get
|
31
|
+
return unless @error.nil?
|
32
|
+
|
33
|
+
dirname = File.dirname(@download_folder)
|
34
|
+
unless File.directory?(dirname)
|
35
|
+
FileUtils.mkdir_p(dirname)
|
36
|
+
end
|
37
|
+
|
38
|
+
local_file = ::File.open(@download_folder, "wb")
|
39
|
+
partial = true
|
40
|
+
@http.request_get(@url) do |response|
|
41
|
+
case response.code
|
42
|
+
when "200"
|
43
|
+
response.read_body do |segment|
|
44
|
+
local_file.write(segment)
|
45
|
+
end
|
46
|
+
@file_size = ::File.size(@download_folder)
|
47
|
+
partial = false
|
48
|
+
when "302"
|
49
|
+
@error = "Token Not Authorized to Access Specified File"
|
50
|
+
else
|
51
|
+
@error = "#{response.code} #{response.class.name}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
rescue => e # Net::ReadTimeout, SocketError
|
55
|
+
@error = "(#{e.class}) #{e.message}"
|
56
|
+
ensure
|
57
|
+
local_file.close if local_file
|
58
|
+
::File.delete(@download_folder) if partial && ::File.exist?(@download_folder)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csr_data_inventory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shiqiang Tao
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-25 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A gem to download CSR Data in a scalable way.
|
14
|
+
email: shiqiang.tao@uth.tmc.edu
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/csr_data_inventory.rb
|
20
|
+
- lib/download_request.rb
|
21
|
+
homepage: https://rubygems.org/gems/csr_data_inventory
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubygems_version: 3.0.8
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Download CSR Data
|
44
|
+
test_files: []
|