awesome-network-analysis/check.r

106 lines
1.9 KiB
R
Raw Normal View History

2017-01-02 13:01:11 -05:00
#
# Common errors
# -------------
# 400 Bad Request
# 403 Forbidden (e.g. Nature website)
# 404 Not Found
# 501 Not Implemented
2017-01-02 13:46:42 -05:00
# 999 LinkedIn being defensive
2017-01-02 13:01:11 -05:00
#
2017-01-01 12:54:03 -05:00
library(httr)
library(stringr)
f <- "check.log"
2017-01-01 12:54:03 -05:00
if (!file.exists(f)) {
u <- str_c(
"https://raw.githubusercontent.com/",
"briatte/awesome-network-analysis/",
"master/README.md"
)
cat("Source:", u, "\n")
u <- GET(u) %>%
content("text") %>%
str_split("\\n") %>% # so as to find [foo]: bar links
unlist
# total number of links
2017-03-14 04:40:30 -04:00
t <- sum(str_count(u, "http"))
cat(t, "URLs, ")
l <- c(
# [foo](bar)
2018-10-20 05:18:59 -04:00
str_extract_all(u, "\\(http(.*?)\\)") %>% # using regular expression see https://gist.github.com/vitorbritto/9ff58ef998100b8f19a0
# for more infos
lapply(str_replace_all, "^\\(|\\)$", "") %>%
unlist,
# [foo]: bar
str_extract_all(u, "^\\[(.*)\\]: (.*)") %>%
unlist %>%
str_replace("^\\[(.*)\\]: (.*)", "\\2")
)
stopifnot(length(l) == t)
} else {
cat("Source:", f, "\n")
2017-03-14 04:40:30 -04:00
l <- str_subset(readLines(f), "^http")
cat(length(l), "URLs, ")
}
2017-01-01 12:54:03 -05:00
2017-03-14 04:40:30 -04:00
l <- sort(unique(l))
2017-01-01 13:16:31 -05:00
2017-01-01 12:54:03 -05:00
cat(length(l), "unique\n")
sink(f, append = FALSE)
2017-01-01 13:16:31 -05:00
cat(as.character(Sys.time()), ": checking", length(l), "URLs\n\n")
sink()
2017-01-01 12:54:03 -05:00
for (i in l) {
2017-03-14 04:40:30 -04:00
x <- try(status_code(GET(i)), silent = TRUE)
2017-01-01 12:54:03 -05:00
if (!"try-error" %in% class(x) && x != 200) {
2017-01-01 13:16:31 -05:00
cat("X")
sink(f, append = TRUE)
2017-01-01 13:16:31 -05:00
cat(i, "\nStatus code:", x, "\n\n")
2017-01-01 13:05:09 -05:00
sink()
2017-01-01 13:16:31 -05:00
2017-03-14 04:40:30 -04:00
} else if ("try-error" %in% class(x)) {
2017-01-01 13:16:31 -05:00
cat("?")
sink(f, append = TRUE)
2017-01-01 13:16:31 -05:00
cat(i, "\nFailed to access\n\n")
2017-01-01 13:05:09 -05:00
sink()
2017-01-01 13:16:31 -05:00
2017-01-01 12:54:03 -05:00
} else {
2017-01-01 13:16:31 -05:00
2017-01-01 12:54:03 -05:00
cat(".")
2017-01-01 13:16:31 -05:00
2017-01-01 12:54:03 -05:00
}
2017-03-14 04:40:30 -04:00
if (!which(l == i) %% 50) {
2017-01-01 13:16:31 -05:00
cat("", length(l) - which(l == i), "left\n")
2017-03-14 04:40:30 -04:00
}
2017-01-01 13:16:31 -05:00
2017-01-01 12:54:03 -05:00
}
2017-01-01 13:16:31 -05:00
sink(f, append = TRUE)
2017-01-01 13:16:31 -05:00
cat(as.character(Sys.time()), ": done.\n")
sink()
2017-03-14 04:40:30 -04:00
cat("\nFound", sum(str_count(readLines(f), "^http")), "problems.\n")