Last active
August 29, 2015 14:08
-
-
Save tts/8f24ba0d15e3aa13ec67 to your computer and use it in GitHub Desktop.
A selection of Impactstory metrics of the experimental Aalto University profile page of research outputs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Code by Andy Teucher | |
| # | |
| # https://github.com/ateucher/crd_rare_bird_bot/blob/master/fun.R | |
| shorten <- function(url, token) { | |
| stop_for_status(GET(url)) | |
| res <- GET("https://api-ssl.bitly.com/v3/shorten", | |
| query = list(access_token=token, longUrl=url)) | |
| stop_for_status(res) | |
| con <- content(res) | |
| short_url <- con$data$url | |
| short_url | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| stats <- function(json, m){ | |
| # type of interaction | |
| type <- switch(m, | |
| software={ | |
| c('github:forks', 'github:stars', 'delicious:bookmarks') | |
| }, | |
| video={ | |
| c('youtube:views', 'vimeo:plays', 'delicious:bookmarks') | |
| }, | |
| slides={ | |
| c('slideshare:views', 'slideshare:downloads', 'delicious:bookmarks') | |
| }, | |
| dataset={ | |
| c('figshare:views', 'delicious:bookmarks') | |
| }, | |
| article={ | |
| c('delicious:bookmarks', 'altmetric_com:impressions') | |
| }) | |
| date <- Sys.Date() | |
| timeStamp <- strftime(date,"%Y-%m-%d") | |
| l <- list() | |
| n <- 1 | |
| for ( i in 1:length(json$products) ) { | |
| val <- '' | |
| prod <- json$products[[i]] | |
| if ( prod$genre == m ) { | |
| # if there are at least some metrics | |
| if ( length(prod$awards) != 0 ) { | |
| for ( a in 1:length(prod$awards) ) { | |
| all.metrics <- length(prod$awards[[a]]$metrics) | |
| for ( j in 1:all.metrics ) { | |
| prod.metrics <- prod$awards[[a]]$metrics[[j]] | |
| t <- prod.metrics$fully_qualified_metric_name | |
| if ( t %in% type && !is.null(prod.metrics$diff_value) && prod.metrics$diff_value > 0 ) { | |
| # Shorten title if necessary | |
| title <- if ( nchar(prod$biblio$display_title) > 30 ) { | |
| paste0(substr(prod$biblio$display_title, 1, 30), '..') | |
| } else prod$biblio$display_title | |
| val <- c(paste0('[', m, ']'), | |
| title, | |
| prod.metrics$diff_value, | |
| t, | |
| prod.metrics$current_value, | |
| ifelse(!is.null(prod.metrics$percentile_value_string), prod.metrics$percentile_value_string, 'N/A'), | |
| prod$biblio$year, | |
| prod$"_tiid", | |
| timeStamp) | |
| l[[n]] <- val | |
| n <- n+1 | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return(l) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/Rscript | |
| library(twitteR) | |
| library(ROAuth) | |
| library(httr) | |
| library(jsonlite) | |
| library(dplyr) | |
| source("f_shortenurl.R") | |
| source("f_stats.R") | |
| url <- "https://impactstory.org/profile/AaltoUniversity" | |
| json <- fromJSON(url, simplifyVector=FALSE) | |
| consumer_key <- my_consumer_key | |
| consumer_secret <- my_consumer_secret | |
| access_token <- my_access_token | |
| access_secret <- my_access_secret | |
| setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret) | |
| articles <- stats(json, "article") | |
| slides <- stats(json, "slides") | |
| software <- stats(json, "software") | |
| video <- stats(json, "video") | |
| datasets <- stats(json, "dataset") | |
| all <- c(articles, slides, software, video, datasets) | |
| temp <- do.call(rbind.data.frame, all) | |
| result <- data.frame(lapply(temp, as.character), stringsAsFactors=FALSE) | |
| colnames(result) <- c("label", "title", "diff", "metrics", "total", | |
| "pctl", "year", "id", "time") | |
| # change the Twitter metrics label. Impactstory does that, too | |
| result$metrics <- gsub("altmetric_com:impressions", "twitter:impressions", result$metrics) | |
| write.table(result, | |
| file = "impactstorystats.csv", | |
| append = TRUE, | |
| sep = "\t", | |
| row.names = FALSE, | |
| col.names = FALSE) | |
| uniq.ids <- paste0("https://impactstory.org/AaltoUniversity/product/",unique(result$id)) | |
| ids.df <- as.data.frame(as.character(uniq.ids), stringsAsFactors=FALSE) | |
| colnames(ids.df) <- c("url") | |
| ids.urls.df <- ids.df %>% | |
| rowwise() %>% | |
| mutate(shorturl = shorten(url, token = token)) | |
| for ( i in 1:nrow(result) ){ | |
| tweet.text <- paste0(result$label[i], | |
| " ", | |
| result$title[i], | |
| ". ", | |
| result$diff[i], | |
| " new ", | |
| result$metrics[i], | |
| " in the last week. ", | |
| "Total ", | |
| result$total[i], | |
| " (", | |
| result$pctl[i], | |
| " pctl ", | |
| result$year[i], | |
| ") ", | |
| ids.urls.df[grep(result$id[i], ids.urls.df$url), c("shorturl")]) | |
| tweet(tweet.text) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment