Class: E621ExportDownloader::Export
- Inherits:
-
Object
- Object
- E621ExportDownloader::Export
- Extended by:
- T::Generic, T::Sig
- Defined in:
- lib/e621_export_downloader/export.rb
Constant Summary collapse
- Model =
type_member
Instance Attribute Summary collapse
-
#date ⇒ Object
Returns the value of attribute date.
-
#downloaded ⇒ Object
If nil, no check has been performed yet.
-
#helper ⇒ Object
Returns the value of attribute helper.
Instance Method Summary collapse
- #delete ⇒ Object
- #download ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(date:, helper:) ⇒ Export
constructor
A new instance of Export.
- #read(&block) ⇒ Object
- #read_all ⇒ Object
Constructor Details
#initialize(date:, helper:) ⇒ Export
Returns a new instance of Export.
24 25 26 27 28 |
# File 'lib/e621_export_downloader/export.rb', line 24 def initialize(date:, helper:) @date = T.let(date.is_a?(DateTime) ? date.to_date : date, Date) @helper = helper @downloaded = T.let(nil, T.nilable(T::Boolean)) end |
Instance Attribute Details
#date ⇒ Object
Returns the value of attribute date.
14 15 16 |
# File 'lib/e621_export_downloader/export.rb', line 14 def date @date end |
#downloaded ⇒ Object
If nil, no check has been performed yet
21 22 23 |
# File 'lib/e621_export_downloader/export.rb', line 21 def downloaded @downloaded end |
#helper ⇒ Object
Returns the value of attribute helper.
17 18 19 |
# File 'lib/e621_export_downloader/export.rb', line 17 def helper @helper end |
Instance Method Details
#delete ⇒ Object
31 32 33 34 35 36 |
# File 'lib/e621_export_downloader/export.rb', line 31 def delete return false unless check_downloaded FileUtils.rm(file_path) @downloaded = false true end |
#download ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/e621_export_downloader/export.rb', line 39 def download raise(ResolveError, "Export #{helper.type} for #{helper.format_date(date)} does not exist") unless exists? if check_downloaded helper.client.debug("using cached export for #{helper.type.serialize}", header: %W[export #{helper.format_date(date)}]) return file_path end helper.client.debug("downloading export for #{helper.type.serialize}", header: %W[export #{helper.format_date(date)}]) FileUtils.mkdir_p(Constants::TEMP_DIR) File.open(file_path, "wb") do |file| inflater = Zlib::Inflate.new(Zlib::MAX_WBITS + 16) res = helper.client.connection.get("#{file_name}.gz") do |req| req..on_data = proc do |chunk, _total| decompressed = inflater.inflate(chunk) file.write(decompressed) if decompressed && !decompressed.empty? end end file.write(inflater.finish) inflater.close file.close unless res.success? raise(ResolveError, "Failed to download export #{helper.type.serialize} for #{helper.format_date(date)}: #{res.status} #{res.reason_phrase}") end rescue # rubocop:disable Style/RescueStandardError FileUtils.rm_f(file_path) raise end @downloaded = true file_path end |
#exists? ⇒ Boolean
76 77 78 |
# File 'lib/e621_export_downloader/export.rb', line 76 def exists? helper.client.connection.head("#{file_name}.gz").success? end |
#read(&block) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/e621_export_downloader/export.rb', line 81 def read(&block) download unless check_downloaded helper.client.debug("reading export for #{helper.type.serialize}", header: %W[export #{helper.format_date(date)}]) total = line_count CSV.foreach(file_path, headers: true) do |row| args = [helper.parser.call(T.cast(row, CSV::Row).to_hash), total] args = args.slice(0, block.arity) if block.arity != -1 block.call(*T.unsafe(args)) end end |
#read_all ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/e621_export_downloader/export.rb', line 93 def read_all download unless check_downloaded helper.client.debug("reading all records for #{helper.type.serialize}", header: %W[export #{helper.format_date(date)}]) results = [] read do |record| results << record end results end |