Подгрузим пакеты
# install.packages(с('rvest', 'dplyr', 'ggplot2', 'tibble', 'stringr'))
library(rvest)
library(dplyr)
library(ggplot2)
library(tibble)
library(stringr)
Напишем функцию, которая будет забирать информацию в зависимости от передаваемого метода и параметров.
get_method <- function(method, token, params, version = "5.52"){
url <- paste('https://api.vk.com/method/', method, sep = '')
params <- append(params, list(access_token = token, v = version))
request <- GET(url, query = params)
info <- content(request)
return(info)
}
Задаем параметры и получаем информацию по первым 100 постам.
params <- list(domain = "hsemem", count = '100', offset = as.character(0))
con <- get_method(method = "wall.get", token = token, params = params)
Функция, которая преобразует собранный контент в табличку, состоящую из лайков и репостов.
to_df <- function(con){
posts <- con$response$items
reposts <- c()
likes <- c()
for(i in 1:length(posts)){
reposts <- append(reposts, posts[[i]]$reposts$count)
likes <- append(likes, posts[[i]]$likes$count)
}
df <- tibble('reposts' = reposts, 'likes' = likes)
return(df)
}
Собираем информацию по всем постам.
# получаем кол-во постов
n <- con$response$count
# собираем информацию по всем постам
for(i in seq(100, n, 100)){
Sys.sleep(0.4)
params <- list(domain = "hsemem", count = '100', offset = as.character(i))
con <- get_method(method = "wall.get", token = token, params = params)
cur_df <- to_df(con)
df <- df %>% union(cur_df)
}
Рисуем графики.