This repository has been archived by the owner on Dec 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
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 #87 from mozilla/86-check-logo
Performs existence check on Clearbit logo before returning (closes #86).
- Loading branch information
Showing
6 changed files
with
112 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
from recommendation.search.classification.domain import DomainClassifier | ||
from recommendation.search.classification.embedly import (FaviconClassifier, | ||
KeyImageClassifier) | ||
from recommendation.search.classification.logo import LogoClassifier | ||
from recommendation.search.classification.wikipedia import WikipediaClassifier | ||
|
||
|
||
CLASSIFIERS = [ | ||
DomainClassifier, | ||
FaviconClassifier, | ||
KeyImageClassifier, | ||
LogoClassifier, | ||
WikipediaClassifier, | ||
] |
This file was deleted.
Oops, something went wrong.
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,27 @@ | ||
import requests | ||
|
||
from recommendation.search.classification.base import BaseClassifier | ||
|
||
|
||
class LogoClassifier(BaseClassifier): | ||
""" | ||
Classifier that is applied if the returned result is the root address of a | ||
domain name. Returns a URL to the logo or None. | ||
""" | ||
type = 'logo' | ||
|
||
def _get_logo(self): | ||
return 'https://logo.clearbit.com/%s' % self.url.netloc | ||
|
||
def _logo_exists(self, url): | ||
response = requests.get(url) | ||
return response.status_code < 400 | ||
|
||
def is_match(self, result): | ||
return self.url.path in ['/', ''] | ||
|
||
def enhance(self): | ||
logo = self._get_logo() | ||
if not self._logo_exists(logo): | ||
return None | ||
return logo |
This file was deleted.
Oops, something went wrong.
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,71 @@ | ||
from unittest import TestCase | ||
from urllib.parse import ParseResult | ||
|
||
import responses | ||
from mock import patch | ||
from nose.tools import eq_, ok_ | ||
|
||
from recommendation.search.classification.logo import LogoClassifier | ||
|
||
|
||
DOMAIN = 'www.mozilla.com' | ||
URL = 'http://%s/' % DOMAIN | ||
LOGO = 'https://logo.clearbit.com/%s' % DOMAIN | ||
|
||
|
||
class TestLogoClassifier(TestCase): | ||
def _result(self, url): | ||
return { | ||
'url': url | ||
} | ||
|
||
def _classifier(self, url): | ||
return LogoClassifier(self._result(url)) | ||
|
||
def _matches(self, url): | ||
return self._classifier(url).is_match(self._result(url)) | ||
|
||
def _enhance(self, url): | ||
return self._classifier(url).enhance() | ||
|
||
def test_get_logo(self): | ||
eq_(self._classifier(URL)._get_logo(), LOGO) | ||
|
||
@responses.activate | ||
def test_logo_exists_ok(self): | ||
responses.add(responses.GET, LOGO, status=200) | ||
eq_(self._classifier(URL)._logo_exists(LOGO), True) | ||
|
||
@responses.activate | ||
def test_logo_exists_invalid(self): | ||
responses.add(responses.GET, LOGO, status=400) | ||
eq_(self._classifier(URL)._logo_exists(LOGO), False) | ||
|
||
@responses.activate | ||
def test_logo_exists_missing(self): | ||
responses.add(responses.GET, LOGO, status=404) | ||
eq_(self._classifier(URL)._logo_exists(LOGO), False) | ||
|
||
@responses.activate | ||
def test_init(self): | ||
responses.add(responses.GET, LOGO, status=200) | ||
instance = self._classifier(URL) | ||
ok_(instance.result) | ||
eq_(instance.result['url'], URL) | ||
ok_(isinstance(instance.url, ParseResult)) | ||
ok_(isinstance(instance.is_match(instance.result), bool)) | ||
ok_(isinstance(instance.enhance(), str)) | ||
|
||
def test_is_match(self): | ||
eq_(self._matches('http://%s' % DOMAIN), True) | ||
eq_(self._matches('http://%s/' % DOMAIN), True) | ||
eq_(self._matches('http://%s/en_US' % DOMAIN), False) | ||
eq_(self._matches('http://%s/en_US/' % DOMAIN), False) | ||
|
||
@patch(('recommendation.search.classification.logo.LogoClassifier.' | ||
'_logo_exists')) | ||
def test_enhance(self, mock_logo_exists): | ||
mock_logo_exists.return_value = False | ||
eq_(self._enhance(URL), None) | ||
mock_logo_exists.return_value = True | ||
eq_(self._enhance(URL), LOGO) |
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