Added support for txt files and support for a config file
This commit is contained in:
@@ -4,3 +4,4 @@
|
|||||||
/.shards/
|
/.shards/
|
||||||
*.dwarf
|
*.dwarf
|
||||||
config.txt
|
config.txt
|
||||||
|
test.txt
|
||||||
+69
-4
@@ -1,22 +1,87 @@
|
|||||||
|
require "option_parser"
|
||||||
require "http/client"
|
require "http/client"
|
||||||
require "validator"
|
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")
|
config = File.read("config.txt")
|
||||||
|
|
||||||
while repeat < ARGV.size
|
while repeat < ARGV.size
|
||||||
if Valid.url? ARGV[repeat]
|
if Valid.url? ARGV[repeat]
|
||||||
response = HTTP::Client.get(ARGV[repeat])
|
response = HTTP::Client.get(ARGV[repeat])
|
||||||
if config.includes?(response.status_code.to_s)
|
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
|
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
|
end
|
||||||
elsif
|
elsif
|
||||||
puts "#{ARGV[repeat]} is not a valid URL"
|
puts "#{ARGV[repeat]} is not a valid URL"
|
||||||
|
notWorking += 1
|
||||||
end
|
end
|
||||||
repeat += 1
|
repeat += 1
|
||||||
end
|
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."
|
||||||
Reference in New Issue
Block a user