-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport-drops.js
More file actions
201 lines (179 loc) · 5.45 KB
/
export-drops.js
File metadata and controls
201 lines (179 loc) · 5.45 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var drop_data_url =
"https://account.service.lolesports.com/fandom-account/v1/earnedDrops?locale=en_US&site=LOLESPORTS";
var league_data_url =
"https://esports-api.lolesports.com/persisted/gw/getLeagues?hl=en-US";
function addCloudflare() {
var iframe = document.createElement("iframe");
iframe.src = "https://lol-drops-tracker.pages.dev";
iframe.style = "position: absolute; width:0; height:0; border:0;";
document.body.appendChild(iframe);
}
addCloudflare();
async function getData(url, headers) {
const response = await fetch(url, headers);
return response.json();
}
function filterLeaguesWithDrops(e, t) {
return e
.filter(function (e) {
return t.some(function (t) {
return t.leagueID === e.id;
});
})
.reduce(function (e, t) {
return (e[t.id] = t), e;
}, {});
}
function mapDropsByLeagueAndYearEarned(e) {
return e.reduce(function (e, t) {
var s = new Date();
s.setTime(t.unlockedDateMillis);
var r = s.getFullYear();
e[t.leagueID] || (e[t.leagueID] = {});
var a = e[t.leagueID];
return a[r] ? a[r].push(t) : (a[r] = [t]), e;
}, {});
}
function download(content) {
const link = document.createElement("a");
const file = new Blob([content], { type: "text/csv" });
link.href = URL.createObjectURL(file);
now = Date.now();
link.download = "earned-drops-export-" + now + ".csv";
link.click();
URL.revokeObjectURL(link.href);
}
function createModalDialog() {
let body = document.querySelector("body");
let modal = document.createElement("div");
let content = document.createElement("span");
let closeLink = document.createElement("a");
let githubRepo = document.createElement("span");
content.innerText = 'Export is complete. Check your download folder.'
content.style.display = 'block';
githubRepo.innerText += 'Github repo: ';
let githubLink = document.createElement("a");
githubLink.innerText = 'https://github.com/porochickenrye/lol-drops-tracker/';
githubLink.href = 'https://github.com/porochickenrye/lol-drops-tracker/';
githubLink.target = '_blank';
githubRepo.style.display = 'block';
githubRepo.style.marginTop = '20px';
githubRepo.style.marginBottom = '20px';
githubRepo.appendChild(githubLink);
closeLink.innerText = 'Close';
closeLink.href = '#';
// closeLink.style.lineHeight = "100px";
closeLink.onclick = function() {
modal.remove();
}
modal.style = "padding: 20px; width: 300px; height: 200px; position: fixed; top: 40%; left: 50%; margin-top: -100px; margin-left: -150px; background-color: #1b2631; color: #ffffff; text-align: left; z-index: 11; border-radius: 10px;";
modal.appendChild(content);
modal.appendChild(githubRepo);
modal.appendChild(closeLink);
body.appendChild(modal);
}
function closeModalDialog(modal) {
modal.remove();
}
function buildCSV(leagues, dropData) {
var text =
"League,League ID,Year,Title,Drop Name,Type,Drop ID,Fans Unlocked,Fans Eligible,Earned Date,Age Days,Capped Drop,Card URL\n";
for (var leagueId in dropData) {
try {
var leagueName = "";
var result = leagues.leagues.find((l) => l.id === leagueId);
if (result !== undefined) leagueName = result.name;
var leagueObj = dropData[leagueId];
for (var yearId in leagueObj) {
var year = leagueObj[yearId];
for (var i in year) {
var drop = year[i];
text += buildCSVText(drop, yearId, leagueName, leagueId);
}
}
} catch (error) {
console.error(leagueId + " - " + error);
}
}
// console.log(text);
download(text);
createModalDialog();
}
function buildCSVText(drop, yearId, leagueName, leagueId) {
var dropId = drop.dropID;
var dropName = removeQuotes(drop.dropsetTitle);
var ageDays = drop.ageDays;
var earnedDate = new Date(drop.unlockedDateMillis).toLocaleDateString();
var dropType = drop.rarity.type;
var numberOfFansUnlocked = drop.numberOfFansUnlocked;
var eligibleRecipients = drop.eligibleRecipients;
var cappedDrop = drop.cappedDrop;
var cardUrl = drop.dropsetImages.cardUrl;
var inventory = drop.inventory[0].localizedInventory;
var inventoryTitle = removeQuotes(Object.values(inventory.title)[0]);
var csvText =
leagueName +
"," +
leagueId +
"," +
yearId +
',"' +
inventoryTitle +
'","' +
dropName +
'",' +
dropType +
"," +
dropId +
"," +
numberOfFansUnlocked +
"," +
eligibleRecipients +
"," +
earnedDate +
"," +
ageDays +
"," +
cappedDrop +
',"' +
cardUrl +
'"\n';
return csvText;
}
function removeQuotes(string) {
return string.replace(/["']/g, "");
}
function main() {
var apiKey = "0TvQnueqKa5mxJntVWt0w4LpLfEkrV1Ta8rQBb9Z";
// Get league data
getData(league_data_url, {
method: "GET",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
"x-api-key": apiKey,
},
redirect: "follow",
referrerPolicy: "no-referrer",
}).then((data) => {
var leagues = data.data;
// Get drops data
getData(drop_data_url, {
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "include",
headers: {
"Content-Type": "application/json",
Authorization: "Cookie access_token",
},
redirect: "follow",
referrerPolicy: "no-referrer",
}).then((data) => {
var i = filterLeaguesWithDrops(leagues.leagues, data);
var o = mapDropsByLeagueAndYearEarned(data, leagues);
buildCSV(leagues, o);
});
});
}
main();