Giter VIP home page Giter VIP logo

brandwatchr's Introduction

brandwatchR

CRAN_Status_Badge

An API wrapper package for the Brandwatch API.

If you're like me and you just want to get your hands on lots of lovely mention data, you do that by:

  • Authenticating with bwr_auth()
  • Working out the project id you need with bwr_projects_get()
  • Working out the id of the query you want results for with bwr_queries_get().

From here, you can retrieve mentions data using bwr_get_mentions(), which takes the project ID and query ID as arguments.

Install

devtools::install_github("Phippsy/brandwatchR")

or install from CRAN, version 0.3.0

install.packages('brandwatchR')

Authenticate

Only username and password are mandatory. You can optionally specify refresh = TRUE to force the function to obtain a new token from the API. Otherwise it will read a cached local file if available. You can also optionally specify cache = FALSE if you prefer not to have your token cached locally. In this case, your token will be stored in a local environment variable and available for the current R session only.

bwr_auth(un = "[email protected]",
         pw = "your_password",
         refresh = TRUE,
         cache = TRUE)

Get project info

my_projects <- bwr_projects_get()
head(my_projects)

This will display a data frame of project information including project ID, description, creation date and more.

Read the Brandwatch documentation for more info

Get queries for a project

Once you have a project ID, you can find out all the available queries in your environment. Do this using the bwr_query_get() function:

# We arbitrarily pull the first available project ID
my_project <- bwr_projects_get()$id[1]

# Get list of queries for this project ID
my_query <- bwr_query_get(project_id = my_project)

This will return a data frame showing you the ID, name, created date and similar metadata for the queries in the specified project.

Get mentions for a query

Finally, the fun stuff - we can now request all mentions for a given query, using a specified date range. We achieve that using the bwr_mentions_get() function.

my_mentions <- bwr_mentions_get(
  project_id = my_project,
  query_id = my_query,
  date_range = c("2018-01-01", "2018-02-01"))

This will return a data frame containing the mentions for your specified query. Brandwatch returns a ton of fields in the API response, so it's likely that you'll want to slim down the resulting data frame by using dplyr's select() function or similar.

You can find information about the mention field definitions within Brandwatch's API documentation.

All functions currently available

Authentication

  • bwr_auth() authenticate

Projects

  • bwr_projects_get()- get a dataframe of projects

Queries / mentions

  • bwr_query_get() - get a dataframe of queries for a project
  • bwr_query_check() - check the syntax of a query
  • bwr_query_create() - upload a new query
  • bwr_query_delete() - delete a specified query
  • bwr_mentions_get() - get the mentions for a specified query
  • bwr_mentions_total() - get the total number of mentions for a specified query or query group.
  • bwr_mentions_topics() - get the matching topics & topic metadata for a specified query or query group.
  • bwr_mentions_topsites() - get the top authors for a specified query or query group.
  • bwr_mentions_toptweeters() - get the top tweeters for a specified query or query group.
Query filters
  • bwr_filters_get() - Get a data frame of all parameters which can be used to filter your query
  • bwr_metrics_get() - Get a data frame of all metrics which have limited accepted values.
Query Groups
  • bwr_querygrp_get() - get the query groups for a specified project
  • bwr_querygrp_delete() - delete a specfied query group

Tags

  • bwr_tag_get() - get the tags for a specified project
  • bwr_tag_create() - create a new tag for a specified project
  • bwr_tag_delete() - delete a tag for a specified project

Categories

  • bwr_cat_get() - get the categories for a specified project
  • bwr_cat_create() - create a new category for a specified project

Rules

  • bwr_rule_get() - get a data frame of all rules available within the specified project.
  • bwr_rule_delete() - delete a rule for a specified project and rule id

Charts

  • bwr_mentions_chart() - get a data frame of chart-ready statistics for a specified query or query group.

brandwatchr's People

Contributors

j450h1 avatar phippsy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

j450h1

brandwatchr's Issues

Error: Bad Request

I've been getting Error: Bad Request (HTTP 400) each time I try to login. Has anyone else had this issue?

Snippets not showing

I was able to pull data for a specific project and query, and all other fields appear to pull successfully, but the snippet does not appear (it shows as NA). When I use a curl statement to pull the same data via the API, the snippets are successfully pulled.

Here is the sample of the code I am running
library(brandwatchR)
library(jsonlite)
library(dplyr)
library(lubridate)

bwr_auth(un = "#############@##############", pw = rstudioapi::askForPassword())

my_project <- bwr_projects_get()$id[2]

my_query <- bwr_query_get(project_id = my_project)$id[4] #4

my_mentions_test <- bwr_mentions_get(project_id = my_project,
query_id = my_query,
filters = list(gender = "female", sentiment = 'neutral'), #, twitterVerified = TRUE),
order_by = 'sentiment', order_direction = 'asc',
page = 0,
date_range = c('2019-01-30', '2019-01-31'))

groups dataframe - Error in is.finite(x[[idx]]) : default method not implemented for type 'list'

Congrats on new package!

Small error here when I try to view the my_groups dataframe:

#install.packages('brandwatchR')
library(brandwatchR)
library(jsonlite)
library(dplyr)

brandwatch_credentials <- file.path("..", "credentials", "brandwatch.json")

bw <- jsonlite::fromJSON(brandwatch_credentials)

# Authenticate

bwr_auth(un= bw$USERNAME,
         pw= bw$PASSWORD,
         refresh = TRUE,
         cache = TRUE)

# Get projects
my_projects <- bwr_projects_get()
#head(my_projects)

PROJECT <- my_projects %>% filter(name == bw$PROJECT) %>% pull(id)

# Get groups
my_groups <- bwr_querygrp_get(project_id = PROJECT)
View(my_groups)

issue with pagging

Hi,

When I run the following code:

bwr_mentions_get( project_id = 1998216861, query_id = 1999369154, page = 0)

It automatically tries to see/check the next page i.e. 1 in this case.
This throws an error:

No encoding supplied: defaulting to UTF-8. Error in vapply(x, obj_sum, character(1L)) : values must be length 1, but FUN(X[[1]]) result is length 0

After checking with the de-bugger:

here is the error:

if (dim(results)[1] >= 5000) { results <- data.table::rbindlist(list(results, bwr_mentions_get(project_id = project_id, query_id = query_id, date_range = date_range, page = json$resultsPage + 1)), fill = TRUE) }

Could we get it fixed please?

dates not pulling

hi I am trying to pull some brandwatch mentions data with this package and everything seems to be working fine except in the data column a lot of values are coming up as NA. here is my code:

library(brandwatchR)
library(dplyr)
library(xlsx)

setwd("")

bwr_auth(un = "",
pw = "",
refresh = FALSE,
cache = FALSE)

View(bwr_cat_get(
project_id =
))

my_projects <- bwr_projects_get()
head(my_projects)

my_mentions1 <- bwr_mentions_get(
project_id = ,
query_id = ,
date_range = c("2019-04-01", "2019-08-12"))

my_mentions2 <- bwr_mentions_get(
project_id = ,
query_id = ,
date_range = c("2019-03-01", "2019-04-01"))

my_mentions3 <- bwr_mentions_get(
project_id = ,
query_id = ,
date_range = c("2019-01-20", "2019-03-01"))

my_mentions4 <- bwr_mentions_get(
project_id = ,
query_id = ,
date_range = c("2019-01-18", "2019-01-20"))

my_mentions5 <- bwr_mentions_get(
project_id = ,
query_id = ,
date_range = c("2019-01-17", "2019-01-18"))

my_mentions6 <- bwr_mentions_get(
project_id = ,
query_id = ,
date_range = c("2018-10-1", "2019-01-12"))

glimpse(my_mentions1)

library(dplyr)
my_mentions1 <- my_mentions1 %>%
as_tibble %>%
select(date, sentiment, categories)

my_mentions2 <- my_mentions2 %>%
as_tibble %>%
select(date, sentiment, categories)

my_mentions3 <- my_mentions3 %>%
as_tibble %>%
select(date, sentiment, categories)

my_mentions4 <- my_mentions4 %>%
as_tibble %>%
select(date, sentiment, categories)

my_mentions5 <- my_mentions5 %>%
as_tibble %>%
select(date, sentiment, categories)

my_mentions6 <- my_mentions6 %>%
as_tibble %>%
select(date, sentiment, categories)

my_mentions <- bind_rows(my_mentions1, my_mentions2, my_mentions3, my_mentions4, my_mentions5, my_mentions6)

write.csv(as.matrix(my_mentions), "mentions.csv")

(I have removed project/ query ID and name and password/ setwd)

The resulting CSV is a file with some dates, but most of them are filled with NA.

Any help would be really appreciated!

bwr_query_create and bwr_query_delete not working

Code being run:

brandwatchR::bwr_auth(un = "XXXX",
                      pw = "XXXX")

my_projects <- bwr_projects_get()
head(my_projects)

(my_project <- bwr_projects_get()$id)

(my_query <- bwr_query_get(project_id = my_project)$id)


bwr_query_create(project_id = my_project,
                 samplePercent = 50,
                 includedTerms = 'at_mentions:mickeymouse',
                 name = 'test1')

output:

Error in bwr_query_create(project_id = my_project, samplePercent = 50,  : 
  Bad Request (HTTP 400).

No issues until I run bwr_query_create.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.