Last active
March 26, 2024 18:21
-
-
Save davidleandro/516f86884b05086bc2287846d458836d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
require 'json' | |
require 'fileutils' | |
class CanalEducacaoAPI | |
include HTTParty | |
base_uri '' | |
def initialize() | |
@options = { headers: { "Content-Type" => "application/json", "Authorization": "" } } | |
end | |
def make_request(path) | |
attempts = 0 | |
max_attempts = 5 | |
begin | |
response = self.class.get(path, @options) | |
return response if response.success? | |
raise 'Request failed' | |
rescue => e | |
attempts += 1 | |
puts "Tentativa #{attempts} para o path #{path} falhou: #{e}. Tentando novamente..." if attempts < max_attempts | |
sleep(5) if attempts < max_attempts | |
retry if attempts < max_attempts | |
puts "Falha após #{max_attempts} tentativas para o path #{path}." | |
return nil | |
end | |
end | |
def fetch_schools | |
make_request('/escolas') | |
end | |
def fetch_classes(school_id) | |
make_request("/escolas/#{school_id}/turmas") | |
end | |
def fetch_class_details(class_id) | |
make_request("/turmas/#{class_id}") | |
end | |
def fetch_class_lessons(class_id) | |
make_request("/turmas/#{class_id}/aulas") | |
end | |
def fetch_lesson_details(lesson_id) | |
make_request("/aulas/#{lesson_id}") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'fileutils' | |
consolidated_data = [] | |
Dir.glob("canal_educacao/*.json").each do |file_path| | |
file_content = File.read(file_path) | |
item = JSON.parse(file_content) | |
consolidated_data.push(item) | |
end | |
File.open('consolidated_canal_educacao.json', 'w') do |file| | |
file.write(JSON.pretty_generate(consolidated_data)) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
api = CanalEducacaoAPI.new | |
schools = api.fetch_schools.parsed_response | |
FileUtils.mkdir_p('canal_educacao') unless Dir.exist?('canal_educacao') | |
schools.each do |school| | |
classes = api.fetch_classes(school['id']).parsed_response | |
classes.each do |class_data| | |
class_file_name = "canal_educacao/#{class_data['codigo']}.json" | |
next if File.exist?(class_file_name) | |
puts "Start processing class #{class_data['codigo']}" | |
class_detail = api.fetch_class_details(class_data['codigo']).parsed_response | |
# class_detail['aulas'] = api.fetch_class_lessons(class_data['codigo']).parsed_response | |
class_detail['aulas'] = api.fetch_class_lessons(class_data['codigo']).parsed_response.map do |lesson| | |
lesson_detail = api.fetch_lesson_details(lesson['id']).parsed_response | |
lesson_detail | |
end | |
File.open(class_file_name, 'w') do |file| | |
file.write(class_detail.to_json) | |
end | |
puts "Processed and saved class #{class_data['codigo']}" | |
sleep(5.seconds) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment