Created
April 17, 2012 17:30
-
-
Save houshuang/2407649 to your computer and use it in GitHub Desktop.
Quick API to check if a URL has downloadable PDF - live at http://reganmian.net/check-oa/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/ruby | |
require 'net/http' | |
require "cgi" | |
# this runs on my server, and is used to check if a certain url links to a publicly available PDF | |
def checkOA(url) | |
url = url.gsub("http:/",'').gsub("http://",'') | |
uri, *path = url.split("/") | |
path = "/" + path.join("/") | |
response = nil | |
Net::HTTP.start(uri, 80) { |http| response = http.head(path) } | |
possible_ctypes = [ | |
"application/pdf", | |
"application/x-pdf", | |
"application/vnd.pdf", | |
"application/text.pdf"] | |
return possible_ctypes.index( response['content-type'] ) | |
end | |
arg = CGI.new['redir'].to_s | |
puts "Content-Type: text/html; charset=utf-8\n\n" | |
puts checkOA(arg) ? 'true' : 'false' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment