From 93de25db15791eb6644d671dd44f65ea672e8d4f Mon Sep 17 00:00:00 2001 From: Jan Eggers Date: Wed, 14 Jul 2021 11:01:46 +0200 Subject: [PATCH 1/3] Rate limitation, ct_auth() function --- R/RCrowdTangle.R | 253 +++++++++++++++++++++++++++++++++++++++++++++-- README.md | 31 +++++- 2 files changed, 276 insertions(+), 8 deletions(-) diff --git a/R/RCrowdTangle.R b/R/RCrowdTangle.R index d5b06e6..936d18c 100644 --- a/R/RCrowdTangle.R +++ b/R/RCrowdTangle.R @@ -1,8 +1,148 @@ +# ----- crowdtangleR.R ----- +# +# Fork of cpbuschmann's RCrowdTangle library +# +# CC-By Jan Eggers + + require("jsonlite") require("dplyr") -ct_get_links <- function(x = "", platforms = "", count = 100, startDate = "", endDate = "", token = "") + +# ---- Definition section ---- + +# Versions as date strings +ctRVersion <- "2021-07-14" +apiVersion <- "2021" + +# Rate limits: +# Default rate limit is 6 calls/minute, except for /links calls, who are +# limited to 2 calls/minute. Every function waits for the appropriate time +# beforte trying again. If not needed, just set to NULL. + +set_api_limit <- function(calls = 6, links=FALSE) { + if (calls > 0) { + if(links) { + apiWaitLinks <<- 60/calls + } else { + # Default wait + apiWait <<- 60/calls + } + } +} + +set_api_limit(6) +set_api_limit(2,links=TRUE) + +# Global variable to store token from environment +ct_token <- NULL + +# ---- Manage API token ---- TODO +# Takes token and stores it in the .Renviron file for future reference +require(stringr) + +ct_auth <- function(token = NULL, overwrite = FALSE) { + # Nicked large parts of this function from Bene Witzenberger's great + # datawRappr package. + # It just reads the .Renviron file, looks if the API key already exists, + # returns a warning if it does (or overwrites, if ordered to), + # or adds it, if necessary + + # Access global environment file: + filename <- paste0(Sys.getenv("HOME"), "/.Renviron") + + # If no token is given, read key from ./Renviron and proceed to trying out + # immediately. + if (is.null(token)) { + token <- Sys.getenv("CROWDTANGLE_API_TOKEN") + if (is.null(token)) stop("No token set") + } else { + # Get on with it. Write key (or ignore if already exists and no overwrite) + hook_name <- "CROWDTANGLE_API_TOKEN" + + if (!file.exists(filename)) { # create .Renviron, if it doesn't exist + file.create(filename) + warning("No Renviron file found") + } + + # check if key already exists - if yes, check for overwrite = TRUE, else: write new key + if (Sys.getenv(token) != "") { + + if (overwrite == TRUE) { + + + # base R-solution: + txt_vector <- readLines(filename, n = -1) + lines_delete <- which(grepl(paste0("^",hook_name,".*$"), txt_vector)) + output_txt <- txt_vector[-lines_delete] + + # add new key: + new_key <- paste0(hook_name,' = ', token) + output_txt <- c(output_txt, new_key) + writeLines(output_txt, filename) + + # reload Renviron after changing it: + readRenviron(filename) + + } else if (overwrite == FALSE) { # if key exists, but overwrite is FALSE: throw warning and end function + # Query existing token + token <- Sys.getenv("CROWDTANGLE_API_TOKEN") + + warning(paste0("API key ", hook_name, " already exists on this system.\nSet `overwrite = TRUE` to delete it."), immediate. = TRUE) + + } + + + } else { + + # write new Key to to environment file + new_key <- paste0(hook_name,' = ', token) + write(new_key, file = filename, append = TRUE) + readRenviron(filename) + } + + } + # Try accessing the API with token; just a blank default query + endpoint.posts <- "https://api.crowdtangle.com/posts" + query.string <- paste0(endpoint.posts, "?token=", token) + response.json <- try(fromJSON(query.string), silent = TRUE) + # Wait a fraction of a second to stay below the API rate limit + if (apiWaitLinks>0) Sys.sleep(apiWaitLinks) + # Check if query returned OK + if (response.json$status==200) { + # Set global variable to token + ct_token <<- token + return("OK") + } else { + stop(paste0("Token not valid - returns ",response.json$status)) + } +} + +# not_run +# ct_auth("your_api_key_here",overwrite=TRUE) +# Read with Sys.getenv("CROWDTANGLE_API_TOKEN") + + +# ---- API call: links ---- +# Retrieve a set of posts matching a certain link. This will return up to +# 1000 posts. + +ct_get_links <- function(x = "", platforms = "", count = 100, + startDate = "", endDate = "", + token = NULL) { + # if no token is given, try to retrieve token from environment + if (is.null(token)) { + # No token given? Try to read it from global variable. + if (is.null(ct_token)) { + # Call auth function without parameters - writes API token to + # global variable ct_token and stops if no API token is set + ct_auth() + } + token <- ct_token + token <- Sys.getenv("CROWDTANGLE_API_TOKEN") + } + # get on with it! endpoint.links <- "https://api.crowdtangle.com/links" query.string <- paste0(endpoint.links, "?link=", x, "&platforms=", platforms, "&count=", count, "&startDate=", startDate, "&endDate=", endDate, "&token=", token) response.json <- try(fromJSON(query.string), silent = TRUE) @@ -17,9 +157,12 @@ ct_get_links <- function(x = "", platforms = "", count = 100, startDate = "", en if("expandedLinks" %in% colnames(posts)) posts <- select(posts, -expandedLinks) if("media" %in% colnames(posts)) posts <- select(posts, -media) posts <- jsonlite::flatten(posts) + # Wait a fraction of a second to stay below the API rate limit + if (apiWaitLinks>0) Sys.sleep(apiWaitLinks) return(posts) } else if (status == 429) + # Wait a minute if API rate limit is hit. Kept in for good measure. { print("API rate limit hit, sleeping...") Sys.sleep(60) @@ -27,19 +170,69 @@ ct_get_links <- function(x = "", platforms = "", count = 100, startDate = "", en } } -ct_get_posts <- function(x = "", searchTerm = "", language = "", types= "", minInteractions = 0, count = 100, startDate = "", endDate = "", token = "") +# ---- API call: posts ---- +# Retrieve a set of posts for the given parameters. + + +ct_get_posts <- function(x = "", searchTerm = "", + language = "", types= "", + minInteractions = 0, count = 100, + startDate = "", endDate = "", + token = NULL) { + # if no token is given, try to retrieve token from environment + if (is.null(token)) { + # No token given? Try to read it from global variable. + if (is.null(ct_token)) { + # Call auth function without parameters - writes API token to + # global variable ct_token and stops if no API token is set + ct_auth() + } + token <- ct_token + token <- Sys.getenv("CROWDTANGLE_API_TOKEN") + } + # get on with it! endpoint.posts <- "https://api.crowdtangle.com/posts" query.string <- paste0(endpoint.posts, "?listIds=", x, "&searchTerm=", searchTerm, "&language=", language, "&types=", types, "&minInteractions=", minInteractions, "&count=", count, "&startDate=", startDate, "&endDate=", endDate, "&token=", token) response.json <- try(fromJSON(query.string), silent = TRUE) status <- response.json$status - nextpage <- response.json$result$pagination$nextPage - posts <- response.json$result$posts %>% select(-expandedLinks, -media) %>% flatten() - return(posts) + if (status == 200) + { + nextpage <- response.json$result$pagination$nextPage + posts <- response.json$result$posts %>% select(-expandedLinks, -media) %>% flatten() + # Wait a fraction of a second to stay below the API rate limit + if (apiWait > 0) Sys.sleep(apiWait) + return(posts) + } else { + # return error + } } -ct_search_posts <- function(x = "", and = "", not = "", inAccountIds = "", inListIds = "", notInAccountIds = "", notInListIds = "", notInTitle = "", platforms = "", types= "", minInteractions = 0, minSubscriberCount = 0, verifiedOnly = "false", count = 100, startDate = "", endDate = "", token = "") + +# ---- API call: posts-search ---- +# Retrieve a set of posts for the given parameters. + + +ct_search_posts <- function(x = "", and = "", not = "", + inAccountIds = "", inListIds = "", + notInAccountIds = "", notInListIds = "", + notInTitle = "", platforms = "", types= "", + minInteractions = 0, minSubscriberCount = 0, + verifiedOnly = "false", count = 100, + startDate = "", endDate = "", token = "") { + # if no token is given, try to retrieve token from environment + if (is.null(token)) { + # No token given? Try to read it from global variable. + if (is.null(ct_token)) { + # Call auth function without parameters - writes API token to + # global variable ct_token and stops if no API token is set + ct_auth() + } + token <- ct_token + token <- Sys.getenv("CROWDTANGLE_API_TOKEN") + } + # get on with it! endpoint.posts <- "https://api.crowdtangle.com/posts" query.string <- paste0(endpoint.posts, "?searchTerm=", x, @@ -51,11 +244,57 @@ ct_search_posts <- function(x = "", and = "", not = "", inAccountIds = "", inLis "&inAccountIds=", inAccountIds, "&inAccountIds=", inAccountIds, - "&language=", language, "&types=", types, "&minInteractions=", minInteractions, "&count=", count, "&startDate=", startDate, "&endDate=", endDate, "&token=", token) + "&language=", language, "&types=", + types, "&minInteractions=", minInteractions, + "&count=", count, + "&startDate=", startDate, "&endDate=", endDate, + "&token=", token) response.json <- try(fromJSON(query.string), silent = TRUE) status <- response.json$status nextpage <- response.json$result$pagination$nextPage posts <- response.json$result$posts %>% select(-expandedLinks, -media) %>% flatten() + + if (apiWait>0) Sys.sleep(apiWait) + return(posts) } + +# ---- API call: /posts/:id ---- TODO +# Retrieves a specific post. There are two versions of this endpoint, depending +# upon what you need. Both return the same data. Please note that you must use +# a dashboard token that corresponds to the post platform - i.e. an Instagram +# token for Instagram posts, and a Facebook token for Facebook posts. +# +# Please also note that the ID format for Facebook and Instagram are different. +# For Instagram, it's [post_id]_[page_id], while for Facebook, +# it's [page_id]_[post_id]. While Page and Post IDs can be found in Facebook +# post URLs, Instagram does not expose the IDs in its URLs. You can pull +# the necessary Instagram IDs from our API. + + + +# ---- API call: /posts/search ---- TODO +# ** Note: Access to the Search is restricted to a limited set customers** +# and usage requires prior approval by CrowdTangle. +# +# Retrieve a set of posts for the given parameters and search terms. +# This endpoint, unlike the main /posts endpoint, searches the entire, +# cross-platform CrowdTangle system of posts. It can be limited by lists and +# accounts, but by default will search beyond the dashboard the token is +# associated with. + + +# ---- API call: /leaderboard ---- TODO +# Retrieves leaderboard data for a certain list or set of accounts. + + +# ---- API call: /lists ---- TODO +# Retrieve the lists, saved searches and saved post lists of the dashboard +# associated with the token sent in. + + +# ---- API call: /lists/:listid/accounts ---- TODO +# Retrieve the accounts for a given list. Accounts may only be retrieved for +# lists of type LIST, as saved searches and saved posts do not have +# associated accounts. \ No newline at end of file diff --git a/README.md b/README.md index 1bcca67..2bd5582 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,35 @@ A Wrapper To Retrieve Data From The CrowdTangle API This package provides programmatic accces to the [CrowdTangle API](https://help.crowdtangle.com/en/articles/1189612-crowdtangle-api) with R. Users need to have a [CrowdTangle](https://www.crowdtangle.com/) account in order to make API calls. +The package currently supports using the *links* and *posts* endpoints of the CrowdTangle API to retrieve data on specific URLs and posts as well as searching for posts. + +## Function calls + +- **ct_auth(token, overwrite=FALSE)** - Set Crowdtangle token as an environment variable +- **ct_get_links()** Call Links endpoint (consult [CT API documentation](https://github.com/CrowdTangle/API/wiki/Links) ) +- **ct_get_posts()** Call Posts endpoint (consult [CT API documentation](https://github.com/CrowdTangle/API/wiki/posts) ) +- **ct_search_posts** - Basically ct_get_posts() with a focus on search terms. + ## Examples -The package currently supports using the *links* and *posts* endpoints of the CrowdTangle API to retrieve data on specific URLs and posts as well as searching for posts. +(TODO) + +## Rate limit + +The default rate limit for CrowdTangle API calls is 6 per minute (with the exception +of the /links call which is limited to 2 calls per minute). The function calls +wait for a fraction of a second before returning. + +If you wish to change the rate limit to something lower, use + +- **set_api_limit(n)** +- **set_api_limit(n, links=TRUE)** + +to set the limit to n calls per minute. + +This sets a simple Sys.sleep() delay - assuming the return time of the API call itself +is negligible. + +## Todo + +- \ No newline at end of file From b245ae3764c9f9132144258cb280906f1b15cfd8 Mon Sep 17 00:00:00 2001 From: Jan Eggers Date: Wed, 14 Jul 2021 11:15:38 +0200 Subject: [PATCH 2/3] Rename API limit function --- R/RCrowdTangle.R | 8 ++++---- README.md | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/RCrowdTangle.R b/R/RCrowdTangle.R index 936d18c..2c46127 100644 --- a/R/RCrowdTangle.R +++ b/R/RCrowdTangle.R @@ -1,4 +1,4 @@ -# ----- crowdtangleR.R ----- +# ----- RCrowdTangle.R ----- # # Fork of cpbuschmann's RCrowdTangle library # @@ -20,7 +20,7 @@ apiVersion <- "2021" # limited to 2 calls/minute. Every function waits for the appropriate time # beforte trying again. If not needed, just set to NULL. -set_api_limit <- function(calls = 6, links=FALSE) { +ct_api_limit <- function(calls = 6, links=FALSE) { if (calls > 0) { if(links) { apiWaitLinks <<- 60/calls @@ -31,8 +31,8 @@ set_api_limit <- function(calls = 6, links=FALSE) { } } -set_api_limit(6) -set_api_limit(2,links=TRUE) +ct_api_limit(6) +ct_api_limit(2,links=TRUE) # Global variable to store token from environment ct_token <- NULL diff --git a/README.md b/README.md index 2bd5582..201b1f8 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,8 @@ wait for a fraction of a second before returning. If you wish to change the rate limit to something lower, use -- **set_api_limit(n)** -- **set_api_limit(n, links=TRUE)** +- **ct_set_api_limit(n)** +- **ct_set_api_limit(n, links=TRUE)** to set the limit to n calls per minute. From 524ac96c8a1414c3a1bc9d60b8525fe82dce06f3 Mon Sep 17 00:00:00 2001 From: Jan Eggers Date: Thu, 15 Jul 2021 10:29:31 +0200 Subject: [PATCH 3/3] Additional functions for querying facebook post, Instagram helper code --- R/RCrowdTangle.R | 188 ++++++++++++++++++++++++++++++++++++++++++----- README.md | 31 ++++++-- 2 files changed, 194 insertions(+), 25 deletions(-) diff --git a/R/RCrowdTangle.R b/R/RCrowdTangle.R index 2c46127..3035d07 100644 --- a/R/RCrowdTangle.R +++ b/R/RCrowdTangle.R @@ -34,6 +34,23 @@ ct_api_limit <- function(calls = 6, links=FALSE) { ct_api_limit(6) ct_api_limit(2,links=TRUE) +# Global variables for last access of API to determine wait +apiWaitTill <- NULL + +# Helper functions + +# Call before accessing the API. +ct_wait <- function(t = apiWait) { + if (!is.null(apiWaitTill)) { + # If before wait point, wait. + while(now() < apiWaitTill) { + Sys.sleep(1) + } + } + # Save new wait point + apiWaitTill <<- now()+t +} + # Global variable to store token from environment ct_token <- NULL @@ -55,7 +72,7 @@ ct_auth <- function(token = NULL, overwrite = FALSE) { # immediately. if (is.null(token)) { token <- Sys.getenv("CROWDTANGLE_API_TOKEN") - if (is.null(token)) stop("No token set") + if (token=="") stop("No token set") } else { # Get on with it. Write key (or ignore if already exists and no overwrite) hook_name <- "CROWDTANGLE_API_TOKEN" @@ -105,9 +122,8 @@ ct_auth <- function(token = NULL, overwrite = FALSE) { # Try accessing the API with token; just a blank default query endpoint.posts <- "https://api.crowdtangle.com/posts" query.string <- paste0(endpoint.posts, "?token=", token) + ct_wait(apiWait) response.json <- try(fromJSON(query.string), silent = TRUE) - # Wait a fraction of a second to stay below the API rate limit - if (apiWaitLinks>0) Sys.sleep(apiWaitLinks) # Check if query returned OK if (response.json$status==200) { # Set global variable to token @@ -144,7 +160,13 @@ ct_get_links <- function(x = "", platforms = "", count = 100, } # get on with it! endpoint.links <- "https://api.crowdtangle.com/links" - query.string <- paste0(endpoint.links, "?link=", x, "&platforms=", platforms, "&count=", count, "&startDate=", startDate, "&endDate=", endDate, "&token=", token) + query.string <- paste0(endpoint.links, + "?link=", x, + "&platforms=", platforms, + "&count=", count, + "&startDate=", startDate, "&endDate=", endDate, + "&token=", token) + ct_wait(apiWaitList) response.json <- try(fromJSON(query.string), silent = TRUE) if (!class(response.json) == "try-error") { @@ -158,15 +180,8 @@ ct_get_links <- function(x = "", platforms = "", count = 100, if("media" %in% colnames(posts)) posts <- select(posts, -media) posts <- jsonlite::flatten(posts) # Wait a fraction of a second to stay below the API rate limit - if (apiWaitLinks>0) Sys.sleep(apiWaitLinks) return(posts) } - else if (status == 429) - # Wait a minute if API rate limit is hit. Kept in for good measure. - { - print("API rate limit hit, sleeping...") - Sys.sleep(60) - } } } @@ -193,15 +208,23 @@ ct_get_posts <- function(x = "", searchTerm = "", } # get on with it! endpoint.posts <- "https://api.crowdtangle.com/posts" - query.string <- paste0(endpoint.posts, "?listIds=", x, "&searchTerm=", searchTerm, "&language=", language, "&types=", types, "&minInteractions=", minInteractions, "&count=", count, "&startDate=", startDate, "&endDate=", endDate, "&token=", token) + query.string <- paste0(endpoint.posts, + "?listIds=", x, + "&searchTerm=", searchTerm, + "&language=", language, + "&types=", types, + "&minInteractions=", minInteractions, + "&count=", count, + "&startDate=", startDate, + "&endDate=", endDate, + "&token=", token) + ct_wait(apiWait) response.json <- try(fromJSON(query.string), silent = TRUE) status <- response.json$status if (status == 200) { nextpage <- response.json$result$pagination$nextPage posts <- response.json$result$posts %>% select(-expandedLinks, -media) %>% flatten() - # Wait a fraction of a second to stay below the API rate limit - if (apiWait > 0) Sys.sleep(apiWait) return(posts) } else { # return error @@ -249,18 +272,16 @@ ct_search_posts <- function(x = "", and = "", not = "", "&count=", count, "&startDate=", startDate, "&endDate=", endDate, "&token=", token) + ct_wait(apiWait) response.json <- try(fromJSON(query.string), silent = TRUE) status <- response.json$status nextpage <- response.json$result$pagination$nextPage posts <- response.json$result$posts %>% select(-expandedLinks, -media) %>% flatten() - - if (apiWait>0) Sys.sleep(apiWait) - return(posts) } -# ---- API call: /posts/:id ---- TODO +# ---- API call: /posts/:id ---- # Retrieves a specific post. There are two versions of this endpoint, depending # upon what you need. Both return the same data. Please note that you must use # a dashboard token that corresponds to the post platform - i.e. an Instagram @@ -272,6 +293,131 @@ ct_search_posts <- function(x = "", and = "", not = "", # post URLs, Instagram does not expose the IDs in its URLs. You can pull # the necessary Instagram IDs from our API. +ct_get_post_by_id <- function(id = NULL, + redditAccount = NULL, + includeHistory = FALSE, + token = NULL) +{ + # if no token is given, try to retrieve token from environment + if (is.null(token)) { + # No token given? Try to read it from global variable. + if (is.null(ct_token)) { + # Call auth function without parameters - writes API token to + # global variable ct_token and stops if no API token is set + ct_auth() + } + token <- ct_token + } + # get on with it! + # Error check: No valid ID? + if (is.null(id)) stop("No ID given") + + endpoint.posts <- "https://api.crowdtangle.com/post" + query.string <- paste0(endpoint.posts, "/",id, + "?", + ifelse(is.null(redditAccount),"", + paste0("redditAccount=", + redditAccount,"&")), + ifelse(includeHistory, + "includeHistory=1&",""), + "token=", token) + ct_wait(apiWait) + response.json <- try(fromJSON(query.string), silent = TRUE) + status <- response.json$status + if (status == 200) + { + nextpage <- response.json$result$pagination$nextPage + posts <- response.json$result$posts %>% select(-expandedLinks, -media) %>% flatten() + return(posts) + } else { + # return error + } +} + +# Wrapper for Facebook and Instagram posts: Return data for post by link +# If you call this function in a dplyr pipeline (e.g. with mutate()), +# use rowwise(). + +ct_get_fb_post <- function (link="", + includeHistory = FALSE, + token = NULL) { + if (link=="") stop("No Link") + # Zahl vor dem Wort "post" + page_id <- str_extract(link,"[0-9]+(?=\\/posts)") + # Zahl am Ende des Links + post_id <- str_extract(link,"[0-9]+$") + return(ct_get_post_by_id( + id = paste0(page_id,"_",post_id), + includeHistory = includeHistory, + token = token)) +} + + +# ---- Instagram post query ---- +# This is more than a simple wrapper for ct_get_post_by_id: +# To query the stats for an Instagram post, you have to grab +# the post ID from the source code, and the profile page of the source, +# and the page ID from that source. + +# Source URL: https://www.instagram.com/p/CQ1iXf_tJ37/ + +# Instagram page id of post can be grabbed from +# +# Instagram post id can be grabbed from +# +# or calculated from code, using the helper fn InstaToPostID() below. + +# Just to keep track of this: if you query +# https://www.instagram.com/p/CQ1iXf_tJ37/?__a=1 +# a post is returned as a JSON with all relevant info. + +# Helper function to convert the Instagram URL to a post ID, +# which you need to query Crowdtangle for a single Insta post. +# +# Makes use of the gmp library which is for handling, like, really big figures. +# It *does* work if you try to use integers but whether it works correctly +# may depend on how double and integer are represented on your very system - +# max number is 64^10-1. +# +# Are you sure you won't want to use that obscure gmp library? + +library(gmp) +library(stringr) +b64_str_split <- unlist( + strsplit("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_","")) +# +instaToPostID <- function(x = "x") { + # Function expects either a url, or the isolated code. + # If url, extract. + if (str_detect(x,"instagram\\.com")) { + # Extract instagram.com/p/ as well as instagram.com/tv/ + x <- str_extract(x,"(?<=instagram\\.com\\/p\\/)[a-zA-Z0-9_\\-]+|(?<=instagram\\.com\\/tv\\/)[a-zA-Z0-9_\\-]+") + if (is.na(x)) stop("Could not extract URL ID code") + } + # convert input string to vector of integers + t <- unlist(strsplit(x,"")) + lt <- length(t) + r = as.bigz(0L) + for (i in 1:lt) { + r <- r+((which(b64_str_split==t[i])-1)*as.bigz(64)^(lt-i)) + } + return(as.character(r)) +} + + +ct_get_insta_post <- function (link="", + includeHistory = FALSE, + token = NULL) { + if (link=="") stop("No Link") + # Zahl vor dem Wort "post" + page_id <- str_extract(link,"[0-9]+(?=\\/posts)") + # Zahl am Ende des Links + post_id <- str_extract(link,"[0-9]+$") + return(ct_get_post_by_id( + id = paste0(page_id,"_",post_id), + includeHistory = includeHistory, + token = token)) +} # ---- API call: /posts/search ---- TODO @@ -297,4 +443,8 @@ ct_search_posts <- function(x = "", and = "", not = "", # ---- API call: /lists/:listid/accounts ---- TODO # Retrieve the accounts for a given list. Accounts may only be retrieved for # lists of type LIST, as saved searches and saved posts do not have -# associated accounts. \ No newline at end of file +# associated accounts. + +# ---- API call: /ctpost/:id ---- TODO +# Gives data on Crowdtangle post - only glimpsed from the demo json file at +# https://ct-staticfiles.s3-us-west-1.amazonaws.com/api/API-Demo-2020.postman_collection.json diff --git a/README.md b/README.md index 201b1f8..2a9fb95 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,18 @@ A Wrapper To Retrieve Data From The CrowdTangle API This package provides programmatic accces to the [CrowdTangle API](https://help.crowdtangle.com/en/articles/1189612-crowdtangle-api) with R. Users need to have a [CrowdTangle](https://www.crowdtangle.com/) account in order to make API calls. -The package currently supports using the *links* and *posts* endpoints of the CrowdTangle API to retrieve data on specific URLs and posts as well as searching for posts. +The package currently supports using the *links* and *posts* endpoints of the CrowdTangle API, as well as the *post/:id* query (Facebook only!) to retrieve data on specific URLs and posts as well as searching for posts. + +## Installing + +Download the RCrowdTangle.R file from the R folder, place it in your +working directory, and include it with ```source("RCrowdTangle.R")``` + +As it's not a proper R library *yet*, installing it with + +```devtools::install_github("untergeekDE/RCrowdTangle")``` + +will lead to errors. Yet. ## Function calls @@ -11,6 +22,8 @@ The package currently supports using the *links* and *posts* endpoints of the Cr - **ct_get_links()** Call Links endpoint (consult [CT API documentation](https://github.com/CrowdTangle/API/wiki/Links) ) - **ct_get_posts()** Call Posts endpoint (consult [CT API documentation](https://github.com/CrowdTangle/API/wiki/posts) ) - **ct_search_posts** - Basically ct_get_posts() with a focus on search terms. +- **ct_get_post_by_id(id)** - Call Post by ID endpoint (consult [CT API doc](https://github.com/CrowdTangle/API/wiki/Posts#get-postid)) +- **ct_get_fb_post(url)** - Return information on single FB post ## Examples @@ -27,11 +40,17 @@ If you wish to change the rate limit to something lower, use - **ct_set_api_limit(n)** - **ct_set_api_limit(n, links=TRUE)** -to set the limit to n calls per minute. - -This sets a simple Sys.sleep() delay - assuming the return time of the API call itself -is negligible. +to set the limit to n calls per minute. Whenever a query is done, a timer is set +via the ## Todo -- \ No newline at end of file +- Examples and use cases +- Convert to a proper R library (anybody any advice how to do this?) +- /posts/search call (invitation only!) +- /leaderboard call +- /lists call +- /lists/:listid/accounts call +- /ctpost/:id call (hidden, possibly deprecated) +- clean up the rather messy parameter structure for the calls +- Error handling