Skip to content

Commit

Permalink
Add back getBaseDomain()
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 committed Dec 19, 2024
1 parent d82873c commit 1d80b29
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/tests/SimilarWeb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import logger from '../logger.js';

/**
* Retrieve the Similarweb rank for a given domain
* @param {string} domain The domain to check
* @param {string} fqdn The domain to check
* @param {*} env The environment
* @returns {Promise<string>} Returns SimilarWeb rank if found
* @throws {Object<message, title>} Throws error object if fetching fails or rank exceeds limit
*/
export default async function(domain, env) {
export default async function(fqdn, env) {
const domain = getBaseDomain(fqdn);
const apiKey = getAPIKey(env);
const res = await fetch(
`https://api.similarweb.com/v1/similar-rank/${domain}/rank?api_key=${apiKey}`,
Expand Down Expand Up @@ -78,3 +79,16 @@ function getAPIKey(env) {
const keys = env.SIMILARWEB_API_KEY.split(' ');
return keys[Math.floor(Math.random() * keys.length)];
}

/**
* Return the base domain of a domain with possible subdomains
* @param {string} hostname The domain to parse
* @returns {string} The base domain
*/
function getBaseDomain(hostname) {
let parts = hostname.split('.');
if (parts.length <= 2) return hostname;
parts = parts.slice(-3);
if (['co', 'com'].includes(parts[1])) return parts.join('.');
return parts.slice(-2).join('.');
}

0 comments on commit 1d80b29

Please sign in to comment.