Retrieve Google+ Post Comments using R

It is high time when we must start analyzing about what people think about each comment is made on a post or a product post. This helps analyzing the sentiments associated with any product post made on any social platform. For example, what people think about newly launched phone. We can analyze the each post’s comments using sentiment analysis.

Today, I am going to write about how you can fetch comments associated with a post. However, I am not very much familiar with R however, I managed to write code for the post and comments and it was written for one of my friends.

Following code will help you fetch the details for comments from google+ :


options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))

# Use your own API key here
api_key <- 'xxx'

# Retrieving post data 
posts=harvestPage("+SamsungMobile", parseFun = parsePost, results = 1, nextToken = NULL, cr = 1)

# Creating a data frame for all comments with empty data later it will be changed with exact data
x = data.frame(activity_sr=1:sum(posts$nC),comments_sr=1,ActivityID=c(''),"Actor"=c(''),"PublishedOn"=c(''), "Comment"=c(''), stringsAsFactors = FALSE)
j =1

# Retrieving data for each post
for (p in 1:length(posts$id)){
	# Sending request for comments to google plus against each activities
	data <- getURL(paste("", levels(droplevels(posts$id[p])) , "/comments?key=", api_key, sep=""),ssl.verifypeer = FALSE)
	js <- fromJSON(data, asText=TRUE);

	# Get all comments for the activity
	for (i in 1:length(js$items)){
		x$ActivityID[j] = levels(droplevels(posts$id[p]))
		x$comments_sr[j] = i
		x$Actor[j] = js$items[[i]]$actor$displayName
		x$PublishedOn[j] = js$items[[i]]$published
		x$Comment[j] = js$items[[i]]$object[[2]]
# Display the data 

In my second post, I will analyse the output to include sentiment analysis code snipet.

Related posts

Leave a Comment