-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
063a49b
commit 9937062
Showing
16 changed files
with
550 additions
and
350 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,3 +1,3 @@ | ||
#!/usr/bin/env bash | ||
|
||
make install && psql -a -d $DATABASE_URL -f page_analyzer.sql | ||
make install && psql -a -d $DATABASE_URL -f database.sql |
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,18 @@ | ||
DROP TABLE IF EXISTS url_checks; | ||
DROP TABLE IF EXISTS urls; | ||
|
||
CREATE TABLE urls ( | ||
id SERIAL PRIMARY KEY, | ||
name varchar(255) UNIQUE NOT NULL, | ||
created_at date DEFAULT CURRENT_DATE | ||
); | ||
|
||
CREATE TABLE url_checks ( | ||
id SERIAL PRIMARY KEY, | ||
url_id int, | ||
status_code int, | ||
h1 varchar(255), | ||
title varchar(255), | ||
description text, | ||
created_at date DEFAULT CURRENT_DATE | ||
) |
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 |
---|---|---|
@@ -1,105 +1,114 @@ | ||
import os | ||
import requests | ||
from dotenv import load_dotenv | ||
from urllib.parse import urlparse | ||
from page_analyzer.url_validator import validate | ||
from page_analyzer.html_parser import parse_page | ||
from flask import ( | ||
Flask, | ||
render_template, | ||
request, | ||
flash, | ||
redirect, | ||
url_for, | ||
abort | ||
redirect, | ||
flash, | ||
get_flashed_messages, | ||
) | ||
from page_analyzer.db import ( | ||
add_url_to_db, | ||
get_url_by_id, | ||
get_url_by_name, | ||
add_check_to_db, | ||
get_checks_desc, | ||
get_urls_with_latest_check | ||
) | ||
|
||
from page_analyzer.db import DbManager | ||
from page_analyzer.html_parser import HTMLParser | ||
from page_analyzer.utils import normalize_url, validate_url | ||
import os | ||
import requests | ||
from dotenv import load_dotenv | ||
|
||
load_dotenv() | ||
app = Flask(__name__) | ||
|
||
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY') | ||
app.config['DATABASE_URL'] = os.getenv('DATABASE_URL') | ||
|
||
db_manager = DbManager(app) | ||
|
||
|
||
@app.errorhandler(404) | ||
def page_not_found(error): | ||
def page_not_found(e): | ||
return render_template('errors/404.html'), 404 | ||
|
||
|
||
@app.errorhandler(500) | ||
def internal_server_error(error): | ||
def internal_server_error(e): | ||
return render_template('errors/500.html'), 500 | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
return render_template('index.html'), 200 | ||
@app.get('/') | ||
def page_analyzer(): | ||
message = get_flashed_messages(with_categories=True) | ||
|
||
return render_template('index.html', message=message) | ||
|
||
@app.post('/urls') | ||
def show_url_page(): | ||
url_check = request.form.get('url') | ||
normal_url = normalize_url(url_check) | ||
validation_error = validate_url(normal_url) | ||
|
||
if validation_error: | ||
flash(validation_error, 'danger') | ||
return render_template('index.html'), 422 | ||
@app.post('/urls') | ||
def add_url(): | ||
new_url = request.form.get('url') | ||
|
||
url_id = db_manager.get_url_by_name(normal_url) | ||
error = validate(new_url) | ||
|
||
if url_id: | ||
flash('Страница уже существует', 'warning') | ||
return redirect(url_for('get_url_list', id=url_id)) | ||
if error: | ||
flash(f'{error}', 'danger') | ||
message = get_flashed_messages(with_categories=True) | ||
return render_template('index.html', message=message), 422 | ||
|
||
url = db_manager.insert_url_in_db(normal_url) | ||
flash('Страница успешно добавлена', 'success') | ||
parsed_url = urlparse(new_url) | ||
normal_url = f"{parsed_url.scheme}://{parsed_url.netloc}" | ||
|
||
return redirect(url_for('get_url_list', id=url.id)) | ||
if get_url_by_name(normal_url): | ||
old_url_data = get_url_by_name(normal_url) | ||
flash('Страница уже существует', 'primary') | ||
|
||
return redirect(url_for('show_url', id=old_url_data[0].id)) | ||
|
||
@app.get('/urls') | ||
def urls(): | ||
all_urls = db_manager.get_urls_list() | ||
add_url_to_db(normal_url) | ||
new_url_data = get_url_by_name(normal_url) | ||
flash('Страница успешно добавлена', 'success') | ||
|
||
return render_template('details.html', urls=all_urls) | ||
return redirect(url_for('show_url', id=new_url_data[0].id)) | ||
|
||
|
||
@app.get('/urls/<id>') | ||
def get_url_list(id): | ||
url = db_manager.get_url_from_urls_list(id) | ||
@app.get('/urls') | ||
def show_all_urls(): | ||
all_urls = get_urls_with_latest_check() | ||
message = get_flashed_messages(with_categories=True) | ||
return render_template('details.html', all_urls=all_urls, message=message) | ||
|
||
if not url: | ||
abort(404) | ||
check_records = db_manager.get_url_from_urls_checks_list(id) | ||
|
||
return render_template('list.html', | ||
url=url, checks_list=check_records) | ||
@app.get('/urls/<int:id>') | ||
def show_url(id): | ||
url_data = get_url_by_id(id) | ||
all_checks = get_checks_desc(id) | ||
message = get_flashed_messages(with_categories=True) | ||
|
||
return render_template( | ||
'list.html', | ||
url_data=url_data, | ||
all_checks=all_checks, | ||
message=message | ||
) | ||
|
||
@app.post('/urls/<url_id>/check') | ||
def check_url(url_id): | ||
url_record = db_manager.get_url_from_urls_list(url_id) | ||
|
||
if not url_record: | ||
abort(404) | ||
@app.post('/urls/<id>/checks') | ||
def add_check(id): | ||
url = get_url_by_id(id) | ||
|
||
try: | ||
response = requests.get(url_record.name) | ||
response = requests.get(url[0].name) | ||
response.raise_for_status() | ||
|
||
except requests.exceptions.RequestException: | ||
flash('Произошла ошибка при проверке', 'danger') | ||
return redirect(url_for('get_url_list', id=url_id)) | ||
|
||
page_content = response.content | ||
page_parser = HTMLParser(page_content) | ||
page_data = page_parser.get_data_page() | ||
full_check = dict(page_data, url_id=url_id, response=response.status_code) | ||
return redirect(url_for('show_url', id=id)) | ||
|
||
db_manager.insert_url_check_in_db(full_check) | ||
status_code = response.status_code | ||
page_data = parse_page(response.text) | ||
add_check_to_db(id, status_code, page_data) | ||
flash('Страница успешно проверена', 'success') | ||
|
||
return redirect(url_for('get_url_list', id=url_id)) | ||
return redirect(url_for('show_url', id=id)) |
Oops, something went wrong.