Browse Source

DIV-1192: Add link checker.

pull/172/head
Steven Ly 5 years ago
parent
commit
83a67c0618
4 changed files with 85 additions and 0 deletions
  1. +28
    -0
      .github/workflows/linkcheck.yml
  2. +1
    -0
      .gitignore
  3. +54
    -0
      edivorce/apps/core/management/commands/link_check.py
  4. +2
    -0
      requirements.txt

+ 28
- 0
.github/workflows/linkcheck.yml View File

@ -0,0 +1,28 @@
name: eDivorce - Link Check
on:
schedule:
- cron: "*/2 * * * *"
jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.6]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Link Checker
run: |
python manage.py link_check

+ 1
- 0
.gitignore View File

@ -32,6 +32,7 @@ var/
.installed.cfg
*.egg
.python-version
pyvenv.cfg
# PyInstaller
# Usually these files are written by a python script from a template


+ 54
- 0
edivorce/apps/core/management/commands/link_check.py View File

@ -0,0 +1,54 @@
import os
import sys
from urllib.request import urlopen
from bs4 import BeautifulSoup
from django.conf import settings
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'Checks links in the eDivorce application.'
def _check_link(self, address):
try:
resp = urlopen(address)
if resp.status in [400, 404, 403, 408, 409, 501, 502, 503]:
return f"{resp.status} - {resp.reason}"
except Exception as e:
return f"{e}"
return None
def handle(self, *args, **options):
errors = []
for root, directory, files in os.walk(settings.BASE_DIR + '/apps/core/templates/'):
for file in files:
if '.html' in file:
file_path = os.path.join(root, file)
fs = open(file_path)
soup = BeautifulSoup(fs, 'html.parser')
links = soup.find_all('a', href=True)
for link in links:
if link is None:
continue
if link['href'].startswith('http'):
filename = str(file_path.name)
status = self._check_link(link['href'])
if status:
errors.append({
'link': link['href'],
'error': status,
'file': filename
})
if len(errors) > 0:
for error in errors:
print('-------------------------------------------------------------')
print(f'File: {error["file"]}')
print(f'link: {error["link"]}')
print(f'Error: {error["error"]}\r\n')
sys.exit(1)

+ 2
- 0
requirements.txt View File

@ -1,4 +1,5 @@
aniso8601==7.0.0
beautifulsoup4==4.9.3
certifi==2020.6.20
chardet==3.0.4
clamd==1.0.2
@ -31,6 +32,7 @@ rjsmin==1.1.0
Rx==1.6.1
singledispatch==3.4.0.3
six==1.15.0
soupsieve==2.0.1
sqlparse==0.3.1
Unidecode==1.1.1
Unipath==1.1


Loading…
Cancel
Save