Skip to content

Instantly share code, notes, and snippets.

@tukachev
Created January 21, 2025 01:52
Show Gist options
  • Save tukachev/0c4542b67334085cf3fc8461ab97f984 to your computer and use it in GitHub Desktop.
Save tukachev/0c4542b67334085cf3fc8461ab97f984 to your computer and use it in GitHub Desktop.
300ya.ru API using R
# 300.ya.ru
# Sys.setenv("YA_TOKEN" = "") # your token
get_sharing_url <- function(article_url) {
token <- Sys.getenv("YA_TOKEN")
if (is.null(token) || token == "") {
stop("YA_TOKEN is empty, please set the environment variable")
}
endpoint <- 'https://300.ya.ru/api/sharing-url'
data <- list(article_url = article_url)
headers <- c('Authorization' = paste0('OAuth ', token))
response <- httr::POST(endpoint,
body = data,
encode = "json",
httr::add_headers(headers))
status_code <- httr::status_code(response)
if (status_code != 200) {
stop("Error: ", status_code)
}
res <- httr::content(response, "parsed")
sharing_url <- res$sharing_url
return(sharing_url)
}
get_sharing_url('https://habr.com/ru/companies/ods/articles/716918/')
#> get_sharing_url('https://habr.com/ru/companies/ods/articles/716918/')
#[1] "https://300.ya.ru/0r2Hecs7"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment