check.jl 2.32 KB
Newer Older
Jan Klass's avatar
Jan Klass committed
1 2 3
function checkurl(data::CrawlData, url)
    push!(data.urlsvisited, url)

4 5
    res = HTTP.get(url; readtimeout=2, redirect=false, status_exception=false, require_ssl_verification=false)
    if res.status == 200
Jan Klass's avatar
Jan Klass committed
6
        push!(data.urlsvalid, url)
7
    elseif HTTP.isredirect(res)
Jan Klass's avatar
Jan Klass committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
        push!(data.urls3xx, url)

        target = nothing
        for (key, value) in res.headers
            if lowercase(key) == "location"
                target = value
                break
            end
        end
        if target == nothing
            @warn "No redirect target on redirect at $url "
            push!(data.urlsfail, url)
        else
            @info "Checking redirect target $target …"
            checkurl(data, target)
        end
24
    else
Jan Klass's avatar
Jan Klass committed
25
        push!(data.urlsfail, url)
26 27 28 29 30 31 32 33 34
    end
end

function rebase(url, base)
    m = match(r"^(?<protocol>[a-zA-Z0-9]+)\:\/\/(?<host>[^\/]+)\/(?<path>.*)$", url)
    return base * m[:path]
end

function check(base)
Jan Klass's avatar
Jan Klass committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48
    data = CrawlData()
    fnin::Filenames=Filenames()
    fnout = Filenames(
        "new.sitemap.urls.xml",
        "new.sitemap.visited.xml",
        "new.sitemap.valid.xml",
        "new.sitemap.404.xml",
        "new.sitemap.3xx.xml",
        "new.sitemap.external.xml",
        "new.sitemap.fail.xml",
        "new.sitemap.ignored.xml",
    )
    #read(data.urls, fnin.urls)
    read(data.urls, fnin.urlsvalid)
49

Jan Klass's avatar
Jan Klass committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    read(data.urlsvisited, fnout.urlsvisited)
    read(data.urlsvalid, fnout.urlsvalid)
    read(data.urls404, fnout.urls404)
    read(data.urls3xx, fnout.urls3xx)
    read(data.urlsfail, fnout.urlsfail)

    left = length(data.urls)
    limit = 1000
    i = 0
    @info "Checking $(left) URLs, $(length(data.urlsvisited)) already checked …"
    for url in data.urls
        if i > limit
            break
        end
        if left % 100 == 0
            @info "$left left"
        end
        left = left - 1
        if in(url, data.urlsvisited) == false
            i = i + 1
            if length(base) > 0
                @debug "Checking URL $url …"
                url = rebase(url, base)
                checkurl(data, url)
            else
                @debug "Checking URL $url …"
                checkurl(data, url)
            end
78 79
        end
    end
Jan Klass's avatar
Jan Klass committed
80 81

    write_crawldata(data, fnout)
82
end