Skip to content

Instantly share code, notes, and snippets.

@timfel
Created June 24, 2015 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timfel/159085273508c08445a3 to your computer and use it in GitHub Desktop.
Save timfel/159085273508c08445a3 to your computer and use it in GitHub Desktop.
require "mechanize"
url = "http://www.crossref.org/guestquery/"
bibfile = ARGV[0]
outfile = File.open(ARGV[1], 'w') || STDOUT
puts "Searching for #{bibfile}, writing to #{outfile}"
agent = Mechanize.new
page = agent.get url
form = page.form_with name: "form2"
author_field = form.field_with name: "auth2"
title_field = form.field_with name: "atitle2"
title = author = nil
contents = File.read(bibfile).each_line.to_a
titler = /^\s*title\s*=\s*\{(.*)\},?\s*$/
authorr = /^\s*author\s*=\s*\{(.*)\},?\s*$/
namer = /((:?De )?(:?D')?\w+(?:-\w+)?)/
author1r = /^\s*#{namer},.*$/
author2r = /.*?#{namer} and.*/
myidx = 0
contents.dup.each_with_index do |line, idx|
line = line.gsub(/\{\"([aou])\}/, "\\1")
if line =~ titler
title = $1
elsif line =~ authorr
author = $1
if author =~ author1r
author = $1
elsif author =~ author2r
author = $1
end
end
if title && author
print "#{author}: #{title}: "
author_field.value = author
title_field.value = title
newpage = form.submit
links = newpage.links_with(href: /dx.doi.org/)
links.each do |link|
doi = link.href.sub('http://dx.doi.org/', '')
print " #{doi} "
myidx += 1
contents.insert(idx + myidx, "doi = {#{doi}},\n")
end
puts
title = nil
author = nil
end
end
outfile << contents.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment