Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try using the public github api if rpkg-api is down #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* `get_wikipidia()` renamed to `get_wikipedia()`.
* `valid_package_name()` now correctly checks a package name according to CRAN policy (@KevCaz, #61).
* `available_on_github()` now uses the public GitHub API if the rpkg-api is down (@patrickvossler18, #69).

# available 1.0.4

Expand Down
37 changes: 29 additions & 8 deletions R/github.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,35 @@ available_on_github <- function(name) {
}

gh_pkg <- memoise::memoise(function(pkg) {
res <- jsonlite::fromJSON(paste0("http://rpkg-api.gepuro.net/rpkg?q=", pkg))
if (length(res) == 0) {
return(data.frame(pkg_name = character(), pkg_location = character(), pkg_org = character()))
}
res$pkg_location <- res$pkg_name
res$pkg_org <- vapply(strsplit(res$pkg_location, "/"), `[[`, character(1), 1)
res$pkg_name <- vapply(strsplit(res$pkg_location, "/"), `[[`, character(1), 2)
res[!(res$pkg_org == "cran" | res$pkg_org == "Bioconductor-mirror"), ]
tryCatch({
rpkg_res <- jsonlite::fromJSON(paste0("http://rpkg-api.gepuro.net/rpkg?q=", pkg))
if (length(rpkg_res) == 0) {
return(data.frame(pkg_name = character(), pkg_location = character(), pkg_org = character()))
}
rpkg_res$pkg_location <- rpkg_res$pkg_name
rpkg_res$pkg_org <- vapply(strsplit(rpkg_res$pkg_location, "/"), `[[`, character(1), 1)
rpkg_res$pkg_name <- vapply(strsplit(rpkg_res$pkg_location, "/"), `[[`, character(1), 2)
rpkg_res[!(rpkg_res$pkg_org == "cran" | rpkg_res$pkg_org == "Bioconductor-mirror"), ]
},
error = function(cond){
message("Got an error trying to use rpkg-api, trying GitHub's api")
message("Here's the original error message:")
message(paste0(cond, "\n"))

gh_api_res <- jsonlite::fromJSON(paste0("https://api.github.com/search/repositories?q=",
pkg,"+language:R&sort=stars&order=desc"))$items

if (length(gh_api_res) == 0) {
return(data.frame(pkg_name = character(), pkg_location = character(), pkg_org = character()))
}

gh_res <- data.frame(pkg_location = gh_api_res$full_name)
gh_res$pkg_org <- vapply(strsplit(gh_res$pkg_location, "/"), `[[`, character(1), 1)
gh_res$pkg_name <- vapply(strsplit(gh_res$pkg_location, "/"), `[[`, character(1), 2)
gh_res[!(gh_res$pkg_org == "cran" | gh_res$pkg_org == "Bioconductor-mirror"), ]


})
})

#' @export
Expand Down