diff --git a/scraper/static/removeOldResult.js b/scraper/static/removeOldResult.js new file mode 100644 index 0000000..ee6c9c2 --- /dev/null +++ b/scraper/static/removeOldResult.js @@ -0,0 +1,24 @@ +// Get array of all dates +var dates = []; +for (var i = 0; i < localStorage.length; i++) { + var shortened_url = localStorage.key(i); + var data = JSON.parse(localStorage.getItem(shortened_url)); + dates.push(data.date); +} + +// Sort the dates in descending order +dates.sort(function(a, b) { + return new Date(b) - new Date(a); +}); + +// Select the two most recent dates +var mostRecentDates = dates.slice(0, 10); + +// Remove any items that are not in the most recent two dates +for (var i = localStorage.length - 1; i >= 0; i--) { + var shortened_url = localStorage.key(i); + var data = JSON.parse(localStorage.getItem(shortened_url)); + if (mostRecentDates.indexOf(data.date) === -1) { + localStorage.removeItem(shortened_url); + } +} \ No newline at end of file diff --git a/scraper/templates/scraper/index-result-table.html b/scraper/templates/scraper/index-result-table.html index d7fa9aa..e9efeff 100644 --- a/scraper/templates/scraper/index-result-table.html +++ b/scraper/templates/scraper/index-result-table.html @@ -31,6 +31,7 @@ + diff --git a/scraper/templates/scraper/result.html b/scraper/templates/scraper/result.html index a368854..673af7a 100644 --- a/scraper/templates/scraper/result.html +++ b/scraper/templates/scraper/result.html @@ -8,7 +8,8 @@ image: "{{ image }}", title: "{{ title }}", rating: "{{ price_rating }}", - price: "{{ price }}" + price: "{{ price }}", + date: new Date() }; var shortened_url = "{{ shortened_url }}"; localStorage.setItem(shortened_url, JSON.stringify(item));