Skip to content

Commit

Permalink
Добавлены функции запроса справочников университетов, школ и станций …
Browse files Browse the repository at this point in the history
…метро: `vkGetDbUniversities()`, `vkGetDbSchools()`, `vkGetDbMetroStations()`.
  • Loading branch information
selesnow committed Oct 16, 2021
1 parent da10b8d commit 5127943
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 22 deletions.
45 changes: 23 additions & 22 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
Package: rvkstat
Type: Package
Title: R Interface to API 'vk.com'
Version: 3.2.0
Date: 2021-10-15
Author: Alexey Seleznev
Maintainer: Alexey Seleznev <[email protected]>
Description: Load data from vk.com api about your communiti users and views,
ads performance, post on user wall and etc. For more information
see API Documentation <https://vk.com/dev/first_guide>.
License: GPL-2
Depends: R (>= 3.5.0)
Imports: dplyr (>= 1.0.0),
tidyr (>= 1.0.0),
jsonlite,
httr,
stringr,
lgr
Language: ru
Encoding: UTF-8
BugReports: https://github.com/selesnow/rvkstat/issues
URL: https://selesnow.github.io/rvkstat/
Package: rvkstat
Type: Package
Title: R Interface to API 'vk.com'
Version: 3.2.0
Date: 2021-10-15
Author: Alexey Seleznev
Maintainer: Alexey Seleznev <[email protected]>
Description: Load data from vk.com api about your communiti users and views,
ads performance, post on user wall and etc. For more information
see API Documentation <https://vk.com/dev/first_guide>.
License: GPL-2
Depends: R (>= 3.5.0)
Imports: dplyr (>= 1.0.0),
tidyr (>= 1.0.0),
jsonlite,
httr,
stringr,
lgr
Language: ru
Encoding: UTF-8
BugReports: https://github.com/selesnow/rvkstat/issues
URL: https://selesnow.github.io/rvkstat/
RoxygenNote: 7.1.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export(vkGetAdPostsReach)
export(vkGetDbCountries)
export(vkGetDbCities)
export(vkGetDbRegions)
export(vkGetDbUniversities)
export(vkGetUserFriends)
export(vkGetUserGroups)
export(vkGetUserWall)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Доработана функция `vkGetAdStatistics()` согласно [тикету #33](https://github.com/selesnow/rvkstat/issues/33).
* Добавлена функция `vkGetGroupMembers()`, которая позволяет получить список учатсников сообщества.
* Исправил ошибку `Column `activity` doesn't exist` которая периодически возникала при использовании функции `vkGetGroupStat()`.
* Добавлены функции запроса справочников университетов, школ и станций метро: `vkGetDbUniversities()`, `vkGetDbSchools()`, `vkGetDbMetroStations()`.

# 3.1.0

Expand Down
90 changes: 90 additions & 0 deletions R/vkGetDbMetroStations.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#' Returns a List of Metro Stations
#'
#' @param city_id City ID, \code{\link{vkGetDbCities}}
#' @param username Your vkontakte login.
#' @param api_version Vkontakte API version.
#' @param token_path Path to dir with credentials
#' @param access_token API access tokens obtained using vkAuth or vkGetToken functions
#'
#' @return tibble with Metro Stations list
#' @export
vkGetDbMetroStations <- function(
city_id,
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) ) {

if ( Sys.getenv("RVK_API_TOKEN") != "" ) {
access_token <- Sys.getenv("RVK_API_TOKEN")
} else {
access_token <- vkAuth(username = username,
token_path = token_path)$access_token
}
}

if ( class(access_token) == "vk_auth" ) {

access_token <- access_token$access_token

}

# paging
offset <- 0
count <- 500
last_iteration <- FALSE
result <- list()

while ( last_iteration == FALSE ) {

# query
answer <- GET("https://api.vk.com/method/database.getMetroStations",
query = list(
city_id = city_id,
offset = offset,
count = count,
extended = 1,
access_token = access_token,
v = api_version)
)

# check status
stop_for_status(answer)
# get query body
dataRaw <- content(answer, "parsed", "application/json")

# check for error
if(!is.null(dataRaw$error)){
stop(paste0("Error ", dataRaw$error$error_code," - ", dataRaw$error$error_msg))
}

# add ti result
result <- append(result, dataRaw$response$items)

# check iteraction
if ( length( dataRaw$response$items ) < count ) {

last_iteration <- TRUE

}

# offet
offset <- offset + count

# sleep
Sys.sleep(0.5)

}

# collect result
result <- bind_rows(result)

# end
return(result)

}
100 changes: 100 additions & 0 deletions R/vkGetDbSchools.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#' Returns a List of Schools
#'
#' @param q Search query string.
#' @param city_id City ID, \code{\link{vkGetDbCities}}
#' @param username Your vkontakte login.
#' @param api_version Vkontakte API version.
#' @param token_path Path to dir with credentials
#' @param access_token API access tokens obtained using vkAuth or vkGetToken functions
#'
#' @return tibble with Schools list
#' @export
vkGetDbSchools <- function(
q = NULL,
city_id = NULL,
username = getOption("rvkstat.username"),
api_version = getOption("rvkstat.api_version"),
token_path = vkTokenPath(),
access_token = getOption("rvkstat.access_token")
)
{

# check
if ( is.null(q) & is.null(city_id) ) {

stop("Set at least one of the following args: q, city_id")

}

# auth
if ( is.null(access_token) ) {

if ( Sys.getenv("RVK_API_TOKEN") != "" ) {
access_token <- Sys.getenv("RVK_API_TOKEN")
} else {
access_token <- vkAuth(username = username,
token_path = token_path)$access_token
}
}

if ( class(access_token) == "vk_auth" ) {

access_token <- access_token$access_token

}


# paging
offset <- 0
count <- 10000
last_iteration <- FALSE
result <- list()

while ( last_iteration == FALSE ) {

# query
answer <- GET("https://api.vk.com/method/database.getSchools",
query = list(
city_id = city_id,
q = q,
offset = offset,
count = count,
access_token = access_token,
v = api_version)
)

# check status
stop_for_status(answer)
# get query body
dataRaw <- content(answer, "parsed", "application/json")

# check for error
if(!is.null(dataRaw$error)){
stop(paste0("Error ", dataRaw$error$error_code," - ", dataRaw$error$error_msg))
}

# add ti result
result <- append(result, dataRaw$response$items)

# check iteraction
if ( length( dataRaw$response$items ) < count ) {

last_iteration <- TRUE

}

# offet
offset <- offset + count

# sleep
Sys.sleep(0.5)

}

# collect result
result <- bind_rows(result)

# end
return(result)

}
107 changes: 107 additions & 0 deletions R/vkGetDbUniversities.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#' Returns a List of Universities
#'
#' @param q Search query string.
#' @param country_id Country ID, \code{\link{vkGetDbCountries}}
#' @param city_id City ID, \code{\link{vkGetDbCities}}
#' @param username Your vkontakte login.
#' @param api_version Vkontakte API version.
#' @param token_path Path to dir with credentials
#' @param access_token API access tokens obtained using vkAuth or vkGetToken functions
#'
#' @return tibble with Universities list
#' @export
vkGetDbUniversities <- function(
q = NULL,
country_id = NULL,
city_id = NULL,
username = getOption("rvkstat.username"),
api_version = getOption("rvkstat.api_version"),
token_path = vkTokenPath(),
access_token = getOption("rvkstat.access_token")
)
{

# check
if ( is.null(q) & is.null(country_id) & is.null(city_id) ) {

stop("Set at least one of the following args: q, country_id, city_id")

}

# auth
if ( is.null(access_token) ) {

if ( Sys.getenv("RVK_API_TOKEN") != "" ) {
access_token <- Sys.getenv("RVK_API_TOKEN")
} else {
access_token <- vkAuth(username = username,
token_path = token_path)$access_token
}
}

if ( class(access_token) == "vk_auth" ) {

access_token <- access_token$access_token

}

# check query length
if(nchar(q) > 50 && !(is.null(q))){
stop(paste0("In q argument maximum length of string is 15 characters. You enter string with ", nchar(q)," characters!"))
}

# paging
offset <- 0
count <- 10000
last_iteration <- FALSE
result <- list()

while ( last_iteration == FALSE ) {

# query
answer <- GET("https://api.vk.com/method/database.getUniversities",
query = list(
country_id = country_id,
city_id = city_id,
q = q,
offset = offset,
count = count,
access_token = access_token,
v = api_version)
)

# check status
stop_for_status(answer)
# get query body
dataRaw <- content(answer, "parsed", "application/json")

# check for error
if(!is.null(dataRaw$error)){
stop(paste0("Error ", dataRaw$error$error_code," - ", dataRaw$error$error_msg))
}

# add ti result
result <- append(result, dataRaw$response$items)

# check iteraction
if ( length( dataRaw$response$items ) < count ) {

last_iteration <- TRUE

}

# offet
offset <- offset + count

# sleep
Sys.sleep(0.5)

}

# collect result
result <- bind_rows(result)

# end
return(result)

}
31 changes: 31 additions & 0 deletions man/vkGetDbMetroStations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5127943

Please sign in to comment.