-
Notifications
You must be signed in to change notification settings - Fork 1
/
R_postFieldsFromSB_Simpl.R
66 lines (55 loc) · 2.25 KB
/
R_postFieldsFromSB_Simpl.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# R Connector to Social Bakers API
# Function to fetch some post's informations like reactions, likes, comments, etc.
## Simplified version, but may not work in Spotfire and Statistics Service. Good enough for RStudio.
# You must pass date1, date2, network and profile ID as parameters.
# Made by Rodrigo Eiras - rsveiras@gmail.com
# 05/09/2017 - Rio de Janeiro / RJ
library(httr)
library(RCurl)
library(jsonlite)
date1 <- as.Date(date1, "%Y-%m-%d")
date2 <- as.Date(date2, "%Y-%m-%d")
daysCount <- as.numeric(difftime(date2,date1,units = "days")) + 1
network <- "facebook"
login <- "login"
secret <- "passwd"
baseURL <- paste0("https://api.socialbakers.com/0/",network,"/page/posts")
doc <- POST(baseURL,
authenticate(login,
secret,
type = "basic"),
body = list(
date_start = date1,
date_end = date2,
profile = profile,
fields = c("id",
"created",
"message",
"comments_count",
"shares_count",
"reactions_count",
"interactions_count",
"url",
"author_id",
"page_id",
"type",
"reactions",
"story",
"insights_impressions",
"insights_impressions_paid",
"insights_engaged_users",
"insights_video_avg_time_watched",
"insights_fan_reach",
"insights_impressions_organic",
"insights_reactions_by_type_total",
"insights_video_complete_views_30s",
"insights_video_complete_views_organic",
"insights_video_complete_views_paid"))
, encode = "json")
stop_for_status(doc)
jsonData <- content(doc, as = "parsed")
print(jsonData)
postsCount <- as.numeric(length(jsonData$data$posts))
parsedToJSON <- toJSON(jsonData)
parsedToR <- fromJSON(parsedToJSON,simplifyVector = TRUE)
postFieldsTable <- as.data.frame(parsedToR)