Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from pdehaan/amo-api
Convert to using AMO API instead of scraping website
- Loading branch information
Showing
6 changed files
with
136 additions
and
384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,49 @@ | ||
const puppeteer = require("puppeteer"); | ||
const axios = require("axios"); | ||
|
||
const amoClient = axios.create({ | ||
baseURL: "https://addons.mozilla.org/api/v4/" | ||
}); | ||
|
||
module.exports = { | ||
fetchAddons | ||
}; | ||
|
||
async function fetchAddons(addons) { | ||
const browser = await puppeteer.launch(); | ||
const page = await browser.newPage(); | ||
|
||
const data = []; | ||
for (const item of addons) { | ||
const res = []; | ||
const categoryAddons = []; | ||
for (const name of item.addons) { | ||
res.push(await getAddon(page, name)); | ||
categoryAddons.push(getAddon(name)); | ||
} | ||
|
||
return { | ||
data.push({ | ||
category: item.category, | ||
addons: res | ||
}; | ||
addons: await Promise.all(categoryAddons) | ||
}); | ||
} | ||
|
||
await browser.close(); | ||
return data; | ||
} | ||
|
||
async function getAddon(page, slug) { | ||
await page.goto(`https://addons.mozilla.org/firefox/addon/${slug}/`); | ||
const title = await page.$eval(".AddonTitle", el => el.textContent); | ||
const author = await page.$eval(".AddonTitle-author", el => el.textContent); | ||
const summary = await page.$eval(".Addon-summary", el => el.textContent); | ||
const isFeatured = await page.$(".Badge-featured"); | ||
const [users, reviews, rating] = await page.$$eval(".MetadataCard-list", nodes => nodes.map(n => n.innerText)); | ||
const version = await page.$eval(".AddonMoreInfo-version", el => el.textContent); | ||
|
||
const homepage = await page.$eval(".AddonMoreInfo-homepage-link", el => decodeURIComponent(el.getAttribute("href").replace(/^.*(https?)%3A/, "$1:"))); | ||
|
||
async function getAddon(slug) { | ||
const {data: addon} = await amoClient.get(`/addons/addon/${slug}`); | ||
return { | ||
slug, | ||
title: title.replace(author, "").trim(), | ||
author: author.replace(/^by /, "").trim(), | ||
version, | ||
summary, | ||
isFeatured: !!isFeatured, | ||
homepage, | ||
metadata: { | ||
users: numberParser(users), | ||
reviews: numberParser(reviews), | ||
rating: parseFloat(rating.split("\n").pop()) | ||
} | ||
name: addon.name["en-US"], | ||
version: addon.current_version.version, | ||
guid: addon.guid, | ||
author: addon.authors[0].name, | ||
summary: addon.summary["en-US"], | ||
type: addon.type, | ||
size: addon.current_version.files[0].size, | ||
createdDate: new Date(addon.created), | ||
lastUpdated: new Date(addon.last_updated), | ||
license: addon.current_version.license.name["en-US"], | ||
isFeatured: addon.is_featured, | ||
isSourcePublic: addon.is_source_public, | ||
ratings: addon.ratings.average, | ||
reviewCount: addon.ratings.count, | ||
users: addon.average_daily_users, | ||
weeklyDownloads: addon.weekly_downloads, | ||
homepage: addon.homepage["en-US"], | ||
url: addon.url.replace("/en-US/", "/"), | ||
slug | ||
}; | ||
} | ||
|
||
function numberParser(value) { | ||
const _value = value.replace("\n", " ").replace(/,/g, ""); | ||
return parseInt(_value, 10); | ||
} |
Oops, something went wrong.