library(scholar)
library(tidyverse)
library(glue)
library(jsonlite)
library(stringdist)
library(stringr)
library(tidyjson)
library(rAltmetric)
library(fuzzyjoin)
## Download google scholar citations
id="zgVlijsAAAAJ"
pubs=get_publications(id)
pubs$title=as.character(pubs$title)


#impact <- get_impactfactor(journals=pubs$journal, max.distance = 0.1)
#network=get_coauthors(id, n_coauthors = 5, n_deep = 1)
#plot_coauthors(network, size_labels = 5)

## Download impactstory information
download.file("https://impactstory.org/api/person/0000-0003-3362-7806.json",
              destfile = "is.json")
is=tidyjson::read_json("is.json",format='json')$..JSON[[1]]


gs=gather_object(is$products) %>% 
  filter(name%in%c("title","altmetric_id","altmetric_score","doi","num_mentions","num_posts","url","year" )) %>% 
  mutate(value=as.character(..JSON)) %>% 
  as_tibble() %>% 
  select(document.id, name,value) %>% 
  spread(name,value)

media=is$products %>% enter_object(posts) %>% gather_array() %>% spread_all() %>% 
  filter(source=="news") %>% 
  mutate(link=paste0("<a href=",url," target='_blank'><font color='lightblue'>",attribution,"</font></a>"))

media_summarized= media %>% 
  group_by(document.id) %>% 
  summarize(media=paste(link, collapse=", "))

gs2=left_join(gs,media_summarized,by="document.id")


pubs2 <- pubs %>%
  fuzzyjoin::stringdist_left_join(gs2, by = "title", max_dist = 1) %>% 
  rename(title=title.x,year=year.x) %>% 
  mutate(
#    author=str_replace_all(author, " (\\S) ", "\\1 "),
#    author=str_replace_all(author, "([A-Z]) ([A-Z]) ", "\\1\\2 "),
#    author=str_replace_all(author, ", \\.\\.\\.", " et al."),
    author=str_replace_all(author, "AM Wilson", "<b>AM Wilson</b>"), # make my name fat 
    author=str_replace_all(author, "A Wilson", "<b>AM Wilson</b>"), # make my name fat 
#    glue_citation=glue('{author} ({year}) <a href="https://scholar.google.com/scholar?oi=bibs&cluster={cid}&btnI=1&hl=en">{title}</a>, <i>{journal}</i>, {number}.'),
    glue_citation=glue('<a href="https://scholar.google.com/scholar?oi=bibs&cluster={cid}&btnI=1&hl=en"><font color="black">{title}</font></a>, <i>{journal}</i>, {number} ({year}).'),
  glue_google=ifelse(cites==0,"",paste('<a title="Citations from Google Scholar" href="https://scholar.google.com/scholar?oi=bibs&hl=en&cites=',cid,'" target="_blank"><img alt="Google Scholar" src="images/google.png" width="15px"><font color="#4285f4">',cites,' citations; </font></a>')),
  glue_altmetric=ifelse(altmetric_score<0.1|is.na(altmetric_score),"",paste('<a title="Altmetric " href="https://www.altmetric.com/details/{altmetric_id}" target="_blank"><img alt="Altmetric" src="images/altm.png" width="15px"><font color="darkred"> Altmetric Score ',round(as.numeric(altmetric_score),1),'</font></a>')),
  glue_media=paste(ifelse(!is.na(media),paste('Media Links:',media),"")))

# escape some special chars, german umlauts, ...
char2html <- function(x){
  dictionary <- data.frame(
    symbol = c("ä","ö","ü","Ä", "Ö", "Ü", "ß"),
    html = c("&auml;","&ouml;", "&uuml;","&Auml;",
             "&Ouml;", "&Uuml;","&szlig;"))
  for(i in 1:dim(dictionary)[1]){
    x <- gsub(dictionary$symbol[i],dictionary$html[i],x)
  }
  x
}


# convert to htlm table - the ugly way ;)
html_2 <- pubs2 %>%
  as_tibble %>% arrange(desc(year)) %>%
 split(.$year) %>%
    map(function(x){
      x <- x %>% glue_data('<tr><td width="100%">{glue_citation} {glue_google} {glue_altmetric} {glue_media}</td></tr>')         %>%
        str_replace_all("(, )+</p>", "</p>") %>%
       char2html()
      x <- c('<table class="publication-table" border="0px solid blue" cellspacing="0" cellpadding="6" rules="", frame=""><tbody>', x, '</tbody></table>')
      return(x);
    }) %>% rev

html_3 <- map2(names(html_2) %>% paste0("<h3>", ., "</h3>"), html_2, c) %>% unlist

# html_4 <- c(
#   paste0('<p style="text-align: right; margin-top: -40px;"><small>Last updated <i>',
#          format(Sys.Date(), format="%B %d, %Y"),
#          '&ndash; Pulled automatically from my <a href="https://scholar.google.com/citations?hl=en&user=zgVlijsAAAAJ">Google Scholar profile</a>. See <a href="https://thackl.github.io/automatically-update-publications-with-R-scholar">this post</a> for how it works.</small></p>'), html_3)


#writeLines(html_4, "publications.html")
writeLines(html_3)
# write the html list to a file


AdamWilsonLab/wilson_publications documentation built on Jan. 12, 2024, 3:53 a.m.