Skip to content

Commit

Permalink
# 3.2.0
Browse files Browse the repository at this point in the history
* Исправлена проблема дублирования строк в функции `vkGetUserWall()`, по [тикету #31](#31).
* Доработана функция `vkGetAdStatistics()` согласно [тикету #33](#33).
  • Loading branch information
selesnow committed Oct 15, 2021
1 parent f6469fe commit b78f48d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: rvkstat
Type: Package
Title: R Interface to API 'vk.com'
Version: 3.1.0
Date: 2021-09-08
Version: 3.2.0
Date: 2021-10-15
Author: Alexey Seleznev
Maintainer: Alexey Seleznev <selesnow@gmail.com>
Description: Load data from vk.com api about your communiti users and views,
Expand All @@ -20,4 +20,3 @@ Language: ru
Encoding: UTF-8
BugReports: https://github.com/selesnow/rvkstat/issues
URL: https://selesnow.github.io/rvkstat/
License: GPL-2
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
№ 3.1.0
# 3.2.0

* Исправлена проблема дублирования строк в функции `vkGetUserWall()`, по [тикету #31](https://github.com/selesnow/rvkstat/issues/31).
* Доработана функция `vkGetAdStatistics()` согласно [тикету #33](https://github.com/selesnow/rvkstat/issues/33).

# 3.1.0

Пакет переведён на работу с API версии 5.131.

Expand Down
62 changes: 39 additions & 23 deletions R/vkGetAdStatistics.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
vkGetAdStatistics <- function(
account_id = vkCurrentAdAccount(),
ids_type = c("office", "client", "campaign", "ad"),
ids,
period = c("day", "week", "month", "year", "overall"),
date_from = Sys.Date() - 30,
date_to = Sys.Date(),
username = getOption("rvkstat.username"),
api_version = getOption("rvkstat.api_version"),
token_path = vkTokenPath(),
access_token = getOption("rvkstat.access_token")
)
{
account_id = vkCurrentAdAccount(),
ids_type = c("office", "client", "campaign", "ad"),
ids,
period = c("day", "week", "month", "year", "overall"),
date_from = Sys.Date() - 30,
date_to = Sys.Date(),
username = getOption("rvkstat.username"),
api_version = getOption("rvkstat.api_version"),
token_path = vkTokenPath(),
access_token = getOption("rvkstat.access_token")
)
{

# auth
if ( is.null(access_token) ) {
Expand All @@ -32,7 +32,7 @@ vkGetAdStatistics <- function(
# check args
ids_type <- match.arg(ids_type)
period <- match.arg(period)

# transfroming filter for campaigns in json
if(period == "month"){
date_from <- ifelse( as.character(date_from) != 0, format(as.Date(as.character(date_from)), "%Y-%m"), 0)
Expand Down Expand Up @@ -70,7 +70,7 @@ vkGetAdStatistics <- function(

# ids in this iteraction
ids <- paste0(i, collapse = ",")

# check if flood control
Sys.sleep(2) #vk limit

Expand All @@ -79,7 +79,7 @@ vkGetAdStatistics <- function(
account_id,
access_token,
api_version
)
)

# API request
answer <- GET("https://api.vk.com/method/ads.getStatistics",
Expand All @@ -104,22 +104,38 @@ vkGetAdStatistics <- function(
stop(paste0("Error ", dataRaw$error$error_code," - ", dataRaw$error$error_msg))
}

message(dataRaw)

#if (length(dataRaw$response[[1]]$stats) > 0)
# {
# parsing
temp <- tibble(response = dataRaw$response) %>%
unnest_wider("response") %>%
unnest_longer("stats") %>%
unnest_wider("stats")
tryCatch(

{
temp <- tibble(response = dataRaw$response) %>%
unnest_wider("response") %>%
unnest_longer("stats") %>%
unnest_wider("stats")

# append to result
result <- append(result, list(temp))
# }
}
, error = function(ex)
{
# here 'ex' is an instance of 'simpleError'
message(as.character(ex))
message(ex$message)
})

# append to result
result <- append(result, list(temp))

Sys.sleep(2) #vk limit

}

# collect result
result <- bind_rows(result)

message(result)
# data type mismatch handler
if (nrow(result) > 0) {

Expand All @@ -133,4 +149,4 @@ vkGetAdStatistics <- function(

# end
return(result)
}
}
7 changes: 4 additions & 3 deletions R/vkGetUserWall.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ vkGetUserWall <- function(
# bind result
result <-
bind_rows(result) %>%
unnest_longer("comments", indices_include = FALSE) %>%
unnest_longer("likes", indices_include = FALSE) %>%
unnest_wider("comments", names_sep = '_') %>%
unnest_wider("likes", names_sep = '_') %>%
unnest_wider("reposts", names_sep = '_') %>%
unnest_longer("donut", indices_include = FALSE)
unnest_wider("donut", names_sep = '_') %>%
unnest_longer("views", indices_include = FALSE)

# check views
if ( "views" %in% names(result) ) {
Expand Down

0 comments on commit b78f48d

Please sign in to comment.