Added support for txt files and support for a config file

This commit is contained in:
2026-07-15 21:52:48 -06:00
parent e82d92c39c
commit ef4bef11a8
2 changed files with 71 additions and 5 deletions
+69 -4
View File
@@ -1,22 +1,87 @@
require "option_parser"
require "http/client"
require "validator"
repeat = 0
def checkUrlFromFile(content) # Sorry for the crappy method name.
repeat = 0
working = 0
notWorking = 0
exist = true
config = File.read("config.txt")
urls = content.split("\n")
while repeat < urls.size
if Valid.url? urls[repeat]
begin
response = HTTP::Client.get(urls[repeat])
if config.includes?(response.status_code.to_s)
puts "#{urls[repeat]} is a valid URL and works. Status code: #{response.status_code}"
working += 1
else
puts "#{urls[repeat]} is a valid URL but does not work. Status Code: #{response.status_code}"
notWorking += 1
end
rescue Socket::Addrinfo::Error
puts "#{urls[repeat]} is a valid URL but does not exist."
notWorking += 1
end
else
puts "#{urls[repeat]} is not a valid URL"
notWorking += 1
end
repeat += 1
end
puts "=============================="
puts "Finished testing urls."
puts "A total of #{repeat} url(s) were tested."
puts "#{working}/#{repeat} url(s) were working."
puts "#{notWorking}/#{repeat} url(s) were not working."
exit 0
end
OptionParser.parse do |parser|
parser.banner = "Usage: rete [arguments] [urls]"
parser.on("-f FILE", "--file=FILE", "Checks all urls in a txt file.") { |file|
urls = File.read(file)
checkUrlFromFile urls
exit 0}
parser.on("-h", "--help", "Show this help") do
puts parser
exit
end
parser.invalid_option do |flag|
STDERR.puts "ERROR: #{flag} is not a valid option."
STDERR.puts parser
exit(1)
end
end
repeat = 0
working = 0
notWorking = 0
config = File.read("config.txt")
while repeat < ARGV.size
if Valid.url? ARGV[repeat]
response = HTTP::Client.get(ARGV[repeat])
if config.includes?(response.status_code.to_s)
puts "#{ARGV[repeat]} is a valid URL and works. #{response.status_code}"
puts "#{ARGV[repeat]} is a valid URL and works. Status code: #{response.status_code}"
working += 1
else
puts "#{ARGV[repeat]} is a valid URL but does not work. #{response.status_code}"
puts "#{ARGV[repeat]} is a valid URL but does not work. Status Code: #{response.status_code}"
notWorking += 1
end
elsif
puts "#{ARGV[repeat]} is not a valid URL"
notWorking += 1
end
repeat += 1
end
puts "=======================\nFinished"
puts "=============================="
puts "Finished testing urls."
puts "A total of #{repeat} url(s) were tested."
puts "#{working}/#{repeat} url(s) were working."
puts "#{notWorking}/#{repeat} url(s) were not working."