Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify response parsing #572

Merged
merged 43 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
80f98b6
Use new helper function to simplify double calls to has_name_
llrs Apr 22, 2021
d5ea248
Setup some function for objects and their tests
llrs Apr 24, 2021
b92854a
shortening for easier read
llrs Apr 25, 2021
ef03c59
Parse polls entity object
llrs Apr 29, 2021
6863bee
Allow to pass some other optional arguments
llrs Apr 29, 2021
1645492
Add undocumented parameter to get the most data of each query
llrs Apr 29, 2021
db8622d
Fix some errors
llrs Apr 29, 2021
804a53e
Subtitute users_to_tbl_ by object centered user function
llrs May 2, 2021
4a6a713
Include alt text by default
llrs May 4, 2021
6d5f866
Changing name to match the API
llrs May 4, 2021
eb15f0b
fix indentation
llrs May 4, 2021
9e73770
Check input just in case
llrs May 4, 2021
0db826f
Parsing the object
llrs May 4, 2021
d68f911
Simplifying the process
llrs May 4, 2021
c3346e5
Adapting test: code now keeps API name
llrs May 4, 2021
c6db708
Fixes warnings and all major issues with the objects
llrs May 6, 2021
4dec1ac
Look for other columns
llrs May 6, 2021
c032cc0
Fix some tests
llrs May 6, 2021
4c6de77
Specific checks on different type of tweets
llrs May 25, 2021
0fddcad
Fix code parsing objects
llrs May 25, 2021
d8b8ae8
Work in progress to rework network_data
llrs May 26, 2021
6fe4879
Merged upstream/master into tweets2tbl
llrs Jun 22, 2021
6f29cb9
Improvements on geo objects
llrs Jun 27, 2021
1451b30
Conditional usage of packages
llrs Jun 27, 2021
76b72cc
Test only data.frame
llrs Jun 27, 2021
372a402
Better handling of indices
llrs Jun 27, 2021
260ea97
Handle all different values a tweet can have
llrs Jun 27, 2021
334fba8
Move the function
llrs Jun 27, 2021
afb926a
No usage of tibbles
llrs Jun 27, 2021
744cf1e
Improved testing on different type of tweets
llrs Jun 27, 2021
0ad32c8
Still evaluating if this function is worth keeping
llrs Jun 27, 2021
c02590d
Testing network_data with new structure
llrs Jun 27, 2021
5036097
testing only for data.frames
llrs Jun 27, 2021
7d03f3d
Remove unused functions, improve documentation
llrs Jun 27, 2021
f637c23
Not load devtools
llrs Jun 27, 2021
f0ebcd4
Update documentation
llrs Jun 27, 2021
81f650a
fix internals and renaming variables
llrs Jun 27, 2021
56bc0ff
Closes #574
llrs Jun 28, 2021
2d1806f
Add test to timeline
llrs Jun 28, 2021
c92bd1b
Remove some no longer needed functions
llrs Jul 3, 2021
90c0916
Fix, follow Hadley review comments
llrs Jul 10, 2021
8476987
Move symbols function closer to hasthags and explain it
llrs Jul 12, 2021
d9dd060
Move data inside test_that calls
llrs Jul 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions R/entities_objects.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Contains functions to parse the objects described here:
# https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/entities

# <https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/entities#hashtags>
hashtags <- function(x) {
if (NROW(x) == 0) {
llrs marked this conversation as resolved.
Show resolved Hide resolved
data.frame(text = NA, indices = I(list(NA)),
stringsAsFactors = FALSE)
} else {
i <- indices_vec(x$indices)
data.frame(text = x$text, indices = I(i))
}
hadley marked this conversation as resolved.
Show resolved Hide resolved
}

# PowerTrack $ text
# has:symbol
# They are the same
# <https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/entities#symbols>
# parse_entities2 uses the name of the columns to match the appropriate function to parse it.
# It needs a symbols function that is the same as hashtags
symbols <- hashtags

indices_vec <- function(x) {
lapply(x, function(y){
matrix(y, ncol = 2, dimnames = list(NULL, c("start", "end")))})
}

# The extended entities is really for media
# <https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/extended-entities>
media <- function(x) {
df <- data.frame(id = NA, id_str = NA, indices = I(list(NA)),
media_url = NA, media_url_https = NA,
url = NA, display_url = NA, expanded_url = NA,
type = NA, sizes = I(list(NA)), ext_alt_text = NA,
stringsAsFactors = FALSE)
if (NROW(x) == 0) {
return(df)
}
indices <- as.data.frame(t(simplify2array(x$indices)))
colnames(indices) <- c("start", "end")
x$indices <- I(list(indices))
sizes <- rbind(x$sizes$large, x$sizes$small, x$sizes$thumb, x$sizes$medium)
sizes$type <- c("large", "small", "thumb", "medium")
x$sizes <- list(sizes)
x[setdiff(colnames(df), colnames(x))] <- rep(NA, nrow(x))
x
}

urls <- function(x) {
df <- data.frame(url = NA, expanded_url = NA, display_url = NA,
indices = I(list(NA)), unwound = I(list(NA)),
stringsAsFactors = FALSE)
if (NROW(x) == 0) {
return(df)
}
indices <- as.data.frame(t(simplify2array(x$indices)))
colnames(indices) <- c("start", "end")
x$indices <- I(indices)
x[setdiff(colnames(df), colnames(x))] <- rep(NA, nrow(x))
x
}

# PowerTrack @ screen_name
# has:mentions
# <https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/entities#mentions>
user_mentions <- function(x) {
df <- data.frame(screen_name = NA, name = NA, id = NA, id_str = NA,
indices = I(list(NA)), stringsAsFactors = FALSE)
if (NROW(x) == 0) {
return(df)
}
indices <- as.data.frame(t(simplify2array(x$indices)))
colnames(indices) <- c("start", "end")
x$indices <- indices
x[setdiff(colnames(df), colnames(x))] <- rep(NA, nrow(x))
rownames(x) <- NULL
x
}

# <https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/entities#polls>
# Not testable without fullarchive access
polls <- function(x) {
df <- data.frame(options= I(list(NA)), end_datetime = NA,
duration_minutes = NA, stringsAsFactors = FALSE)
if (NROW(x) == 0) {
return(df)
}
x[setdiff(colnames(df), colnames(x))] <- rep(NA, nrow(x))
x
}


parse_entities <- function(x) {

if (is.null(x)) {
return(list(description = urls(NULL), url = urls(NULL)))
}

if (is.null(x$description$urls)) {
description <- list(description = urls(x$description$urls))
} else {
description <- lapply(x$description$urls, urls)

}

if (is.null(x$url$urls)) {
url <- list(url = urls(x$url$urls))
} else {
url <- lapply(x$url$urls, urls)
}
l <- Map(list, description, url)
lapply(l, `names<-`, value = c("description", "url"))
}

5 changes: 3 additions & 2 deletions R/favorites.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ get_favorites <- function(user,
}

get_favorites_user <- function(user, ..., parse = TRUE, token = NULL) {
stopifnot(length(user) == 1)
params <- list(
tweet_mode = "extended",
include_ext_alt_text = "true"
# Undocumented parameter https://github.com/ropensci/rtweet/issues/575#issuecomment-829605892
llrs marked this conversation as resolved.
Show resolved Hide resolved
tweet_mode = "extended"
)
params[[user_type(user)]] <- user

Expand Down
56 changes: 56 additions & 0 deletions R/geo_objects.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Contains functions to parse the objects described here:
# https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/geo

bounding_box <- function(x) {
empty <- data.frame(long = NA, lat = NA, type = NA)
if (is.null(x) || (identical(x, NA))) {
return(empty)
}
if (is.data.frame(x)) {
coord <- x$coordinates[[1]][1, , ]
if (is.null(coord)) {
return(empty)
}
return(data.frame(long = coord[, 1], lat = coord[, 2], type = x$type))
}
m <- x$coordinates[1, , ]
colnames(m) <- c("long", "lat")
df <- as.data.frame(m)
df$type <- x$type
df
}

# <https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/geo#coordinates>
coordinates <- function(x) {
if (is.null(x) || length(x) == 1 && is.na(x)) {
return(data.frame(long = NA, lat = NA, type = NA))
}
if (has_name_children(x, "coordinates", "coordinates")) {
return(data.frame(long = x$coordinates[[1]], lat = x$coordinates[[2]], type = x$type))
}
data.frame(long = x$coordinates[[1]], lat = x$coordinates[[2]], type = x$type)
}

# <https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/geo#place>
place <- function(x) {
if (is.null(x) || length(x) == 1 && is.na(x)) {
df <- data.frame(geo = I(list(coordinates(NA))), coordinates = I(list(coordinates(NA))),
place = I(list(NA)))
return(df)
}

if (is.data.frame(x)) {
l <- simplify2array(x[!colnames(x) %in% c("geo", "coordinates", "bounding_box")])
} else if (is.list(x)) {
l <- simplify2array(x[!names(x) %in% c("geo", "coordinates", "bounding_box")])
if (nrow(l) != 1) {
l <- t(l)
}
}
place <- as.data.frame(l)
place$bounding_box <- list(bounding_box(x$bounding_box))

data.frame(geo = I(coordinates(x$geo)),
coordinates = I(coordinates(x$coordinates)),
place = I(place))
}
Loading