1

I have a list of DOIs that I would like to convert into BibTeX records. The bib2doi package doesn't seem to be working so I wrote the following code using R's curl package to scan through the list, create the bibtex record and append it to a file. It works fine for many DOIs but it returns this error (Failed to connect to data.chinadoi.cn port 80: Connection refused) for the DOI 10.11975/j.issn.1002-6819.2017.z1.035. What I can't figure out is how to write out the bad DOI and keep going. Here's the code with three DOIs, the second DOI is the one that fails.

library(curl)
DOIlist <- c("10.1111/1748-5967.12330", "10.11975/j.issn.1002-6819.2017.z1.035", "10.1016/j.envsci.2019.03.017")

h <- new_handle()
handle_setheaders(h, "accept" = "application/x-bibtex")

for (i in 1:length(DOIlist)) {
  url <- paste0("https://doi.org/", DOIlist[i])
  print(paste0("url: ", url))
  curl_download(url, destfile = "curltest.bib", handle = h, mode = "a")
}
JerryN
  • 2,356
  • 1
  • 15
  • 49

2 Answers2

5

If you want the for loop to keep going after an error is thrown because of a bad DOI, you can wrap the curl_download() call in try(). It'll still throw the error, but your loop will keep going:

library(curl)
DOIlist <- c("10.1111/1748-5967.12330", "10.11975/j.issn.1002-6819.2017.z1.035", "10.1016/j.envsci.2019.03.017")

h <- new_handle()
handle_setheaders(h, "accept" = "application/x-bibtex")

for (i in 1:length(DOIlist)) {
  url <- paste0("https://doi.org/", DOIlist[i])
  print(paste0("url: ", url))
  try(curl_download(url, destfile = "curltest.bib", handle = h, mode = "a"))
}
Brigadeiro
  • 2,649
  • 13
  • 30
2

I tried running the code and the resulting .bib file has only one entry. It does not seem to append them. Below is a screenshot. Has anyone else got this issue? Also, the third DOI in the string results in a Connection timed out after 10014 milliseconds error.

enter image description here

I found an answer on another post. Here is the code I used to get it to work

pacman::p_load(curl,readr, tidyverse) # load required packages

urls <- c("https://doi.org/10.1016/j.tvjl.2017.12.021", "https://doi.org/10.1016/j.yqres.2013.10.005") 
walk(urls, ~ {
  curl(., handle = h) %>%
    readLines(warn = FALSE) %>%
    write(file = "Desktop\\test.bib", append = TRUE) 
})

read_delim("Desktop\\test.bib", delim = "\n") # this will add break lines to your bib file you created
Patrick
  • 915
  • 2
  • 9
  • 26