-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscraper.R
177 lines (135 loc) · 4.57 KB
/
scraper.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# ---
# Load libraries
library(rJava)
# ---
# Settings
## User account to scrape
user_account <- "berlinphil"
## Max number of posts to retrieve
media_count <- as.integer(100)
# ---
# Initialize JVM
jvm_status <- .jinit(paste0("./libs/instagramscraper-scraper-0.0.1.jar;",
"./libs/okhttp-3.6.0.jar;",
"./libs/okio-1.11.0.jar;",
"./libs/gson-2.8.0.jar"))
## Print the classpath
# print(.jclassPath())
## Check the JVM init
if (jvm_status == 0) {
message("JVM init successful.")
} else {
stop("Error: JVM init.")
}
# ---
# Instantiate a new Instagram object with default constructor
tryCatch(
j_instagram <- .jnew("me/postaddict/instagramscraper/Instagram"),
Exception = function(e) {
e$printStackTrace()
})
if (is.jnull(j_instagram)) {
stop("Error: Can't instantiate the Instagram object.")
}
# ---
# Account information
## Call method Instagram.getAccountByUsername()
## Params:
## - String username: "berlinphil" (@berlinphil, Berliner Philharmoniker)
j_account <- j_instagram$getAccountByUsername(user_account)
if (is.jnull(j_account)) {
stop("Error: Can't retrieve account information.")
} else {
message("Account information retrieved successfully.")
}
# ---
# Account information: Create a dataframe
df_account <- data.frame(id = j_account$id,
username = j_account$username,
followsCount = j_account$followsCount,
followedByCount = j_account$followedByCount,
profilePicUrl = j_account$profilePicUrl,
biography = ifelse(is.jnull(j_account$biography),
"",
j_account$biography),
fullName = j_account$fullName,
mediaCount = j_account$mediaCount,
isPrivate = j_account$isPrivate,
externalUrl = ifelse(is.jnull(j_account$externalUrl),
"",
j_account$externalUrl),
isVerified = j_account$isVerified,
stringsAsFactors = FALSE)
## Treat encoding
Encoding(df_account$biography) <- c("UTF-8")
Encoding(df_account$fullName) <- c("UTF-8")
# ---
# Account information: Write to CSV
account_file_path <- paste0("./dataout/",
"instagram_account_",
user_account,
".csv")
write.csv(df_account,
file = account_file_path,
fileEncoding = "UTF-8",
row.names = FALSE)
# ---
# Media
## Call method Instagram.getMediasArray()
## Params:
## - String username: "berlinphil" (@berlinphil, Berliner Philharmoniker)
## - int count: 100 (to retrieve only 100 posts)
## Return:
## - Array of Media objects
tryCatch(
j_medias_array <-
.jcall(j_instagram,
"[Lme/postaddict/instagramscraper/model/Media;",
"getMediasArray",
user_account,
media_count,
evalString = FALSE),
Exception = function(e) {
e$printStackTrace()
})
if (length(j_medias_array) == 0) {
warning("Warning: No media retrieved.")
} else {
message("Media posts retrieved successfully.")
}
# ---
# Media: Create dataframe
posts_rows <-
lapply(
j_medias_array,
function(media) {
post_row <- data.frame(
id = media$id,
createdTime = as.POSIXct(media$createdTime,
origin = "1970-01-01"),
type = media$type,
link = media$link,
imageStandardResolutionUrl = media$imageStandardResolutionUrl,
caption = media$caption,
code = media$code,
commentsCount = media$commentsCount,
likesCount = media$likesCount,
videoViews = media$videoViews,
stringsAsFactors = FALSE
)
}
)
df_posts <- do.call("rbind", posts_rows)
## Treat encoding
Encoding(df_posts$caption) <- c("UTF-8")
# ---
# Media: Write to CSV
posts_file_path <- paste0("./dataout/",
"instagram_posts_",
user_account,
".csv")
write.csv(df_posts,
file = posts_file_path,
fileEncoding = "UTF-8",
row.names = FALSE)
message("End of script.")