SU2-2 | Скрипт интегрирован в sidekiq, создан простой интерфейс на взаимодействие с SK, тест на нескольких ИНН
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
require 'zip'
|
||||
require 'stringio'
|
||||
|
||||
|
||||
class FileProcessorJob
|
||||
include Sidekiq::Job
|
||||
|
||||
def perform(inn, output_path)
|
||||
Rails.logger.info "=== Начало загрузки для ИНН: #{inn} ==="
|
||||
download_inn(inn, output_path)
|
||||
Rails.logger.info "=== Завершено для ИНН: #{inn} ==="
|
||||
rescue => e
|
||||
Rails.logger.error "=== ОШИБКА: #{e.message} ==="
|
||||
Rails.logger.error e.backtrace.first(5).join("\n")
|
||||
raise
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get_content(url, json_need = false)
|
||||
headers = {
|
||||
'User-Agent' => "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
|
||||
'Accept-Language' => 'en-US,en;q=0.9',
|
||||
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
|
||||
}
|
||||
content = HTTParty.get(url, headers: headers).body
|
||||
json_need ? JSON.parse(content) : content
|
||||
end
|
||||
|
||||
def download_inn(inn, output)
|
||||
url = "https://bo.nalog.gov.ru/advanced-search/organizations/search?query=#{inn}&page=0&size=20"
|
||||
content = get_content(url, true)
|
||||
id = content['content'][0]['id']
|
||||
|
||||
url = "https://bo.nalog.gov.ru/nbo/organizations/#{id}/bfo/"
|
||||
content = get_content(url, true)
|
||||
periods_and_details_ids = content.map { |item| [item['period'], item['typeCorrections'][0]['correction']['id']] }
|
||||
|
||||
periods_and_details_ids.each do |period, details_id|
|
||||
url = "https://bo.nalog.gov.ru/download/bfo/#{id}?auditReport=false&balance=true"\
|
||||
"&capitalChange=true&clarification=false&targetedFundsUsing=false&detailsId=#{details_id}"\
|
||||
"&financialResult=true&fundsMovement=true&type=XLS&period=#{period}"
|
||||
content = get_content(url)
|
||||
|
||||
zip_file = Zip::File.open_buffer(StringIO.new(content))
|
||||
zip_file.each do |entry|
|
||||
output_file = File.join(output, entry.name)
|
||||
FileUtils.mkdir_p(File.dirname(output_file))
|
||||
zip_file.extract(entry, output_file) { true } # перезаписывать если существует
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user