Create a gist now

Instantly share code, notes, and snippets.

Super simple basic hacky NYTimes API that hooks into their JSONP feeds
<html>
<head>
<title>Super Simple Sandbox</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script src="nyt.js"></script>
</body>
</html>
var NYT = NYTimes = {
data: false,
path: "http://json8.nytimes.com/services/json/sectionfronts/",
getSection: function(section, subsection) {
var section = section || false;
var subsection = subsection || false;
if (!section) {
return {};
}
var fn = section;
if (subsection) {
fn += "_" + subsection;
}
if (section == "index" || section == "homepage") {
fn = "homepage";
}
this.load(fn.toLowerCase());
},
getHomepage: function() {
var fn = "homepage";
this.load(fn);
},
createList: function(limit) {
if (!this.data) {
return;
}
var limit = limit || this.data.items.length;
document.body.innerHTML = "";
for (var i=0; i<limit; i++) {
var item = this.data.items[i];
document.body.innerHTML += "<p>" + item.headline + "</p>"
console.log(item.headline);
}
},
createGallery: function(limit) {
if (!this.data) {
return;
}
var limit = limit || this.data.items.length;
document.body.innerHTML = "";
for (var i=0; i<limit; i++) {
var item = this.data.items[i];
if (item.promotional_media && item.promotional_media.image && item.promotional_media.image.image_crops && item.promotional_media.image.image_crops.square320) {
var img = item.promotional_media.image.image_crops.square320;
document.body.innerHTML += "<img src='" + img.url + "'/>"
console.log(item.promotional_media.image);
}
}
},
load: function(feed) {
var fn = (feed=="homepage") ? "" : feed;
window["jsonFeedCallback_" + feed] = new Function(
"return function " + feed + "(data){ NYT.callback(data)}"
)();
var script = document.createElement('script');
script.src = this.path + fn.replace("_", "/") + "/index.jsonp";
document.getElementsByTagName('head')[0].appendChild(script);
},
callback: function(data) {
console.log("callback", data);
this.data = data;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment