-
-
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"
This file contains 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/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