-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #3656 - Fetch and save top domains from Tranco and optionally A…
…lexa
- Loading branch information
Showing
12 changed files
with
702 additions
and
316 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
"""Tests for Siterank class.""" | ||
|
||
import unittest | ||
from unittest.mock import patch | ||
|
||
from tools.topsites.siterank import SiteRank | ||
|
||
|
||
class TestSiteRank(unittest.TestCase): | ||
"""Tests for Top Sites Tools.""" | ||
|
||
def setUp(self): | ||
"""Set up the tests.""" | ||
self.siterank = SiteRank() | ||
|
||
def test_get_priority(self): | ||
"""Get a priority based on rank and initial priority.""" | ||
self.assertEqual(self.siterank.get_priority(100, 1), 1) | ||
self.assertEqual(self.siterank.get_priority(1000, 1), 2) | ||
self.assertEqual(self.siterank.get_priority(9999, 1), 3) | ||
self.assertEqual(self.siterank.get_priority(100, 2), 2) | ||
self.assertEqual(self.siterank.get_priority(999, 2), 3) | ||
|
||
@patch('tools.topsites.siterank.site_global_session.add') | ||
def test_save_global_rank(self, session_mock): | ||
"""Save domain with a global rank.""" | ||
site_row = self.siterank.save_global_rank('example.com', 555) | ||
self.assertEqual(self.siterank.sites_global, {'example.com': site_row}) | ||
|
||
@patch('tools.topsites.siterank.site_global_session.add') | ||
@patch('tools.topsites.siterank.site_regional_session.add') | ||
def test_save_regional_rank(self, r_mock, g_mock): | ||
"""Save domain with a regional rank.""" | ||
regional_site_row = self.siterank.save_regional_rank( | ||
'mysite.com', 49, 'DE' | ||
) | ||
self.assertEqual(self.siterank.sites_regional, { | ||
'mysite.com': regional_site_row | ||
}) | ||
|
||
@patch('tools.topsites.siterank.site_global_session.add') | ||
@patch('tools.topsites.siterank.site_regional_session.add') | ||
def test_save_regional_rank_same(self, r_mock, g_mock): | ||
"""Regional rank is not saved because its priority is the same in global.""" # noqa | ||
global_site_row = self.siterank.save_global_rank('example.com', 555) | ||
self.siterank.save_regional_rank('example.com', 48, 'GB') | ||
|
||
self.assertEqual(self.siterank.sites_global, { | ||
'example.com': global_site_row | ||
}) | ||
self.assertEqual(self.siterank.sites_regional, {}) | ||
|
||
@patch('tools.topsites.siterank.site_global_session.add') | ||
@patch('tools.topsites.siterank.site_regional_session.add') | ||
def test_save_regional_rank_lower(self, r_mock, g_mock): | ||
"""Regional rank is saved because priority is lower than in global.""" | ||
global_site_row = self.siterank.save_global_rank('example.com', 1001) | ||
regional_site_row = self.siterank.save_regional_rank( | ||
'example.com', 48, 'GB' | ||
) | ||
self.assertEqual(self.siterank.sites_global, { | ||
'example.com': global_site_row | ||
}) | ||
self.assertEqual(self.siterank.sites_regional, { | ||
'example.com': regional_site_row | ||
}) | ||
|
||
@patch('tools.topsites.siterank.site_global_session.add') | ||
@patch('tools.topsites.siterank.site_regional_session.add') | ||
def test_save_regional_rank_multiple(self, r_mock, g_mock): | ||
"""If a site is popular in two countries, higher priority one will be saved.""" # noqa | ||
self.siterank.save_regional_rank('example.com', 101, 'GB') | ||
regional_site_row_2 = self.siterank.save_regional_rank( | ||
'example.com', 49, 'DE' | ||
) | ||
self.assertEqual(self.siterank.sites_regional, { | ||
'example.com': regional_site_row_2, | ||
}) | ||
|
||
@patch('tools.topsites.siterank.site_global_session.add') | ||
@patch('tools.topsites.siterank.site_regional_session.add') | ||
def test_save_global_regional_rank_multiple(self, r_mock, g_mock): | ||
"""If a site is popular globally and in multiple countries, higher priority one will be saved.""" # noqa | ||
global_site_row = self.siterank.save_global_rank('example.com', 12) | ||
self.siterank.save_regional_rank('example.com', 101, 'GB') | ||
self.siterank.save_regional_rank('example.com', 49, 'DE') | ||
|
||
self.assertEqual(self.siterank.sites_global, { | ||
'example.com': global_site_row | ||
}) | ||
self.assertEqual(self.siterank.sites_regional, {}) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
""" | ||
Script for assigning priority to sites. | ||
The rule is as follows: | ||
Critical: alexa top 100 in worldwide | ||
Important: alexa top 101-1000 in worldwide or alexa top 100 in tier 1 countries/regions | ||
Normal: alexa top 1001-10000 or alexa top 101-1000 in tier 1 countries/regions | ||
""" | ||
|
||
import argparse | ||
|
||
from tools.topsites.tranco import Tranco | ||
from tools.topsites.alexa import Alexa | ||
from tools.topsites.siterank import SiteRank | ||
from tools.topsites.siterank import DATA_PATH | ||
from tools.topsites.utils import parse_site_dom_element | ||
|
||
REGIONS = ['US', 'FR', 'IN', 'DE', 'TW', 'ID', 'HK', 'SG', 'PL', | ||
'GB', 'RU'] | ||
|
||
|
||
def main() -> None: | ||
description = "Script to fetch and update top site ranking from Tranco and optionally Alexa." | ||
parser = argparse.ArgumentParser(description=description) | ||
|
||
parser.add_argument( | ||
"--retrieve-regional", | ||
action="store_true", | ||
help="Whether to retrieve regional ranking from Alexa (access and secret keys will be required).", | ||
) | ||
|
||
parser.add_argument( | ||
"--ats_access_key", | ||
help="AWS access key.", | ||
type=str, | ||
) | ||
parser.add_argument( | ||
"--ats_secret_key", | ||
help="AWS secret key.", | ||
type=str, | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.retrieve_regional: | ||
print('Warning: Alexa APIs will be deprecated on December 15, 2022.') | ||
|
||
if not (args.ats_secret_key and args.ats_access_key): | ||
print('Please specify secret and access key for Alexa API.') | ||
return | ||
|
||
tranco = Tranco(DATA_PATH) | ||
tranco_list = tranco.get_list() | ||
siterank = SiteRank() | ||
|
||
# Get global ranking | ||
for (i, domain) in enumerate(tranco_list, start=1): | ||
siterank.save_global_rank(domain, i) | ||
|
||
# Get ranking per country | ||
if args.retrieve_regional: | ||
alexa = Alexa(args.ats_access_key, args.ats_secret_key) | ||
for country_code in REGIONS: | ||
sites = alexa.query_topsites(country_code=country_code) | ||
for site in sites: | ||
url, rank = parse_site_dom_element(site) | ||
siterank.save_regional_rank(url, rank, country_code) | ||
|
||
siterank.commit_regional() | ||
|
||
siterank.commit_global() | ||
|
||
siterank.cleanup_db(args.retrieve_regional) | ||
tranco.cleanup() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
|
Oops, something went wrong.