Skip to content

Instantly share code, notes, and snippets.

@rococodogs
Created February 12, 2016 21:05
Show Gist options
  • Select an option

  • Save rococodogs/1cb11cee287d7f75a271 to your computer and use it in GitHub Desktop.

Select an option

Save rococodogs/1cb11cee287d7f75a271 to your computer and use it in GitHub Desktop.
<?php
/**
* proxy to access HathiTrust bib api via https
*/
if (!isset($_SERVER['QUERY_STRING'])) {
echo "{}";
exit();
}
$qs = $_SERVER['QUERY_STRING'];
if (substr($qs, 0, 4) !== "url=") {
echo "{}";
exit();
}
$url = substr($qs, 4);
if (substr($url, 0, 30) !== "http://catalog.hathitrust.org/") {
echo "{}";
exit();
}
header('Content-type', 'applcation/javascript');
$response = file_get_contents($url);
print_r($response);
// add url to hathiProxy hosted on https server
var hathiProxy = ""
// appends url query string param needed for hathiProxy
hathiProxy = hathiProxy + "?url="
function compose_hathi() {
hathiContainer = document.getElementById("hathi856");
oclcworked = null;
if (!document.hathi497_form && !document.hathilccn498_form) {}
else {
if (document.hathi497_form) {
var oclc = document.getElementById("hathi").innerHTML;
var hathi_api_url = hathiProxy + "http://catalog.hathitrust.org/api/volumes/brief/json/oclc:" + oclc + "&callback=show_Hathi";
var scriptLine = "%3Cscript src='" + hathi_api_url + "' type='text/javascript'%3E%3C/script%3E";
document.write(unescape(scriptLine));
}
if (document.hathilccn498_form) {
var lccn = document.getElementById("hathilccn").innerHTML;
var hathi_api_lccn_url = hathiProxy + "http://catalog.hathitrust.org/api/volumes/brief/json/lccn:" + lccn + "&callback=show_HathiLCCN";
var scriptLineL = "%3Cscript src='" + hathi_api_lccn_url + "' type='text/javascript'%3E%3C/script%3E";
document.write(unescape(scriptLineL));
}
}
}
// Take the info returned from Hathi and do stuff with it
function show_Hathi(HathiBookInfo) {
for (items in HathiBookInfo) {
var HbookInfo = HathiBookInfo[items];
if (!HbookInfo.items[0]){}
else {
var howmanyItems = HbookInfo.items.length;
for (var i = 0; i < howmanyItems; i++) {
if (HbookInfo.items[i].rightsCode in {"pd":"" , "pdus":"" , "world":""}) {
var urlHathi = HbookInfo.items[i].itemURL;
var hathi_html = "<table border=\"0\" class=\"bibLinks\"><tr><th><span class=\"linkTextHeader\">Available online:</span></th></tr><tr align=\"center\"><td><a href=\"" + urlHathi + "\" onclick=\"window.open('" + urlHathi + "'); return false;\">Full text from the HathiTrust Digital Library</a></td></tr></table>";
hathiContainer.innerHTML = hathi_html;
hathiContainer.style.display = "block";
oclcworked = true;
}
}
}
}
}
function show_HathiLCCN(HathiBookInfo) {
if (oclcworked == true) {return}
for (items in HathiBookInfo) {
var HbookInfo = HathiBookInfo[items];
if (!HbookInfo.items[0]){}
else {
var howmanyItems = HbookInfo.items.length;
for (var i = 0; i < howmanyItems; i++) {
if (HbookInfo.items[i].rightsCode in {"pd":"" , "pdus":"" , "world":""}) {
var urlHathi = HbookInfo.items[i].itemURL;
var hathi_html = "<table border=\"0\" class=\"bibLinks\"><tr><th><span class=\"linkTextHeader\">Available online:</span></th></tr><tr align=\"center\"><td><a href=\"" + urlHathi + "\" onclick=\"window.open('" + urlHathi + "'); return false;\">Full text from the HathiTrust Digital Library</a></td></tr></table>";
hathiContainer.innerHTML = hathi_html;
hathiContainer.style.display = "block";
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment