Enhance SimilarWeb test (#7487)

pull/7490/head
Carlgo11 1 year ago committed by GitHub
parent 56b719a3f2
commit 1d8b40de47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -24,16 +24,16 @@ end
# Similarweb API handling
module Similarweb
def self.connect(url)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.request(Net::HTTP::Get.new(url, { 'Accept' => 'application/json' }))
def self.api_key
key = ENV['SIMILARWEB_API_KEY']
raise('Similarweb API key not set') if key.nil?
keys = key.split(' ')
keys[rand(0..(keys.length - 1))]
end
def self.fetch(site)
apikey = ENV['SIMILARWEB_API_KEY']
url = URI("https://api.similarweb.com/v1/similar-rank/#{site}/rank?api_key=#{apikey}")
response = connect(url)
response = Net::HTTP.get_response URI("https://api.similarweb.com/v1/similar-rank/#{site}/rank?api_key=#{api_key}")
raise("#{site} doesn't have a Similarweb ranking") if response.code.eql? '404'
raise("(#{response.code}) Request failed.") unless response.code.eql? '200'
@ -43,16 +43,17 @@ module Similarweb
end
end
raise('Similarweb API key not set') if ENV['SIMILARWEB_API_KEY'].nil?
status = 0
# Fetch changes
diff = `git diff origin/master...HEAD entries/ | sed -n 's/^+.*"domain"[^"]*"\\(.*\\)".*/\\1/p'`
# Strip and loop through diff
diff.split("\n").each do |site|
diff.split("\n").each.with_index do |site, i|
sleep 2 if i.positive?
domain = Addressable::URI.parse("https://#{site}").domain
rank = Cache.fetch(domain) || Similarweb.fetch(domain)
raise("Global rank #{rank} of #{site} is above the maximum rank of 200K") if rank.to_i > 200_000
failure = rank.to_i > 200_000
puts "\e[#{failure ? '31' : '32'}m#{domain} - #{rank}\e[39m"
raise("Global rank #{rank} of #{domain} is above the maximum rank of 200K") if failure
rescue StandardError => e
puts "\e[31m#{e.message}\e[39m"
status = 1

Loading…
Cancel
Save