-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook_graph_api_20180503.R
92 lines (57 loc) · 2.41 KB
/
facebook_graph_api_20180503.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#### FACEBOOK GRAPH API ####
#https://graph.facebook.com/v2.8/search?access_token=EAACEdEose0cBACn6ZBlet9dqwfIBWi132MM8YuYHTwZCvAYa4v4S4t7ph1ZAWBtx2ycfZARm8z47ZAKiX0vNgepRCqt2hcZCEGcn4ZBEdYNTFOAZAvLKY5O8a8ZCDy6IiMxeOhkv5GYsHXl1ZAhwRbcn99YfM0KgCVPGmwvevTZCGTAklB1tGAUg7bXmUzUzB36ZAkjcW5aC1C5jOAZDZD&pretty=1&fields=name,single_line_address,phone,location,fan_count,overall_star_rating,price_range,link&q=alapitvany¢er=47.49801,19.03991&distance=20000&type=page&limit=300&after=MjYZD
facebook_api <- function(token, keyword, type, limit,center,distance) {
# declare the empty objects
data<-NULL
data2<-NULL
data3<-NULL
# cycle that iterates by keywords
for (i in seq_len(length(keyword))) {
## read URL
adat <-try(
fromJSON(
paste0(
"https://graph.facebook.com/v2.11/search?access_token=",token,
"&pretty=1&fields=name,single_line_address,phone,location,fan_count,overall_star_rating,price_range&q=",keyword[i],
"¢er=",center,
"&distance=",distance,
"&type=",type,
"&limit=",limit,
"&after=MjYZD"
)
),
silent=T
)
if (is(adat,"try-error")) next
adat_2<-adat$data
# list to data.frame
for (l in 1:length(adat_2)) {
one_line<-data.frame(adat_2[[l]])
data<-rbindlist(list(data,one_line), fill=T)
}
# additional information
data$run_time<-Sys.time()
data$type<-type
#data$category<-keyword[i]
#define paging URL
url <- adat$paging$`next`
while (!is.null(url)){
adat2 <- fromJSON(url)
adat2_2<-adat2$data
#
for (j in 1:length(adat2_2)) {
one_line2<-data.frame(adat2_2[[j]])
data2<-rbindlist(list(data2,one_line2), fill=T)
}
data2$run_time<-Sys.time()
data2$type<-type
#data2$category<-keyword[i]
url <- adat2$paging$`next`
#binding data tables
data <- rbindlist(list(data, data2), fill = T)
}
data3<-rbindlist(list(data3,data),fill = T)
data3$category<-keyword[i]
}
return(data3)
}