Skip to content

Commit

Permalink
Merge pull request #4 from Leszek-Sieminski/develop
Browse files Browse the repository at this point in the history
merging Develop to Master
  • Loading branch information
Leszek-Sieminski authored Jul 17, 2019
2 parents bca2574 + b8f9d1c commit 85b637b
Show file tree
Hide file tree
Showing 49 changed files with 410 additions and 416 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
1.R
testy.R
tabular_fun.R
RAhrefs.Rproj
helper_authorization\.R
.travis.yml
appveyor.yml
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.RData
.Ruserdata
*.csv
helper_authorization\.R
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Depends: R (>= 3.5.0)
Imports:
assertthat,
httr,
jsonlite
jsonlite,
testthat
Author: Leszek Siemiński [aut, cre]
Maintainer: Leszek Siemiński <[email protected]>
Description: Interface for interaction with the Ahrefs.com API through R.
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ export(rah_refdomains_new_lost_counters)
export(rah_refips)
export(rah_subscription_info)
import(assertthat)
importFrom(httr,GET)
importFrom(httr,add_headers)
importFrom(httr,content)
importFrom(httr,stop_for_status)
importFrom(jsonlite,fromJSON)
58 changes: 40 additions & 18 deletions R/auth.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# auth ------------------------------------------------------------------------
#' Authorize your Ahrefs API connection with a API Key (Token)
#'
#' @param api_key character string. Valid API key obtained at: https://ahrefs.com/api/profile
Expand All @@ -7,6 +6,12 @@
#' @return invisibly returns API token into environment variable AHREFS_AUTH_TOKEN and prints the status
#' @export
#'
#' @import assertthat
#' @importFrom httr GET
#' @importFrom httr add_headers
#' @importFrom httr content
#' @importFrom jsonlite fromJSON
#'
#' @examples
#' \dontrun{
#' rah_auth("ABCDEFGHIJKLMNOPQRST")
Expand All @@ -15,25 +20,42 @@ rah_auth <- function(
api_key,
verbose = TRUE
){
assertthat::noNA(api_key)
assertthat::not_empty(api_key)
assertthat::is.string(api_key)
# safety net ----------------------------------------------------------------
assert_that(
!is.na(api_key), noNA(api_key), not_empty(api_key), is.string(api_key),
nchar(api_key) > 0,
noNA(verbose), not_empty(verbose), assert_that(is.logical(verbose)))

# connecting to auth endpoint -----------------------------------------------
# x <- GET(url = 'https://apiv2.ahrefs.com/',
# add_headers(token = api_key))

response <- GET(url = paste0(
"https://apiv2.ahrefs.com/",
"?token=", api_key,
"&from=", "anchors",
"&target=", "ahrefs.com",
"&mode=", "domain",
# if (!is.null(metrics)) {paste0("&select=", paste(metrics, collapse = ","))},
"&limit=", 2,
"&output=json"#,
# if (!is.null(where)) {paste0("&where=", where)},
# if (!is.null(having)) {paste0("&having=", having)},
# if (!is.null(order_by)){paste0("&order_by=", order_by)}
))

assertthat::noNA(verbose)
assertthat::not_empty(verbose)
assertthat::assert_that(is.logical(verbose))
stop_for_status(response)
content <- content(response, type = "text", encoding = "UTF-8")
result <- fromJSON(content, simplifyVector = FALSE)

x <- httr::GET(url = 'https://apiv2.ahrefs.com/',
httr::add_headers(token = api_key))
# api_key sanity check ------------------------------------------------------
http_status_200 <- response$status_code == 200
no_hidden_error <- !("error" %in% names(jsonlite::fromJSON(httr::content(response, as = "text"))))
# is_df <- is.data.frame(result)

if (x$status_code == 200){
# saving enviromental variable ----------------------------------------------
if (http_status_200 & no_hidden_error){
Sys.setenv("AHREFS_AUTH_TOKEN" = api_key)
if (verbose) {
message("API authorized.")
}
} else {
message(paste0("Authorization error: HTTP status code ",
x$status_code,
". Check your api key."))
}
if (verbose) message("API authorized.")
} else stop(paste0("Authorization error: HTTP status code ", response$status_code,". Check your api key."))
}
30 changes: 10 additions & 20 deletions R/condition.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,16 @@
#' value = "10")
#' }
rah_condition <- function(column_name, operator, value, is_date = FALSE){
assert_that(is.logical(is_date), # date
not_empty(is_date),
!is.na(is_date),
!is.null(is_date),
not_empty(column_name), # column_name
!is.null(column_name),
!is.na(column_name),
is.string(column_name),
not_empty(value), # value
!is.null(value),
!is.na(value),
not_empty(operator), # operator
!is.null(operator),
!is.na(operator),
operator %in% c("EQUALS", "UNEQUALS", "LESS_THAN",
"LESS_OR_EQUAL", "GREATER_THAN",
"GREATER_OR_EQUAL", "SUBDOMAIN",
"SUBSTRING", "WORD"),
length(operator) == 1
)
# safety net ----------------------------------------------------------------
assert_that(
is.logical(is_date), not_empty(is_date), !is.na(is_date), !is.null(is_date),
not_empty(column_name), !is.null(column_name), !is.na(column_name),
is.string(column_name), not_empty(value), !is.null(value), !is.na(value),
not_empty(operator), !is.null(operator), !is.na(operator),
operator %in% c("EQUALS", "UNEQUALS", "LESS_THAN", "LESS_OR_EQUAL",
"GREATER_THAN", "GREATER_OR_EQUAL", "SUBDOMAIN", "SUBSTRING",
"WORD"), length(operator) == 1
)

if(!is_date){
x <- switch(
Expand Down
53 changes: 19 additions & 34 deletions R/downloader.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
#' @return list or nested list object
#'
#' @import assertthat
#' @importFrom httr GET
#' @importFrom httr stop_for_status
#' @importFrom httr content
#' @importFrom jsonlite fromJSON
#'
#' @examples
#' # do not use this function - instead use its wrappers (rah_<report_name>())
Expand Down Expand Up @@ -109,45 +113,26 @@ rah_downloader <- function(target,
# modes list ----------------------------------------------------------------
mode_vector <- c("exact", "domain", "subdomains", "prefix")

# safety net --------------------------------------------------------
assert_that(!is.null(target), # target
!is.na(target),
is.string(target),
grepl("\\.", target),
!is.null(report), # report
!is.na(report),
is.string(report),
report %in% report_vector,
!is.null(token), # token
!is.na(token),
is.string(token),
nchar(token) > 30,
!is.null(mode), # mode
!is.na(mode),
is.string(mode),
mode %in% mode_vector,
is.null(metrics) | is.vector(metrics, mode = "character"), # metrics
# if (!is.null(metrics)) {is.vector(metrics, mode = "character")}, # metrics
limit >= 1, # limit
limit %% 1 == 0,
is.number(limit)
# !is.na(having), # having
# is.string(having),
# !is.na(where), # where
# is.string(where)
)
# safety net ----------------------------------------------------------------
assert_that(
!is.null(target), !is.na(target), is.string(target), grepl("\\.", target),
!is.null(report), !is.na(report), is.string(report),
report %in% report_vector, !is.null(token), !is.na(token),
is.string(token), nchar(token) > 30, !is.null(mode), !is.na(mode),
is.string(mode), mode %in% mode_vector,
is.null(metrics) | is.vector(metrics, mode = "character"), limit >= 1,
limit %% 1 == 0, is.number(limit)
)

# order_by preparation ------------------------------------------------------
if (!is.null(order_by)) {
if (grepl("\\:", order_by)) {
order_by <- gsub(pattern = "\\:",
replacement = "%3A",
order_by)
order_by <- gsub("\\:", "%3A", order_by)
}
}

# downloading ---------------------------------------------------------------
response <- httr::GET(paste0(
response <- GET(paste0(
"https://apiv2.ahrefs.com/",
"?token=", token,
"&from=", report,
Expand All @@ -161,8 +146,8 @@ rah_downloader <- function(target,
if (!is.null(order_by)){paste0("&order_by=", order_by)}
))

httr::stop_for_status(response)
content <- httr::content(response, type = "text", encoding = "UTF-8")
result <- jsonlite::fromJSON(content, simplifyVector = FALSE)
stop_for_status(response)
content <- content(response, type = "text", encoding = "UTF-8")
result <- fromJSON(content, simplifyVector = FALSE)
return(result)
}
12 changes: 6 additions & 6 deletions R/rah_ahrefs_rank.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ rah_ahrefs_rank <- function(target,
having = NULL
){
data_list <- rah_downloader(
target = target,
report = "ahrefs_rank",
token = token,
mode = mode,
metrics = metrics,
limit = limit,
target = target,
report = "ahrefs_rank",
token = token,
mode = mode,
metrics = metrics,
limit = limit,
order_by = order_by,
where = where,
having = having)
Expand Down
31 changes: 16 additions & 15 deletions R/rah_anchors.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,23 @@
#' where = cond_where,
#' order_by = "refpages:desc")
#' }
rah_anchors <- function(target,
token = Sys.getenv("AHREFS_AUTH_TOKEN"),
mode = "domain",
metrics = NULL,
limit = 1000,
order_by = NULL,
where = NULL,
having = NULL
){
rah_anchors <- function(
target,
token = Sys.getenv("AHREFS_AUTH_TOKEN"),
mode = "domain",
metrics = NULL,
limit = 1000,
order_by = NULL,
where = NULL,
having = NULL)
{
data_list <- rah_downloader(
target = target,
report = "anchors",
token = token,
mode = mode,
metrics = metrics,
limit = limit,
target = target,
report = "anchors",
token = token,
mode = mode,
metrics = metrics,
limit = limit,
order_by = order_by,
where = where,
having = having)
Expand Down
19 changes: 10 additions & 9 deletions R/rah_anchors_refdomains.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,16 @@
#' where = cond_where,
#' order_by = "anchors:desc")
#' }
rah_anchors_refdomains <- function(target,
token = Sys.getenv("AHREFS_AUTH_TOKEN"),
mode = "domain",
metrics = NULL,
limit = 1000,
order_by = NULL,
where = NULL,
having = NULL
){
rah_anchors_refdomains <- function(
target,
token = Sys.getenv("AHREFS_AUTH_TOKEN"),
mode = "domain",
metrics = NULL,
limit = 1000,
order_by = NULL,
where = NULL,
having = NULL)
{
data_list <- rah_downloader(
target = target,
report = "anchors_refdomains",
Expand Down
2 changes: 0 additions & 2 deletions R/rah_backlinks.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@
#' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")}
#' }
#'
#' @source \url{https://ahrefs.com/api/documentation}
#'
#' @return data frame
#' @export
#'
Expand Down
19 changes: 10 additions & 9 deletions R/rah_backlinks_new_lost.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,16 @@
#' where = cond_where,
#' order_by = "domain_rating:desc")
#' }
rah_backlinks_new_lost <- function(target,
token = Sys.getenv("AHREFS_AUTH_TOKEN"),
mode = "domain",
metrics = NULL,
limit = 1000,
order_by = NULL,
where = NULL,
having = NULL
){
rah_backlinks_new_lost <- function(
target,
token = Sys.getenv("AHREFS_AUTH_TOKEN"),
mode = "domain",
metrics = NULL,
limit = 1000,
order_by = NULL,
where = NULL,
having = NULL)
{
data_list <- rah_downloader(
target = target,
report = "backlinks_new_lost",
Expand Down
20 changes: 10 additions & 10 deletions R/rah_backlinks_new_lost_counters.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
#' mode = "domain", metrics = NULL, limit = 1000, where = fin_cond, order_by = "first_seen:asc")}
#' }
#'
#' @source \url{https://ahrefs.com/api/documentation}
#'
#' @return data frame
#' @export
Expand Down Expand Up @@ -122,15 +121,16 @@
#' where = cond_where,
#' order_by = "links_external:desc")
#' }
rah_backlinks_new_lost_counters <- function(target,
token = Sys.getenv("AHREFS_AUTH_TOKEN"),
mode = "domain",
metrics = NULL,
limit = 1000,
order_by = NULL,
where = NULL,
having = NULL
){
rah_backlinks_new_lost_counters <- function(
target,
token = Sys.getenv("AHREFS_AUTH_TOKEN"),
mode = "domain",
metrics = NULL,
limit = 1000,
order_by = NULL,
where = NULL,
having = NULL)
{
data_list <- rah_downloader(
target = target,
report = "backlinks_new_lost_counters",
Expand Down
Loading

0 comments on commit 85b637b

Please sign in to comment.