From f55d057d881de14c4a355aa4f089c88a8ff2123e Mon Sep 17 00:00:00 2001 From: jsanahuja Date: Mon, 22 Feb 2021 19:13:10 +0100 Subject: [PATCH] cache_time feature --- dist/InstagramFeed.min.js | 2 +- index.html | 10 +++++- package.json | 2 +- src/InstagramFeed.js | 65 ++++++++++++++++++++++++++++++--------- 4 files changed, 62 insertions(+), 17 deletions(-) diff --git a/dist/InstagramFeed.min.js b/dist/InstagramFeed.min.js index b77d784..e81865a 100644 --- a/dist/InstagramFeed.min.js +++ b/dist/InstagramFeed.min.js @@ -1 +1 @@ -function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}(function(a,b){"function"==typeof define&&define.amd?define([],b):"object"===("undefined"==typeof exports?"undefined":_typeof(exports))?module.exports=b():a.InstagramFeed=b()})(this,function(){function a(a){return a.replace(/[&<>"'`=\/]/g,function(a){return d[a]})}var b={host:"https://www.instagram.com/",username:"",tag:"",container:"",display_profile:!0,display_biography:!0,display_gallery:!0,display_captions:!1,display_igtv:!1,callback:null,styling:!0,items:8,items_per_row:4,margin:.5,image_size:640,lazy_load:!1,on_error:console.error},c={150:0,240:1,320:2,480:3,640:4},d={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};return function(d){this.options=Object.assign({},b),this.options=Object.assign(this.options,d),this.is_tag=""==this.options.username,this.valid=!0,""==this.options.username&&""==this.options.tag&&(this.options.on_error("InstagramFeed: Error, no username or tag defined.",1),this.valid=!1),"undefined"!=typeof this.options.get_data&&console.warn("InstagramFeed: options.get_data is deprecated, options.callback is always called if defined"),null==this.options.callback&&""==this.options.container&&(this.options.on_error("InstagramFeed: Error, neither container found nor callback defined.",2),this.valid=!1),this.get=function(a){var b=this.is_tag?this.options.host+"explore/tags/"+this.options.tag+"/":this.options.host+this.options.username+"/",c=new XMLHttpRequest,d=this;c.onload=function(){if(4===c.readyState)if(200===c.status){try{var b=c.responseText.split("window._sharedData = ")[1].split("")[0]}catch(a){return void d.options.on_error("InstagramFeed: It looks like the profile you are trying to fetch is age restricted. See https://github.com/jsanahuja/InstagramFeed/issues/26",3)}if(b=JSON.parse(b.substr(0,b.length-1)),b=b.entry_data.ProfilePage||b.entry_data.TagPage,"undefined"==typeof b)return void d.options.on_error("InstagramFeed: It looks like YOUR network has been temporary banned because of too many requests. See https://github.com/jsanahuja/jquery.instagramFeed/issues/25",4);b=b[0].graphql.user||b[0].graphql.hashtag,a(b,d)}else d.options.on_error("InstagramFeed: Unable to fetch the given user/tag. Instagram responded with the status code: "+c.statusText,5)},c.open("GET",b,!0),c.send()},this.parse_caption=function(a,b){return"undefined"!=typeof a.node.edge_media_to_caption.edges[0]&&"undefined"!=typeof a.node.edge_media_to_caption.edges[0].node&&"undefined"!=typeof a.node.edge_media_to_caption.edges[0].node.text&&null!==a.node.edge_media_to_caption.edges[0].node.text?a.node.edge_media_to_caption.edges[0].node.text:"undefined"!=typeof a.node.title&&null!==a.node.title&&0!=a.node.title.length?a.node.title:"undefined"!=typeof a.node.accessibility_caption&&null!==a.node.accessibility_caption&&0!=a.node.accessibility_caption.length?a.node.accessibility_caption:(this.is_tag?b.name:b.username)+" image "},this.display=function(b){var d,e="";if(this.options.styling){var f=(100-2*this.options.margin*this.options.items_per_row)/this.options.items_per_row;d={profile_container:" style='text-align:center;'",profile_image:" style='border-radius:10em;width:15%;max-width:125px;min-width:50px;'",profile_name:" style='font-size:1.2em;'",profile_biography:" style='font-size:1em;'",gallery_image:" style='width:100%;'",gallery_image_link:" style='width:"+f+"%; margin:"+this.options.margin+"%; position:relative; display: inline-block; height: 100%;'"},this.options.display_captions&&(e+="")}else d={profile_container:"",profile_image:"",profile_name:"",profile_biography:"",gallery_image:"",gallery_image_link:""};if(this.options.display_profile&&(e+="
",e+=""+(this.is_tag?b.name+" tag pic":b.username+" profile pic")+" profile pic",e+=this.is_tag?"

#"+this.options.tag+"

":"

@"+b.full_name+" (@"+this.options.username+")

",!this.is_tag&&this.options.display_biography&&(e+="

"+b.biography+"

"),e+="
"),this.options.display_gallery){var g="undefined"==typeof c[this.options.image_size]?c[640]:c[this.options.image_size];if("undefined"!=typeof b.is_private&&!0===b.is_private)e+="

This profile is private

";else{var h=(b.edge_owner_to_timeline_media||b.edge_hashtag_to_media).edges;p=h.length>this.options.items?this.options.items:h.length,e+=""}}if(this.options.display_igtv&&"undefined"!=typeof b.edge_felix_video_timeline){var o=b.edge_felix_video_timeline.edges,p=o.length>this.options.items?this.options.items:o.length;if(0";for(var j=0;j",e+="",e+=""}e+=""}}this.options.container.innerHTML=e},this.run=function(){this.get(function(a,b){""!=b.options.container&&b.display(a),"function"==typeof b.options.callback&&b.options.callback(a)})},this.valid&&this.run()}}); +function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}(function(a,b){"function"==typeof define&&define.amd?define([],b):"object"===("undefined"==typeof exports?"undefined":_typeof(exports))?module.exports=b():a.InstagramFeed=b()})(this,function(){function a(a){return a.replace(/[&<>"'`=\/]/g,function(a){return d[a]})}var b={host:"https://www.instagram.com/",username:"",tag:"",container:"",display_profile:!0,display_biography:!0,display_gallery:!0,display_captions:!1,display_igtv:!1,callback:null,styling:!0,items:8,items_per_row:4,margin:.5,image_size:640,lazy_load:!1,cache_time:120,on_error:console.error},c={150:0,240:1,320:2,480:3,640:4},d={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};return function(d){this.options=Object.assign({},b),this.options=Object.assign(this.options,d),this.is_tag=""==this.options.username,this.valid=!0,""==this.options.username&&""==this.options.tag&&(this.options.on_error("InstagramFeed: Error, no username or tag defined.",1),this.valid=!1),"undefined"!=typeof this.options.get_data&&console.warn("InstagramFeed: options.get_data is deprecated, options.callback is always called if defined"),null==this.options.callback&&""==this.options.container&&(this.options.on_error("InstagramFeed: Error, neither container found nor callback defined.",2),this.valid=!1),this.get=function(a){var b=this.is_tag?this.options.host+"explore/tags/"+this.options.tag+"/":this.options.host+this.options.username+"/",c=new XMLHttpRequest,d=this;c.onload=function(){if(4===c.readyState)if(200===c.status){try{var b=c.responseText.split("window._sharedData = ")[1].split("")[0]}catch(a){return void d.options.on_error("InstagramFeed: It looks like the profile you are trying to fetch is age restricted. See https://github.com/jsanahuja/InstagramFeed/issues/26",3)}b=JSON.parse(b.substr(0,b.length-1)),b=b.entry_data.ProfilePage||b.entry_data.TagPage;var e=!1;if("undefined"==typeof b){var f=localStorage.getItem(d.cache_data_key);if(null!==f&&(b=JSON.parse(f),e=!0),d.options.on_error("Instagram Feed: Your network has been temporary banned by Instagram because of too many requests. Consider increasing your 'cache_time'. See https://github.com/jsanahuja/jquery.instagramFeed/issues/25 and https://github.com/jsanahuja/jquery.instagramFeed/issues/101",4),!b)return}else b=b[0].graphql.user||b[0].graphql.hashtag;!e&&0",e+=""+(this.is_tag?b.name+" tag pic":b.username+" profile pic")+" profile pic",e+=this.is_tag?"

#"+this.options.tag+"

":"

@"+b.full_name+" (@"+this.options.username+")

",!this.is_tag&&this.options.display_biography&&(e+="

"+b.biography+"

"),e+=""),this.options.display_gallery){var g="undefined"==typeof c[this.options.image_size]?c[640]:c[this.options.image_size];if("undefined"!=typeof b.is_private&&!0===b.is_private)e+="

This profile is private

";else{var h=(b.edge_owner_to_timeline_media||b.edge_hashtag_to_media).edges;p=h.length>this.options.items?this.options.items:h.length,e+=""}}if(this.options.display_igtv&&"undefined"!=typeof b.edge_felix_video_timeline){var o=b.edge_felix_video_timeline.edges,p=o.length>this.options.items?this.options.items:o.length;if(0";for(var j=0;j",e+="",e+=""}e+=""}}this.options.container.innerHTML=e},this.run=function(){var a=null;if(this.cache_data_key="InstagramFeed_"+(this.is_tag?"t_"+this.options.tag:"u_"+this.options.username),this.cache_data_key_cached=this.cache_data_key+"_cached",0new Date().getTime()){var c=localStorage.getItem(this.cache_data_key);null!==c&&(a=JSON.parse(c))}}null===a?this.get(function(a,b){""!=b.options.container&&b.display(a),"function"==typeof b.options.callback&&b.options.callback(a)}):(""!=this.options.container&&this.display(a),"function"==typeof this.options.callback&&this.options.callback(a))},this.valid&&this.run()}}); diff --git a/index.html b/index.html index 6deb272..623b10d 100644 --- a/index.html +++ b/index.html @@ -289,6 +289,14 @@

Options

https://www.instagram.com/ URL where to fetch the data. Useful if instagram changes CORS policy + + + cache_time + + number [Int] + 120 + Instagram response cache expiry time in minutes + on_error @@ -701,4 +709,4 @@

Still not enough?

- \ No newline at end of file + diff --git a/package.json b/package.json index 62e55c0..e75cc5a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@jsanahuja/instagramfeed", "description": "Instagram Feed without access token. Not using the Instagram API", "homepage": "https://github.com/jsanahuja/InstagramFeed", - "version": "1.5.4", + "version": "2.0.0", "keywords": [ "instagram", "feed", diff --git a/src/InstagramFeed.js b/src/InstagramFeed.js index dcba919..8885484 100644 --- a/src/InstagramFeed.js +++ b/src/InstagramFeed.js @@ -1,10 +1,7 @@ /* * InstagramFeed * - * @version 1.4.0 - * - * @author jsanahuja - * @contributor csanahuja + * @version 2.0.0 * * https://github.com/jsanahuja/InstagramFeed * @@ -35,6 +32,7 @@ 'margin': 0.5, 'image_size': 640, 'lazy_load': false, + 'cache_time': 120, 'on_error': console.error }; @@ -56,6 +54,7 @@ '`': '`', '=': '=' }; + function escape_string(str){ return str.replace(/[&<>"'`=\/]/g, function (char) { return escape_map[char]; @@ -96,11 +95,26 @@ } data = JSON.parse(data.substr(0, data.length - 1)); data = data.entry_data.ProfilePage || data.entry_data.TagPage; - if(typeof data === "undefined"){ - _this.options.on_error("InstagramFeed: It looks like YOUR network has been temporary banned because of too many requests. See https://github.com/jsanahuja/jquery.instagramFeed/issues/25", 4); - return; + + var skipCaching = false; + if (typeof data === "undefined") { + var cache_data_raw = localStorage.getItem(_this.cache_data_key); + if (cache_data_raw !== null) { + data = JSON.parse(cache_data_raw); + skipCaching = true; + } + + _this.options.on_error("Instagram Feed: Your network has been temporary banned by Instagram because of too many requests. Consider increasing your 'cache_time'. See https://github.com/jsanahuja/jquery.instagramFeed/issues/25 and https://github.com/jsanahuja/jquery.instagramFeed/issues/101", 4); + if (!data) return; + }else{ + data = data[0].graphql.user || data[0].graphql.hashtag; } - data = data[0].graphql.user || data[0].graphql.hashtag; + + if (!skipCaching && _this.options.cache_time > 0) { + localStorage.setItem(_this.cache_data_key, JSON.stringify(data)); + localStorage.setItem(_this.cache_data_key_cached, new Date().getTime()); + } + callback(data, _this); } else { _this.options.on_error("InstagramFeed: Unable to fetch the given user/tag. Instagram responded with the status code: " + xhr.statusText, 5); @@ -261,14 +275,37 @@ }; this.run = function() { - this.get(function(data, instance) { - if(instance.options.container != ""){ - instance.display(data); + var cache_data = null; + this.cache_data_key = 'InstagramFeed_' + (this.is_tag ? 't_' + this.options.tag : 'u_' + this.options.username); + this.cache_data_key_cached = this.cache_data_key + '_cached'; + + if (this.options.cache_time > 0) { + var cached_time = localStorage.getItem(this.cache_data_key_cached); + if (cached_time !== null && parseInt(cached_time) + 1000 * 60 * this.options.cache_time > new Date().getTime()) { + var cache_data_raw = localStorage.getItem(this.cache_data_key); + if (cache_data_raw !== null) { + cache_data = JSON.parse(cache_data_raw); + } + } + } + + if (cache_data !== null) { + if(this.options.container != ""){ + this.display(cache_data); } - if(typeof instance.options.callback === "function"){ - instance.options.callback(data); + if(typeof this.options.callback === "function"){ + this.options.callback(cache_data); } - }); + }else{ + this.get(function(data, instance) { + if(instance.options.container != ""){ + instance.display(data); + } + if(typeof instance.options.callback === "function"){ + instance.options.callback(data); + } + }); + } }; if (this.valid) {