-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
401118
committed
Jul 15, 2024
1 parent
a9d15b9
commit 6291795
Showing
10 changed files
with
121 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,4 @@ Suggests: | |
testthat, | ||
rmarkdown, | ||
covr | ||
RoxygenNote: 7.2.0 | ||
RoxygenNote: 7.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
|
||
.scrap_specific_file <- function( | ||
ext, | ||
link, | ||
path | ||
) { | ||
|
||
urls_containing_files <- weblink_scrap( | ||
link, | ||
contain = ext | ||
) | ||
|
||
files_to_consider <- urls_containing_files %>% | ||
purrr::keep(function(x) { | ||
tools::file_ext(x) == ext | ||
}) | ||
|
||
for (i in seq_along(files_to_consider)) { | ||
|
||
tryCatch( | ||
expr = { | ||
download.file(files_to_consider[i], | ||
destfile = paste0(path, "/", basename(files_to_consider[i])), | ||
mode = "wb" | ||
) | ||
|
||
}, | ||
|
||
error = function(cond) { | ||
|
||
if (!has_internet()) { | ||
|
||
message(paste0("Please check your internet connexion: ", cond)) | ||
|
||
return(NA) | ||
|
||
} else if (grepl("current working directory", cond) || | ||
grepl("HTTP error 404", cond)) { | ||
|
||
message(paste0("The URL doesn't seem to be a valid one: ", link)) | ||
|
||
message(paste0("Here the original error message: ", cond)) | ||
|
||
return(NA) | ||
|
||
} else { | ||
|
||
message(paste0("Undefined Error: ", cond)) | ||
return(NA) | ||
|
||
} | ||
} | ||
|
||
|
||
) | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
|
||
#' Scrape and download pdf files from a Web Page | ||
#' | ||
#' @param link the link of the web page | ||
#' @param pdfpath the path where to save the PDF files. Defaults to the current directory | ||
#' @param askRobot logical. Should the function ask the robots.txt if we're allowed or not to scrape the web page ? Default is FALSE. | ||
#' | ||
#' @return called for the side effect of downloading PDF files | ||
#' @export | ||
#' | ||
|
||
pdfs_scrap <- function( | ||
link, | ||
pdfpath = getwd(), | ||
askRobot = FALSE | ||
) { | ||
|
||
|
||
if (pdfpath != getwd() && !dir.exists(pdfpath)) { | ||
stop("the path: ", pdfpath, " doesn't seem to exist !") | ||
} | ||
|
||
if (askRobot) { | ||
|
||
if (paths_allowed(link) == TRUE) { | ||
message(green("the robot.txt doesn't prohibit scraping this web page")) | ||
|
||
} else { | ||
message(bgRed( | ||
"WARNING: the robot.txt doesn't allow scraping this web page" | ||
)) | ||
|
||
} | ||
} | ||
|
||
.scrap_specific_file( | ||
link = link, | ||
path = pdfpath, | ||
ext = "pdf" | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.