-
Notifications
You must be signed in to change notification settings - Fork 607
/
check.r
executable file
·111 lines (75 loc) · 2.02 KB
/
check.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# Common errors
# -------------
# 400 Bad Request
# 403 Forbidden (e.g. Nature website)
# 404 Not Found
# 501 Not Implemented
# 999 LinkedIn being defensive
#
library(httr)
library(stringr)
f <- "check.log"
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()
# remove links that have been commented out
u <- str_remove_all(u, "<!--.*?-->")
# total number of links (made to match web.archive.org links only once)
t <- sum(str_count(u, "(?<!/)http"))
cat(t, "URLs, ")
l <- c(
# [foo](bar)
str_extract_all(u, "\\(http(.*?)\\)") %>%
lapply(str_replace_all, "^\\(|\\)$", "") %>%
unlist(),
# [foo]: bar
str_extract_all(u, "^\\[(.*)\\]: (.*)") %>%
unlist() %>%
str_replace("^\\[(.*)\\]: (.*)", "\\2")
)
stopifnot(length(l) == t)
} else {
cat("Source:", f, "\n")
l <- str_subset(stringi::stri_read_lines(f), "^http")
cat(length(l), "URLs, ")
}
l <- str_squish(sort(unique(l)))
cat(length(l), "unique\n")
cat("Ignoring", sum(str_detect(l, "^https://doi.org/")), "DOIs\n")
l <- str_subset(l, "^https://doi.org/", negate = TRUE)
sink(f, append = FALSE)
cat(as.character(Sys.time()), ": checking", length(l), "URLs\n\n")
sink()
for (i in l) {
x <- try(status_code(GET(i)), silent = TRUE)
if (!"try-error" %in% class(x) && x != 200) {
cat("X")
sink(f, append = TRUE)
cat(i, "\nStatus code:", x, "\n\n")
sink()
} else if ("try-error" %in% class(x)) {
cat("?")
sink(f, append = TRUE)
cat(i, "\nFailed to access\n\n")
sink()
} else {
cat(".")
}
if (!which(l == i) %% 50) {
cat("", length(l) - which(l == i), "left\n")
}
}
sink(f, append = TRUE)
f <- sum(str_count(stringi::stri_read_lines(f), "^http"))
cat(as.character(Sys.time()), ": done,", f, "errors.\n")
sink()
cat("\n", f, "errors\n")