Skip to content

Commit

Permalink
fix download of auphonic metadata file (#455)
Browse files Browse the repository at this point in the history
fixes #454
  • Loading branch information
madmas authored Dec 29, 2024
1 parent a4ce77c commit f7c88eb
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions serious/lib/serious.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,34 @@ def self.get_chapters(url)


def self.get_metadata(url)
metadata = nil
return metadata if url.nil?
return nil if url.nil? || url.strip.empty?

begin
uri = URI.parse(url)
#create connection
Net::HTTP.start(uri.host, 80, {read_timeout: 10, open_timeout: 10}) {|http|
http.read_timeout = 10
http.open_timeout = 10
response = http.get(uri.path)
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", read_timeout: 10, open_timeout: 10) do |http|
response = http.get(uri)

#get data
if response.kind_of? Net::HTTPSuccess
metadata = JSON.parse(response.body.force_encoding("UTF-8"))
if response.is_a?(Net::HTTPSuccess)
return JSON.parse(response.body.force_encoding('UTF-8'))
else
puts "Failed to fetch metadata. HTTP Status: #{response.code}"
end
}
rescue Exception => e
metadata = nil
puts url
puts e
end
rescue URI::InvalidURIError => e
puts "Invalid URL: #{url}"
puts e.message
rescue JSON::ParserError => e
puts "Failed to parse JSON from URL: #{url}"
puts e.message
rescue Net::OpenTimeout, Net::ReadTimeout => e
puts "Timeout error while fetching metadata from URL: #{url}"
puts e.message
rescue StandardError => e
puts "Unexpected error occurred while fetching metadata from URL: #{url}"
puts e.message
end

metadata
nil
end
end

Expand Down

0 comments on commit f7c88eb

Please sign in to comment.