Skip to content

Commit

Permalink
added errors page and routes, moved html parser, fixed db.py app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey-Barinov committed Apr 25, 2024
1 parent e9f94bb commit 6504560
Show file tree
Hide file tree
Showing 9 changed files with 411 additions and 33 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ build:
lint:
poetry run flake8 page_analyzer

test:
poetry run pytest

selfcheck:
poetry check

Expand Down
24 changes: 14 additions & 10 deletions page_analyzer/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import requests
from dotenv import load_dotenv
from page_analyzer.url_validator import validate
from urllib.parse import urlparse
from bs4 import BeautifulSoup
from page_analyzer.url_validator import validate
from page_analyzer.html_parser import parse_page
from flask import (
Flask,
render_template,
Expand All @@ -28,6 +28,16 @@
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')


@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404


@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500


@app.get('/')
def page_analyzer():
message = get_flashed_messages(with_categories=True)
Expand Down Expand Up @@ -102,15 +112,9 @@ def add_check(id):

status_code = response.status_code

html_data = BeautifulSoup(response.text, 'html.parser')

title = html_data.title.string if html_data.title else None
h1 = html_data.h1.string if html_data.h1 else None
description = html_data.find('meta', {'name': 'description'})
if description:
description = description.get('content')
page_data = parse_page(response.text)

add_check_to_db(id, status_code, h1, title, description)
add_check_to_db(id, status_code, page_data)

flash('Страница успешно проверена', 'success')

Expand Down
39 changes: 22 additions & 17 deletions page_analyzer/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_url_by_id(url_id):
return url_data


def add_check_to_db(url_id, status_code, h1, title, description):
def add_check_to_db(url_id, status_code, page_data):
conn = get_connection()

with conn.cursor() as cur:
Expand All @@ -63,21 +63,26 @@ def add_check_to_db(url_id, status_code, h1, title, description):
"description "
") "
"VALUES (%s, %s, %s, %s, %s)",
(url_id, status_code, h1, title, description)
(url_id,
status_code,
page_data['h1'],
page_data['title'],
page_data['description']
)
)
conn.commit()
conn.close()


def get_urls_with_latest_check():
conn = get_connection()
query = "SELECT urls.id, "\
"urls.name, "\
"COALESCE(url_checks.status_code::text, '') as status_code, "\
"COALESCE(MAX(url_checks.created_at)::text, '') as latest_check "\
"FROM urls "\
"LEFT JOIN url_checks ON urls.id = url_checks.url_id "\
"GROUP BY urls.id, url_checks.status_code "\
query = "SELECT urls.id, " \
"urls.name, " \
"COALESCE(url_checks.status_code::text, '') as status_code, " \
"COALESCE(MAX(url_checks.created_at)::text, '') as latest_check " \
"FROM urls " \
"LEFT JOIN url_checks ON urls.id = url_checks.url_id " \
"GROUP BY urls.id, url_checks.status_code " \
"ORDER BY urls.id DESC"

all_urls_with_latest_check = fetch_all(conn, query)
Expand All @@ -87,14 +92,14 @@ def get_urls_with_latest_check():

def get_checks_desc(url_id):
conn = psycopg2.connect(DATABASE_URL)
query = "SELECT id, "\
"status_code, "\
"COALESCE(h1, '') as h1, "\
"COALESCE(title, '') as title, "\
"COALESCE(description, '') as description, "\
"created_at::text "\
"FROM url_checks "\
"WHERE url_id = %s "\
query = "SELECT id, " \
"status_code, " \
"COALESCE(h1, '') as h1, " \
"COALESCE(title, '') as title, " \
"COALESCE(description, '') as description, " \
"created_at::text " \
"FROM url_checks " \
"WHERE url_id = %s " \
"ORDER BY id DESC"
value = (url_id,)

Expand Down
15 changes: 15 additions & 0 deletions page_analyzer/html_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from bs4 import BeautifulSoup


def parse_page(response_text):
html_data = BeautifulSoup(response_text, 'html.parser')
page_data = {'title': html_data.title.string if html_data.title else None,
'h1': html_data.h1.string if html_data.h1 else None}

description = html_data.find('meta', {'name': 'description'})
if description:
description = description.get('content')

page_data['description'] = description

return page_data
7 changes: 7 additions & 0 deletions page_analyzer/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "layout.html" %}
{% block content %}
<h1>Страница не найдена</h1>
<p>Проверьте корректность адреса</p>
<a href="{ url_for('page_analyzer') }">Вернуться на главную</a>
{% endblock %}

6 changes: 6 additions & 0 deletions page_analyzer/templates/500.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "layout.html" %}
{% block content %}
<h1>Ошибка сервера</h1>
<p>Извините, попробуте выполнить ваш запрос позже</p>
<a href="{ url_for('page_analyzer') }">Вернуться на главную</a>
{% endblock %}
2 changes: 0 additions & 2 deletions page_analyzer/url_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ def validate(url):
return 'URL превышает 255 символов'
elif validators.url(url) is not True:
return 'Некорректный URL'
else:
return ''
Loading

0 comments on commit 6504560

Please sign in to comment.