-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Добавлены функции запроса справочников университетов, школ и станций …
…метро: `vkGetDbUniversities()`, `vkGetDbSchools()`, `vkGetDbMetroStations()`.
- Loading branch information
Showing
9 changed files
with
424 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.