this repo has no description
at main 2.4 kB view raw
1#!/usr/bin/env ruby 2require "uri" 3require "net/http" 4require "json" 5require "set" 6require "yaml" 7 8require "bundler/inline" 9gemfile true do 10 source 'https://rubygems.org' 11 gem 'parallel' 12end 13 14# dumb websites 15# maps hosts to allowed >=400 http status codes 16exceptions = { 17 "twitter.com": 400, 18 "www.fiverr.com": 403, 19 "distrokid.com": 403, 20 "www.amazon.com": 500, 21 "evann.tech": 0, 22} 23 24 25check_every = 3600 / 8 # seconds 26 27 28def log_error msg 29 File.write "deadlinks.log", "[#{Time.now}] #{msg}\n", mode: 'a+' 30end 31 32def parse_uri uri 33 if uri !~ /https?:\/\// then uri = "https://ewen.works#{uri}" end 34 return URI.parse URI::Parser.new.escape uri 35end 36 37def check_broken_links database, exceptions 38 links = Set.new 39 40 database.each do |id, work| 41 work['content'].each do |language, localized| 42 localized['blocks'].each do |block| 43 if block['type'] == 'link' 44 links.add? parse_uri block['url'] 45 end 46 end 47 end 48 end 49 50 YAML.load_file("./tags.yaml").each do |tag| 51 if tag.has_key? "learn more at" 52 links.add? parse_uri tag["learn more at"] 53 end 54 end 55 56 YAML.load_file("./technologies.yaml").each do |tag| 57 if tag.has_key? "learn more at" 58 links.add? parse_uri tag["learn more at"] 59 end 60 end 61 62 return Parallel.map(links) do |url| 63 begin 64 puts "Checking #{url}" 65 response = (Net::HTTP.get_response url) 66 if response.code.to_i >= 400 67 unless exceptions.has_key? url.host.to_sym and response.code.to_i == exceptions[url.host.to_sym] 68 log_error "#{url} broken: got status #{response.code}" 69 url 70 end 71 end 72 rescue StandardError => ex 73 unless exceptions.has_key? url.host.to_sym and exceptions[url.host.to_sym] == 0 74 log_error "#{url}: utterly broken: #{ex}" 75 url 76 end 77 end 78 end.compact 79end 80 81def push_kuma_status error_messages 82 uptime_kuma_push_url = URI.parse ARGV[1] 83 push_params = { 84 "status": if error_messages.empty? then "up" else "down" end, 85 "msg": if error_messages.empty? then "OK" else error_messages.join " \n" end, 86 "ping": "" 87 } 88 89 uptime_kuma_push_url.query = URI.encode_www_form push_params.to_a 90 91 Net::HTTP.get_response uptime_kuma_push_url 92end 93 94while true 95 database = JSON.load (File.open ARGV[0]) 96 broken_links = check_broken_links database, exceptions 97 push_kuma_status broken_links 98 puts "Will check again in #{check_every} seconds…" 99 sleep check_every 100end