|
1 | 1 | /*!
|
2 | 2 | * jquery.instagramFeed
|
3 | 3 | *
|
4 |
| - * @version 1.4.1 |
| 4 | + * @version 2.0.0 |
5 | 5 | *
|
6 | 6 | * @author jsanahuja <[email protected]>
|
7 | 7 | * @contributor csanahuja <[email protected]>
|
|
27 | 27 | 'margin': 0.5,
|
28 | 28 | 'image_size': 640,
|
29 | 29 | 'lazy_load': false,
|
| 30 | + 'cache_time': 120, |
30 | 31 | 'on_error': console.error
|
31 | 32 | };
|
32 | 33 | var image_sizes = {
|
|
78 | 79 | }
|
79 | 80 |
|
80 | 81 | $.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; |
93 | 92 |
|
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 | + } |
109 | 108 | }
|
| 109 | + |
110 | 110 | data = data[0].graphql.user || data[0].graphql.hashtag;
|
111 |
| - |
| 111 | + |
112 | 112 | if (options.container != "") {
|
113 | 113 | var html = "",
|
114 | 114 | styles;
|
|
232 | 232 | options.callback(data);
|
233 | 233 | }
|
234 | 234 |
|
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 | + } |
238 | 273 |
|
239 | 274 | return true;
|
240 | 275 | };
|
|
0 commit comments