Skip to content
This repository was archived by the owner on May 3, 2021. It is now read-only.

Commit 4e77a43

Browse files
authored
New feature: Caching instagram response option (#97)
1 parent 2fa84c8 commit 4e77a43

File tree

4 files changed

+78
-35
lines changed

4 files changed

+78
-35
lines changed

index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ <h3>Options</h3>
280280
<td><code>https://www.instagram.com/</code></td>
281281
<td>URL where to fetch the data. Useful if instagram changes CORS policy</td>
282282
</tr>
283+
<tr>
284+
<td class="d-flex flex-wrap">
285+
<code class="text-bold flex-grow-1">cache_time</code>
286+
</td>
287+
<td><em>number [Int]</em></td>
288+
<td><code>120</code></td>
289+
<td>Instagram response cache expiry time in minutes</td>
290+
</tr>
283291
<tr>
284292
<td class="d-flex flex-wrap">
285293
<code class="text-bold flex-grow-1">on_error</code>

jquery.instagramFeed.js

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* jquery.instagramFeed
33
*
4-
* @version 1.4.1
4+
* @version 2.0.0
55
*
66
* @author jsanahuja <[email protected]>
77
* @contributor csanahuja <[email protected]>
@@ -27,6 +27,7 @@
2727
'margin': 0.5,
2828
'image_size': 640,
2929
'lazy_load': false,
30+
'cache_time': 120,
3031
'on_error': console.error
3132
};
3233
var image_sizes = {
@@ -78,37 +79,36 @@
7879
}
7980

8081
$.instagramFeed = function (opts) {
81-
var options = $.fn.extend({}, defaults, opts);
82-
if (options.username == "" && options.tag == "") {
83-
options.on_error("Instagram Feed: Error, no username nor tag defined.", 1);
84-
return false;
85-
}
86-
if (typeof options.get_data !== "undefined") {
87-
console.warn("Instagram Feed: options.get_data is deprecated, options.callback is always called if defined");
88-
}
89-
if (options.callback == null && options.container == "") {
90-
options.on_error("Instagram Feed: Error, neither container found nor callback defined.", 2);
91-
return false;
92-
}
82+
function on_get_insta_data(data) {
83+
if (typeof data === 'string') {
84+
try {
85+
data = data.split("window._sharedData = ")[1].split("<\/script>")[0];
86+
} catch (e) {
87+
options.on_error("Instagram Feed: It looks like the profile you are trying to fetch is age restricted. See https://github.com/jsanahuja/InstagramFeed/issues/26", 3);
88+
return;
89+
}
90+
data = JSON.parse(data.substr(0, data.length - 1));
91+
data = data.entry_data.ProfilePage || data.entry_data.TagPage;
9392

94-
var is_tag = options.username == "",
95-
url = is_tag ? options.host + "explore/tags/" + options.tag + "/" : options.host + options.username + "/";
96-
97-
$.get(url, function (data) {
98-
try {
99-
data = data.split("window._sharedData = ")[1].split("<\/script>")[0];
100-
} catch (e) {
101-
options.on_error("Instagram Feed: It looks like the profile you are trying to fetch is age restricted. See https://github.com/jsanahuja/InstagramFeed/issues/26", 3);
102-
return;
103-
}
104-
data = JSON.parse(data.substr(0, data.length - 1));
105-
data = data.entry_data.ProfilePage || data.entry_data.TagPage;
106-
if (typeof data === "undefined") {
107-
options.on_error("Instagram Feed: It looks like YOUR network has been temporary banned because of too many requests. See https://github.com/jsanahuja/jquery.instagramFeed/issues/25", 4);
108-
return;
93+
var skipCaching = false;
94+
if (typeof data === "undefined") {
95+
var cache_data_raw = localStorage.getItem(cache_data_key);
96+
if (cache_data_raw !== null) {
97+
data = JSON.parse(cache_data_raw);
98+
skipCaching = true;
99+
}
100+
101+
options.on_error("Instagram Feed: It looks like YOUR network has been temporary banned because of too many requests. See https://github.com/jsanahuja/jquery.instagramFeed/issues/25", 4);
102+
if (!data) return;
103+
}
104+
if (!skipCaching && options.cache_time > 0) {
105+
localStorage.setItem(cache_data_key, JSON.stringify(data));
106+
localStorage.setItem(cache_data_key_cached, new Date().getTime());
107+
}
109108
}
109+
110110
data = data[0].graphql.user || data[0].graphql.hashtag;
111-
111+
112112
if (options.container != "") {
113113
var html = "",
114114
styles;
@@ -232,9 +232,44 @@
232232
options.callback(data);
233233
}
234234

235-
}).fail(function (e) {
236-
options.on_error("Instagram Feed: Unable to fetch the given user/tag. Instagram responded with the status code: " + e.status, 5);
237-
});
235+
}
236+
237+
var options = $.fn.extend({}, defaults, opts);
238+
if (options.username == "" && options.tag == "") {
239+
options.on_error("Instagram Feed: Error, no username nor tag defined.", 1);
240+
return false;
241+
}
242+
if (typeof options.get_data !== "undefined") {
243+
console.warn("Instagram Feed: options.get_data is deprecated, options.callback is always called if defined");
244+
}
245+
if (options.callback == null && options.container == "") {
246+
options.on_error("Instagram Feed: Error, neither container found nor callback defined.", 2);
247+
return false;
248+
}
249+
250+
var is_tag = options.username == "",
251+
url = is_tag ? options.host + "explore/tags/" + options.tag + "/" : options.host + options.username + "/",
252+
cache_data = null,
253+
cache_data_key = 'instagramFeed_' + (is_tag ? 't_' + options.tag : 'u_' + options.username),
254+
cache_data_key_cached = cache_data_key + '_cached';
255+
256+
if (options.cache_time > 0) {
257+
var cached_time = localStorage.getItem(cache_data_key_cached);
258+
if (cached_time !== null && parseInt(cached_time) + 1000 * 60 * options.cache_time > new Date().getTime()) {
259+
var cache_data_raw = localStorage.getItem(cache_data_key);
260+
if(cache_data_raw !== null){
261+
cache_data = JSON.parse(cache_data_raw);
262+
}
263+
}
264+
}
265+
266+
if (cache_data !== null) {
267+
on_get_insta_data(cache_data);
268+
} else {
269+
$.get(url, on_get_insta_data).fail(function (e) {
270+
options.on_error("Instagram Feed: Unable to fetch the given user/tag. Instagram responded with the status code: " + e.status, 5);
271+
});
272+
}
238273

239274
return true;
240275
};

jquery.instagramFeed.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "jquery.instagramFeed",
33
"description": "Instagram Feed without access token. Not using the Instagram API",
44
"homepage": "https://github.com/jsanahuja/jquery.instagramFeed",
5-
"version": "1.4.1",
5+
"version": "2.0.0",
66
"keywords": [
77
"instagram",
88
"feed",
@@ -38,4 +38,4 @@
3838
"test": "grunt test",
3939
"build": "grunt build"
4040
}
41-
}
41+
}

0 commit comments

Comments
 (0)