-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
145 lines (132 loc) · 4.1 KB
/
Copy pathscript.js
File metadata and controls
145 lines (132 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const resourcePath = "/test-data.json";
const theGallery = document.querySelector("#all-the-photos");
const awesomeList = document.querySelector("#awesome-list-of-places");
const newList = [];
function cleanUpData(rawData) {
// The raw data contains check-ins that are not of interest
// and are not pushed to the new list of relevant items
rawData.forEach(function (item, index, array) {
// Set up a flag to mark if item is of interest
let interesting = true;
// Check-ins without location? Not interesting
if (item.venue_name === null) {
interesting = false;
}
// Multiple beers at same venue same day? Flip flag!
let previousItem = array[index - 1];
if (index > 0) {
if (item.venue_name == previousItem.venue_name) {
interesting = false;
}
}
// Turn JSON string into an actual Date object
item.created_at = new Date(Date.parse(item.created_at));
// Skip any items from cities that I lived in at that time
const movedToOslo = new Date("2015-08-31");
if (
item.created_at.getTime() < movedToOslo.getTime() &&
item.venue_city === "Bergen"
) {
interesting = false;
}
if (
item.created_at.getTime() > movedToOslo.getTime() &&
item.venue_city === "Oslo"
) {
interesting = false;
}
// Make a new list of all interesting check-ins!
if (interesting) {
newList.push(item);
}
});
// Pass this cleaned up list into other functions
createGallery(newList);
createMarkup(newList);
// downloadPhotos(newList); // uncomment to call 🤠
}
// Not sure I need to use this function again, it might have done its job
// https://elisabethirgens.github.io/notes/2023/11/free-my-photos/
function downloadPhotos() {
newList.forEach(function (item, index) {
fetchWithAnchorElement(item, index);
async function fetchWithAnchorElement() {
const img = await fetch(item.photo_url);
console.log(item);
const imgBlob = await img.blob();
const imgUrl = URL.createObjectURL(imgBlob);
const anchor = document.createElement("a");
anchor.href = imgUrl;
anchor.download = `beer${index}.jpg`;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
}
});
}
function createGallery(everything) {
let galleryMarkup = "";
let copyToReverse = Array.from(everything);
for (let item of copyToReverse.reverse()) {
if (item.photo_url) {
galleryMarkup += `
<img
src="${item.photo_url}" class="photo"
alt="${item.venue_name}" />
`;
}
}
try {
theGallery.innerHTML = galleryMarkup;
} catch (error) {
console.log(error);
}
}
function createMarkup(everywhere) {
// Loop through the array, spot by spot and create markup
// This is currently local json data in a tiny test file,
// but should later on be sanitized for increased security
let htmlString = "";
for (let spot of everywhere) {
// Format date to something readable
var formattedDate = Intl.DateTimeFormat("en-GB", {
day: "numeric",
month: "short",
year: "numeric",
}).format(spot.created_at);
htmlString += `
<div class="place">
<span class="location">${spot.venue_name} </span>
<span class="where">
${spot.venue_city ? spot.venue_city + "," : ""}
${spot.venue_country}</span>
<span class="when">${formattedDate}</span>
</div>
`;
}
try {
awesomeList.innerHTML = htmlString;
} catch (error) {
console.log(error);
}
}
function goAndFetchSomeData() {
// Use the fetch() method to get data from the json file into this script,
// returning a promise which is fulfilled when the response is available
// and handle errors if something doesn't work
fetch(resourcePath)
.then(function (response) {
if (response.ok) {
return response.json();
}
throw response.status;
})
.then(function (data) {
// Pass data into the cleanUpData function
cleanUpData(data);
})
.catch(function (error) {
console.warn(error);
});
}
goAndFetchSomeData();