Add rating for contact and social media links

This commit is contained in:
Marian Steinbach 2018-11-20 22:38:28 +01:00
parent f555781735
commit 3cb7bbbec6
3 changed files with 104 additions and 0 deletions

View file

@ -6,6 +6,7 @@ criteria based on information gather by checks before.
import logging
from rating import canonical_url
from rating import contact_link
from rating import favicon
from rating import feeds
from rating import https
@ -15,6 +16,7 @@ from rating import reachable
from rating import resolvable
from rating import response_duration
from rating import responsive_layout
from rating import social_media_links
from rating import use_specific_fonts
from rating import www_optional
@ -30,6 +32,7 @@ def calculate_rating(results):
# The raters to execute.
rating_modules = {
'CANONICAL_URL': canonical_url,
'CONTACT_LINK': contact_link,
'DNS_RESOLVABLE_IPV4': resolvable,
'FAVICON': favicon,
'FEEDS': feeds,
@ -39,6 +42,7 @@ def calculate_rating(results):
'NO_SCRIPT_ERRORS': no_script_errors,
'RESPONSIVE': responsive_layout,
'SITE_REACHABLE': reachable,
'SOCIAL_MEDIA_LINKS': social_media_links,
'USE_SPECIFIC_FONTS': use_specific_fonts,
'WWW_OPTIONAL': www_optional,
}

41
rating/contact_link.py Normal file
View file

@ -0,0 +1,41 @@
"""
Checks whether the pages has a link "Kontakt"
"""
from rating.abstract_rater import AbstractRater
class Rater(AbstractRater):
rating_type = 'boolean'
default_value = False
depends_on_checks = ['hyperlinks']
max_score = 1
def __init__(self, check_results):
super().__init__(check_results)
def rate(self):
value = self.default_value
score = 0
urls = 0
urls_with_contact_link = 0
for url in self.check_results['hyperlinks']:
urls += 1
for link in self.check_results['hyperlinks'][url]['links']:
if link['text'].lower() == 'kontakt':
urls_with_contact_link += 1
if urls_with_contact_link == urls:
score = self.max_score
value = True
return {
'type': self.rating_type,
'value': value,
'score': score,
'max_score': self.max_score,
}

View file

@ -0,0 +1,59 @@
"""
Checks whether the pages have a link to social media profiles.
"""
from rating.abstract_rater import AbstractRater
from urllib.parse import urlparse
import logging
class Rater(AbstractRater):
rating_type = 'boolean'
default_value = False
depends_on_checks = ['hyperlinks']
max_score = 1
def __init__(self, check_results):
super().__init__(check_results)
def rate(self):
value = self.default_value
score = 0
urls = 0
urls_with_social_media_links = 0
for url in self.check_results['hyperlinks']:
urls += 1
for link in self.check_results['hyperlinks'][url]['links']:
if link['href'] is None:
continue
# only process absolute links
if not (link['href'].startswith('http:') or link['href'].startswith('https:')):
continue
parsed = urlparse(link['href'])
if ("facebook.com" in parsed.hostname or
"twitter.com" in parsed.hostname or
"instagram.com" in parsed.hostname or
"plus.google.com" in parsed.hostname):
logging.debug("Found social media link on %s: %s" % (url, link['href']))
urls_with_social_media_links += 1
# make sure we only count 1 for this url
break
if urls_with_social_media_links == urls:
score = self.max_score
value = True
return {
'type': self.rating_type,
'value': value,
'score': score,
'max_score': self.max_score,
}