您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

validate_links.rb 1.6 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env ruby
  2. require 'httparty'
  3. require 'ruby-progressbar'
  4. require 'uri'
  5. allowed_codes = [200, 302, 403]
  6. args = ARGV
  7. filename = args[0]
  8. contents = File.open(filename, 'rb') { |f| f.read }
  9. raw_links = URI.extract(contents, ['http', 'https'])
  10. # Remove trailing ')' from entry URLs
  11. links = []
  12. raw_links.each do |link|
  13. if link.end_with?(')')
  14. links.push(link[0...-1])
  15. else
  16. links.push(link)
  17. end
  18. end
  19. fails = []
  20. # Fail on any duplicate elements
  21. dup = links.select{|element| links.count(element) > 1}
  22. if dup.uniq.length > 0
  23. dup.uniq.each do |e|
  24. fails.push("(DUP): #{e}")
  25. end
  26. end
  27. # Remove any duplicates from array
  28. links = links.uniq
  29. count = 0
  30. total = links.length
  31. progressbar = ProgressBar.create(:total => total)
  32. # GET each link and check for valid response code from allowed_codes
  33. links.each do |link|
  34. begin
  35. count += 1
  36. res = HTTParty.get(link, timeout: 10)
  37. if res.code.nil?
  38. fails.push("(NIL): #{link}")
  39. next
  40. end
  41. if !allowed_codes.include?(res.code)
  42. fails.push("(#{res.code}): #{link}")
  43. end
  44. rescue Net::ReadTimeout
  45. fails.push("(TMO): #{link}")
  46. rescue OpenSSL::SSL::SSLError
  47. fails.push("(SSL): #{link}")
  48. rescue SocketError
  49. fails.push("(SOK): #{link}")
  50. rescue
  51. fails.push("(ERR): #{link}")
  52. end
  53. progressbar.increment
  54. end
  55. puts "#{count}/#{total} links checked"
  56. if fails.length <= 0
  57. puts "all links valid"
  58. exit(0)
  59. else
  60. puts "-- RESULTS --"
  61. fails.each do |e|
  62. puts e
  63. end
  64. exit(1)
  65. end