Skip to content

Instantly share code, notes, and snippets.

@ryanfb
Last active May 20, 2020 15:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanfb/cbb91ecbe56d2c96c1e220e94738ac04 to your computer and use it in GitHub Desktop.
Save ryanfb/cbb91ecbe56d2c96c1e220e94738ac04 to your computer and use it in GitHub Desktop.
Download this script, make it executable (chmod a+x academia-dl.rb), install the necessary gems (gem install nokogiri open_uri_redirections), then run with: ./academia-dl.rb "https://www.academia.edu/rest/of/url"
#!/usr/bin/env ruby
require 'nokogiri'
require 'uri'
require 'open-uri'
require 'open_uri_redirections'
REFERER = 'http://scholar.google.com'
PREFIX = 'https://www.academia.edu/download'
OPEN_URI_OPTIONS = {"Referer" => REFERER, :allow_redirections => :all}
ARGV.each do |academia_url|
doc = nil
begin
doc = Nokogiri::HTML(open(academia_url))
rescue OpenURI::HTTPError => e
$stderr.puts e.inspect
sleep(5)
retry
end
download_url = doc.css('a.js-swp-download-button').first['href']
download_id = download_url.split('/')[-2]
filename = "#{URI(academia_url).path.split('/').last}.pdf"
url = "#{PREFIX}/#{download_id}/#{filename}"
if File.exist?(filename)
$stderr.puts "#{filename} already exists, skipping"
else
IO.copy_stream(open(url, OPEN_URI_OPTIONS), filename)
$stderr.puts "Downloaded #{filename}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment