Skip to content

Commit

Permalink
Use all.json in /search
Browse files Browse the repository at this point in the history
  • Loading branch information
Ry0taK committed Jun 22, 2024
1 parent 80561a7 commit 55e6162
Showing 1 changed file with 4 additions and 47 deletions.
51 changes: 4 additions & 47 deletions root/search/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,38 +139,9 @@ <h1><a href=/>OWSC</a></h1>
</div>
</footer>
<script>
function getDatabaseURL(year, id) {
return `https://raw.githubusercontent.com/gnknzm/crss.gnknzm.net/master/database/CRE-${year}-${id + 36000}.json?${Math.random()}`;
}

async function getLastCREinYear(year, start) {
let diff_power = 0;
function* getRequestPromises() {
for(let i = 0; ; i++) {
yield fetch(getDatabaseURL(year, start + (1 << i)));
}
}
for await (const resp of getRequestPromises()) {
if (resp.status !== 200) {
break;
}
diff_power++;
}

let end = start;
while (diff_power--) {
const resp = await fetch(getDatabaseURL(year, end));
if (resp.status === 200) {
end += 1 << diff_power;
} else {
end -= 1 << diff_power;
}
}
const resp = await fetch(getDatabaseURL(year, end));
if (resp.status !== 200) {
end--;
}
return end;
async function retrieveDatabase() {
const resp = await fetch(`https://raw.githubusercontent.com/gnknzm/crss.gnknzm.net/master/database/all.json?${Math.random()}`);
return await resp.json();
}

function createQueryResult(result) {
Expand Down Expand Up @@ -217,21 +188,7 @@ <h1><a href=/>OWSC</a></h1>
const query = new URLSearchParams(location.search).get('q') ?? "";
document.getElementById('query').value = decodeURIComponent(query);

const database = JSON.parse(localStorage.getItem('database')) ?? [];
const startYear = 2024 + database.length;
const endYear = new Date().getFullYear();
for (let i = 0; i <= endYear - startYear; i++) {
const year = startYear + i;
const end = await getLastCREinYear(year, database.length);
const allResps = await Promise.all(
new Array(end + 1)
.slice(database.length)
.keys()
.map(id => fetch(getDatabaseURL(year, id)))
);
const allData = await Promise.all(allResps.map(resp => resp.json()));
database[i] = (database[i] ?? []).concat(allData);
}
const database = await retrieveDatabase();
localStorage.setItem('database', JSON.stringify(database));
document.getElementById('query').addEventListener('input', updateQueryResult);
updateQueryResult();
Expand Down

0 comments on commit 55e6162

Please sign in to comment.