Skip to content

Commit

Permalink
Merge pull request #299 from guohuansu/guohuansu-patch-1
Browse files Browse the repository at this point in the history
Add lines to detect user's ip, if the ip is in China, use the mirror site
  • Loading branch information
cboettig authored Nov 21, 2024
2 parents f503885 + 660a200 commit ed14218
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Description: A programmatic interface to 'FishBase', re-written
supports experimental access to 'SeaLifeBase' data, which contains
nearly 200,000 species records for all types of aquatic life not covered by
'FishBase.'
Version: 5.0.0
Version: 5.0.1
Encoding: UTF-8
License: CC0
Authors@R: c(person("Carl", "Boettiger",
Expand All @@ -19,7 +19,9 @@ Authors@R: c(person("Carl", "Boettiger",
person("Duncan", "Temple Lang", role = "aut"),
person("Peter", "Wainwright", role = "aut"),
person("Kevin", "Cazelles", role = "ctb",
comment = c(ORCID = "0000-0001-6619-9874")))
comment = c(ORCID = "0000-0001-6619-9874")),
person("Guohuan", "Su", role = "ctb",
comment = c(ORCID = "0000-0003-0091-9773")))
URL: https://docs.ropensci.org/rfishbase/, https://github.com/ropensci/rfishbase
BugReports: https://github.com/ropensci/rfishbase/issues
Depends:
Expand All @@ -28,6 +30,7 @@ Imports:
glue,
stringr,
purrr,
httr,
jsonlite,
dplyr,
duckdbfs,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ And constructed with the following guidelines:

For more information on SemVer, please visit http://semver.org/.

v 5.0.1
-------
Fix the issue faced by users in China who can't access the URL https://huggingface.co directly. When the user's ip address is detected to be in China, it will be automatically switched to the mirror URL https://hf-mirror.com

v 5.0.0
-------

Expand Down
26 changes: 26 additions & 0 deletions R/fb_tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@ available_releases <- function(server = c("fishbase", "sealifebase")) {
sv <- server_code(server)
repo <- "datasets/cboettig/fishbase"
path <- glue::glue("data/{sv}")
is_user_in_china <- function() {
response <- httr::GET("https://ipinfo.io")
if (httr::status_code(response) == 200) {
data <- jsonlite::fromJSON(httr::content(response, as = "text"))
country <- data$country
return(country == "CN")
} else FALSE
}

if (is_user_in_china()) {
hf <- "https://hf-mirror.com"
} else {
hf <- "https://huggingface.co"
}
branch <- "main"
versions <-
glue::glue("{hf}/api/{repo}/tree/{branch}/{path}") |>
Expand All @@ -85,7 +98,20 @@ hf_urls <- function(path = "data/fb/v24.07/parquet",
branch = "main"
) {

is_user_in_china <- function() {
response <- httr::GET("https://ipinfo.io")
if (httr::status_code(response) == 200) {
data <- jsonlite::fromJSON(httr::content(response, as = "text"))
country <- data$country
return(country == "CN")
} else FALSE
}

if (is_user_in_china()) {
hf <- "https://hf-mirror.com"
} else {
hf <- "https://huggingface.co"
}
paths <-
glue::glue("{hf}/api/{repo}/tree/{branch}/{path}") |>
jsonlite::read_json() |>
Expand Down

0 comments on commit ed14218

Please sign in to comment.