Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Commit

Permalink
[FIX]: Now scores of <= 2 pages download correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Yip committed Sep 23, 2021
1 parent 41c7a9a commit c716243
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.0
2.2.1
6 changes: 3 additions & 3 deletions musescore_scraper/MuseScraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ async def get_score_tags() -> str:
"Keywords": await get_score_tags(),
}

imgs: List[str] = await page.evaluate(str(get_data("musescore_scraper",
"script.js",
), "utf-8"))
imgs: List[str] = await asyncio.wait_for(page.evaluate(str(get_data("musescore_scraper",
"script.js",
), "utf-8")), self.timeout)

await page.close()

Expand Down
31 changes: 23 additions & 8 deletions musescore_scraper/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,48 @@
classCounts[childClass] = [];
}
classCounts[childClass].push(child);
if (desiredClass === undefined || classCounts[childClass].length > classCounts[desiredClass].length) {
if (desiredClass === undefined
|| classCounts[childClass].length > classCounts[desiredClass].length) {
desiredClass = childClass;
}
}
let svgs = [];

//If it's 2 pages, the 2nd page may be already loaded, thus making generic function not work.
if (classCounts[desiredClass].length <= 2) {
let imgs = [];
for (let div of classCounts[desiredClass]) {
img = div.querySelector("img");
if (img !== undefined) {
imgs.push(img.src);
}
}
if (imgs.length == classCounts[desiredClass].length) {
return imgs;
}
}

let imgs = [];
let i = undefined;

scrollDiv.scroll({
left : 0,
top : 0,
behavior : "smooth"
});

return new Promise(resolve => {
function addSvg(records, observer) {
function addImg(records, observer) {
for (let record of records) { //records is a list of MutationRecords
if (record.target.tagName === "IMG"
&& record.attributeName === "src"
&& record.target.src !== undefined)
{
svgs.push(record.target.src);
imgs.push(record.target.src);

if (classCounts[desiredClass].indexOf(record.target.parentElement)
== classCounts[desiredClass].length - 1)
{
observer.disconnect();
resolve(svgs);
resolve(imgs);
}
else
{
Expand All @@ -45,7 +60,7 @@
}
}
}
let observer = new MutationObserver(addSvg);
let observer = new MutationObserver(addImg);
for (let child of classCounts[desiredClass]) {
observer.observe(child, {
attributes: true,
Expand All @@ -57,7 +72,7 @@
}
else
{
addSvg([{
addImg([{
attributeName: "src",
target: classCounts[desiredClass][0].querySelector("img"),
}]);
Expand Down

0 comments on commit c716243

Please sign in to comment.