Skip to content

Instantly share code, notes, and snippets.

@spirinvladimir
Last active August 29, 2015 14:16
Show Gist options
  • Save spirinvladimir/106c288fe4c65e76b070 to your computer and use it in GitHub Desktop.
Save spirinvladimir/106c288fe4c65e76b070 to your computer and use it in GitHub Desktop.
requirebin sketch
/*jslint node:true*/
var domReady = require('domready'),
domify = require('domify'),
Instafeed = require('instafeed.js').Instafeed,
instafeedPlugin = function (opts) {
'use strict';
var haveSome = false,
loading = opts.loading,
filter = (function (filter) {
if (typeof filter === 'function') {
return filter;
} else if (filter === 'filter by timestamp range') {
return function (image) {
// remove images by time
if (image.created_time < 1421884800 || image.created_time > 1421971200 || image.user.username === 'xavrburra') {
return false;
}
// otherwise keep them
return true;
};
}
}(opts.filter)),
unSuccessfulLookups = 0,
unSuccessfulLookupsMax = 10,
next = function () {
if (this.hasNext()) {
this.next();
} else if (loading) {
loading.style.visibility = 'hidden';
}
},
loadMoreHide = function () {
if (opts.loadmore) {
opts.loadmore.style.visibility = 'hidden';
}
},
feed = new Instafeed({
get: 'tagged',
tagName: opts.tagName,//'proudtobeabulldog',
resolution: 'low_resolution',
limit: '60',
sortBy: opts.sortBy,//'most-liked',
filter: filter,
clientId: '6c5069dd78b84e0691ec42c6a4cd5023',
accessToken: '654925344.6c5069d.c5157f3cd8c949efacb22bb36c8ae540',
template: '<a href="{{link}}"><img src="http:{{image}}" /></a>',
success: function (respond) {
if (opts.filter === 'filter by tag snap likes') {
respond.data = respond.data.filter(function (o) {
return o.user_has_liked;
});
}
if (respond.data.length > 0) {
haveSome = true;
unSuccessfulLookups = 0;
} else {
unSuccessfulLookups += 1;
}
setTimeout(function () {
if (haveSome) {
if (unSuccessfulLookups < unSuccessfulLookupsMax) {
next.call(feed);
} else {
loadMoreHide();
}
} else {
next.call(feed);
}
}, 0);
}
});
if (loading) {
loading.style.visibility = '';
}
feed.run();
return feed;
};
domReady(function () {
'use strict';
var add = function (content, target) {
var el = domify(content);
(target || document.body).appendChild(el);
return el;
},
feed;
add('<div id="instafeed"></div>');
feed = instafeedPlugin({
tagName: 'proudtobeabulldog',
sortBy: 'most-liked',
filter: 'filter by tag snap likes',
//controls elements:
loading: add('<h1>Loading...</h1>'),
loadmore: add('<button>Load More</button>')
});
});
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({domready:[function(require,module,exports){!function(name,definition){if(typeof module!="undefined")module.exports=definition();else if(typeof define=="function"&&typeof define.amd=="object")define(definition);else this[name]=definition()}("domready",function(){var fns=[],listener,doc=document,hack=doc.documentElement.doScroll,domContentLoaded="DOMContentLoaded",loaded=(hack?/^loaded|^c/:/^loaded|^i|^c/).test(doc.readyState);if(!loaded)doc.addEventListener(domContentLoaded,listener=function(){doc.removeEventListener(domContentLoaded,listener);loaded=1;while(listener=fns.shift())listener()});return function(fn){loaded?fn():fns.push(fn)}})},{}]},{},[]);require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({domify:[function(require,module,exports){module.exports=parse;var div=document.createElement("div");div.innerHTML=' <link/><table></table><a href="/a">a</a><input type="checkbox"/>';var innerHTMLBug=!div.getElementsByTagName("link").length;div=undefined;var map={legend:[1,"<fieldset>","</fieldset>"],tr:[2,"<table><tbody>","</tbody></table>"],col:[2,"<table><tbody></tbody><colgroup>","</colgroup></table>"],_default:innerHTMLBug?[1,"X<div>","</div>"]:[0,"",""]};map.td=map.th=[3,"<table><tbody><tr>","</tr></tbody></table>"];map.option=map.optgroup=[1,'<select multiple="multiple">',"</select>"];map.thead=map.tbody=map.colgroup=map.caption=map.tfoot=[1,"<table>","</table>"];map.text=map.circle=map.ellipse=map.line=map.path=map.polygon=map.polyline=map.rect=[1,'<svg xmlns="http://www.w3.org/2000/svg" version="1.1">',"</svg>"];function parse(html,doc){if("string"!=typeof html)throw new TypeError("String expected");if(!doc)doc=document;var m=/<([\w:]+)/.exec(html);if(!m)return doc.createTextNode(html);html=html.replace(/^\s+|\s+$/g,"");var tag=m[1];if(tag=="body"){var el=doc.createElement("html");el.innerHTML=html;return el.removeChild(el.lastChild)}var wrap=map[tag]||map._default;var depth=wrap[0];var prefix=wrap[1];var suffix=wrap[2];var el=doc.createElement("div");el.innerHTML=prefix+html+suffix;while(depth--)el=el.lastChild;if(el.firstChild==el.lastChild){return el.removeChild(el.firstChild)}var fragment=doc.createDocumentFragment();while(el.firstChild){fragment.appendChild(el.removeChild(el.firstChild))}return fragment}},{}]},{},[]);require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({"instafeed.js":[function(require,module,exports){(function(){var Instafeed,root;Instafeed=function(){function Instafeed(params,context){var option,value;this.options={target:"instafeed",get:"popular",resolution:"thumbnail",sortBy:"none",links:true,mock:false,useHttp:false};if(typeof params==="object"){for(option in params){value=params[option];this.options[option]=value}}this.context=context!=null?context:this;this.unique=this._genKey()}Instafeed.prototype.hasNext=function(){return typeof this.context.nextUrl==="string"&&this.context.nextUrl.length>0};Instafeed.prototype.next=function(){if(!this.hasNext()){return false}return this.run(this.context.nextUrl)};Instafeed.prototype.run=function(url){var header,instanceName,script;if(typeof this.options.clientId!=="string"){if(typeof this.options.accessToken!=="string"){throw new Error("Missing clientId or accessToken.")}}if(typeof this.options.accessToken!=="string"){if(typeof this.options.clientId!=="string"){throw new Error("Missing clientId or accessToken.")}}if(this.options.before!=null&&typeof this.options.before==="function"){this.options.before.call(this)}if(typeof document!=="undefined"&&document!==null){script=document.createElement("script");script.id="instafeed-fetcher";script.src=url||this._buildUrl();header=document.getElementsByTagName("head");header[0].appendChild(script);instanceName="instafeedCache"+this.unique;window[instanceName]=new Instafeed(this.options,this);window[instanceName].unique=this.unique}return true};Instafeed.prototype.parse=function(response){var anchor,fragment,header,htmlString,image,imageString,imageUrl,images,img,imgUrl,instanceName,node,reverse,sortSettings,tmpEl,_i,_j,_k,_len,_len1,_len2,_ref;if(typeof response!=="object"){if(this.options.error!=null&&typeof this.options.error==="function"){this.options.error.call(this,"Invalid JSON data");return false}else{throw new Error("Invalid JSON response")}}if(response.meta.code!==200){if(this.options.error!=null&&typeof this.options.error==="function"){this.options.error.call(this,response.meta.error_message);return false}else{throw new Error("Error from Instagram: "+response.meta.error_message)}}if(response.data.length===0){if(this.options.error!=null&&typeof this.options.error==="function"){this.options.error.call(this,"No images were returned from Instagram");return false}else{throw new Error("No images were returned from Instagram")}}if(this.options.success!=null&&typeof this.options.success==="function"){this.options.success.call(this,response)}this.context.nextUrl="";if(response.pagination!=null){this.context.nextUrl=response.pagination.next_url}if(this.options.sortBy!=="none"){if(this.options.sortBy==="random"){sortSettings=["","random"]}else{sortSettings=this.options.sortBy.split("-")}reverse=sortSettings[0]==="least"?true:false;switch(sortSettings[1]){case"random":response.data.sort(function(){return.5-Math.random()});break;case"recent":response.data=this._sortBy(response.data,"created_time",reverse);break;case"liked":response.data=this._sortBy(response.data,"likes.count",reverse);break;case"commented":response.data=this._sortBy(response.data,"comments.count",reverse);break;default:throw new Error("Invalid option for sortBy: '"+this.options.sortBy+"'.")}}if(typeof document!=="undefined"&&document!==null&&this.options.mock===false){images=response.data;if(this.options.limit!=null){if(images.length>this.options.limit){images=images.slice(0,this.options.limit+1||9e9)}}fragment=document.createDocumentFragment();if(this.options.filter!=null&&typeof this.options.filter==="function"){images=this._filter(images,this.options.filter)}if(this.options.template!=null&&typeof this.options.template==="string"){htmlString="";imageString="";imgUrl="";tmpEl=document.createElement("div");for(_i=0,_len=images.length;_i<_len;_i++){image=images[_i];imageUrl=image.images[this.options.resolution].url;if(!this.options.useHttp){imageUrl=imageUrl.replace("http://","//")}imageString=this._makeTemplate(this.options.template,{model:image,id:image.id,link:image.link,image:imageUrl,caption:this._getObjectProperty(image,"caption.text"),likes:image.likes.count,comments:image.comments.count,location:this._getObjectProperty(image,"location.name")});htmlString+=imageString}tmpEl.innerHTML=htmlString;_ref=[].slice.call(tmpEl.childNodes);for(_j=0,_len1=_ref.length;_j<_len1;_j++){node=_ref[_j];fragment.appendChild(node)}}else{for(_k=0,_len2=images.length;_k<_len2;_k++){image=images[_k];img=document.createElement("img");imageUrl=image.images[this.options.resolution].url;if(!this.options.useHttp){imageUrl=imageUrl.replace("http://","//")}img.src=imageUrl;if(this.options.links===true){anchor=document.createElement("a");anchor.href=image.link;anchor.appendChild(img);fragment.appendChild(anchor)}else{fragment.appendChild(img)}}}document.getElementById(this.options.target).appendChild(fragment);header=document.getElementsByTagName("head")[0];header.removeChild(document.getElementById("instafeed-fetcher"));instanceName="instafeedCache"+this.unique;window[instanceName]=void 0;try{delete window[instanceName]}catch(e){}}if(this.options.after!=null&&typeof this.options.after==="function"){this.options.after.call(this)}return true};Instafeed.prototype._buildUrl=function(){var base,endpoint,final;base="https://api.instagram.com/v1";switch(this.options.get){case"popular":endpoint="media/popular";break;case"tagged":if(typeof this.options.tagName!=="string"){throw new Error("No tag name specified. Use the 'tagName' option.")}endpoint="tags/"+this.options.tagName+"/media/recent";break;case"location":if(typeof this.options.locationId!=="number"){throw new Error("No location specified. Use the 'locationId' option.")}endpoint="locations/"+this.options.locationId+"/media/recent";break;case"user":if(typeof this.options.userId!=="number"){throw new Error("No user specified. Use the 'userId' option.")}if(typeof this.options.accessToken!=="string"){throw new Error("No access token. Use the 'accessToken' option.")}endpoint="users/"+this.options.userId+"/media/recent";break;default:throw new Error("Invalid option for get: '"+this.options.get+"'.")}final=""+base+"/"+endpoint;if(this.options.accessToken!=null){final+="?access_token="+this.options.accessToken}else{final+="?client_id="+this.options.clientId}if(this.options.limit!=null){final+="&count="+this.options.limit}final+="&callback=instafeedCache"+this.unique+".parse";return final};Instafeed.prototype._genKey=function(){var S4;S4=function(){return((1+Math.random())*65536|0).toString(16).substring(1)};return""+S4()+S4()+S4()+S4()};Instafeed.prototype._makeTemplate=function(template,data){var output,pattern,varName,varValue,_ref;pattern=/(?:\{{2})([\w\[\]\.]+)(?:\}{2})/;output=template;while(pattern.test(output)){varName=output.match(pattern)[1];varValue=(_ref=this._getObjectProperty(data,varName))!=null?_ref:"";output=output.replace(pattern,""+varValue)}return output};Instafeed.prototype._getObjectProperty=function(object,property){var piece,pieces;property=property.replace(/\[(\w+)\]/g,".$1");pieces=property.split(".");while(pieces.length){piece=pieces.shift();if(object!=null&&piece in object){object=object[piece]}else{return null}}return object};Instafeed.prototype._sortBy=function(data,property,reverse){var sorter;sorter=function(a,b){var valueA,valueB;valueA=this._getObjectProperty(a,property);valueB=this._getObjectProperty(b,property);if(reverse){if(valueA>valueB){return 1}else{return-1}}if(valueA<valueB){return 1}else{return-1}};data.sort(sorter.bind(this));return data};Instafeed.prototype._filter=function(images,filter){var filteredImages,image,_fn,_i,_len;filteredImages=[];_fn=function(image){if(filter(image)){return filteredImages.push(image)}};for(_i=0,_len=images.length;_i<_len;_i++){image=images[_i];_fn(image)}return filteredImages};return Instafeed}();root=typeof exports!=="undefined"&&exports!==null?exports:window;root.Instafeed=Instafeed}).call(this)},{}]},{},[]);var domReady=require("domready"),domify=require("domify"),Instafeed=require("instafeed.js").Instafeed,instafeedPlugin=function(opts){"use strict";var haveSome=false,loading=opts.loading,filter=function(filter){if(typeof filter==="function"){return filter}else if(filter==="filter by timestamp range"){return function(image){if(image.created_time<1421884800||image.created_time>1421971200||image.user.username==="xavrburra"){return false}return true}}}(opts.filter),unSuccessfulLookups=0,unSuccessfulLookupsMax=10,next=function(){if(this.hasNext()){this.next()}else if(loading){loading.style.visibility="hidden"}},loadMoreHide=function(){if(opts.loadmore){opts.loadmore.style.visibility="hidden"}},feed=new Instafeed({get:"tagged",tagName:opts.tagName,resolution:"low_resolution",limit:"60",sortBy:opts.sortBy,filter:filter,clientId:"6c5069dd78b84e0691ec42c6a4cd5023",accessToken:"654925344.6c5069d.c5157f3cd8c949efacb22bb36c8ae540",template:'<a href="{{link}}"><img src="http:{{image}}" /></a>',success:function(respond){if(opts.filter==="filter by tag snap likes"){respond.data=respond.data.filter(function(o){return o.user_has_liked})}if(respond.data.length>0){haveSome=true;unSuccessfulLookups=0}else{unSuccessfulLookups+=1}setTimeout(function(){if(haveSome){if(unSuccessfulLookups<unSuccessfulLookupsMax){next.call(feed)}else{loadMoreHide()}}else{next.call(feed)}},0)}});if(loading){loading.style.visibility=""}feed.run();return feed};domReady(function(){"use strict";var add=function(content,target){var el=domify(content);(target||document.body).appendChild(el);return el},feed;add('<div id="instafeed"></div>');feed=instafeedPlugin({tagName:"proudtobeabulldog",sortBy:"most-liked",filter:"filter by tag snap likes",loading:add("<h1>Loading...</h1>"),loadmore:add("<button>Load More</button>")})});
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"domready": "1.0.7",
"domify": "1.3.1",
"instafeed.js": "1.3.2"
}
}
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; }
body, html { height: 100%; width: 100%; }</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment