Skip to content

Commit 827c3a8

Browse files
committed
data_base.py parser.py url_validator.py
1 parent 6a265a0 commit 827c3a8

File tree

3 files changed

+15
-51
lines changed

3 files changed

+15
-51
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
.venv
33
.idea
44

5-
instance
5+
page_analyzer/instance
66
page_analyzer/__pycache__/
77
__pycache__

instance/urls.db

Whitespace-only changes.

page_analyzer/app.py

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
import os
22
from flask import Flask, render_template, request, redirect, flash, url_for
33
import requests
4-
import psycopg2
5-
from dotenv import load_dotenv
6-
import validators
74
from bs4 import BeautifulSoup
85
from urllib.parse import urlparse
6+
from page_analyzer.data_base import save_url, get_existing_urls, get_db_connection
7+
from page_analyzer.parser import parse_url
8+
from page_analyzer.url_validator import validate_url
99

10-
load_dotenv()
11-
DATABASE_URL = os.getenv('DATABASE_URL')
1210

1311
app = Flask(__name__)
1412
app.secret_key = os.getenv('SECRET_KEY')
1513

16-
17-
def get_db_connection():
18-
conn = psycopg2.connect(DATABASE_URL)
19-
return conn
20-
21-
2214
@app.route('/', methods=['GET'])
2315
def index():
2416
return render_template('index.html')
@@ -27,48 +19,20 @@ def index():
2719
@app.route('/urls', methods=['POST'])
2820
def create_url():
2921
url_input = request.form.get('url')
30-
error_message = None
31-
32-
# Валидация URL
33-
if not url_input or len(url_input) > 255:
34-
error_message = "Некорректный URL или превышена длина 255 символов."
35-
elif not validators.url(url_input):
36-
error_message = "Некорректный URL."
37-
22+
error_message = validate_url(url_input)
3823
if error_message:
3924
flash(error_message, 'error')
4025
return render_template('index.html', url=url_input), 422
41-
42-
# Нормализация URL
43-
normalized_url = url_input.strip()
44-
parsed_input_url = urlparse(normalized_url)
45-
base_input_domain = f"{parsed_input_url.scheme}://{parsed_input_url.netloc}"
46-
47-
# Проверка на наличие URL в базе данных
48-
with get_db_connection() as conn:
49-
with conn.cursor() as cur:
50-
cur.execute('SELECT id, name FROM urls')
51-
existing_urls = cur.fetchall()
52-
53-
# Проверяем, если в базе данных есть URL
54-
if existing_urls:
55-
for existing_url in existing_urls:
56-
if len(existing_url) > 1:
57-
parsed_existing_url = urlparse(existing_url[1])
58-
base_existing_domain = f"{parsed_existing_url.scheme}://{parsed_existing_url.netloc}"
59-
60-
if base_input_domain == base_existing_domain:
61-
flash('Страница уже существует', 'error')
62-
return redirect(url_for('url_detail',
63-
url_id=existing_url[0]))
64-
65-
cur.execute('INSERT INTO urls (name) '
66-
'VALUES (%s) RETURNING id',
67-
(normalized_url,))
68-
url_id = cur.fetchone()[0]
69-
conn.commit()
70-
flash('Страница успешно добавлена', 'success')
71-
return redirect(url_for('url_detail', url_id=url_id))
26+
base_input_domain = parse_url(url_input)
27+
existing_urls = get_existing_urls()
28+
if existing_urls:
29+
for existing_url in existing_urls:
30+
if base_input_domain == parse_url(existing_url[1]):
31+
flash('Страница уже существует', 'error')
32+
return redirect(url_for('url_detail', url_id=existing_url[0]))
33+
url_id = save_url(url_input)
34+
flash('Страница успешно добавлена', 'success')
35+
return redirect(url_for('url_detail', url_id=url_id))
7236

7337

7438
@app.route('/urls', methods=['GET'])

0 commit comments

Comments
 (0)