Skip to content

Commit

Permalink
reversed changes in db.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ReYaNOW committed Jan 14, 2024
1 parent 6787ecc commit ea75c18
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion page_analyzer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
abort,
)

from page_analyzer.db import Database
from page_analyzer.sql_requests import Database
from page_analyzer.parse_html import get_specific_tags
from page_analyzer.utils import (
validate_url,
Expand Down
57 changes: 31 additions & 26 deletions page_analyzer/db.py → page_analyzer/sql_requests.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,82 @@
import os
from collections import namedtuple

import psycopg2
from dotenv import load_dotenv
from psycopg2.extras import NamedTupleCursor

load_dotenv('.env')
DATABASE_URL = os.getenv('DATABASE_URL')

_FETCH_TYPE_VALUES = ('one', 'all')
FETCH_TYPES = namedtuple('FormatChoices', map(str.upper, _FETCH_TYPE_VALUES))(
*_FETCH_TYPE_VALUES
)


class Database:
def __init__(self, connect=False):
if connect:
print(DATABASE_URL)
self.conn = psycopg2.connect(DATABASE_URL)
else:
raise ConnectionError('Need to connect to db before execute query')
self.conn = None

def _make_request(self, request, params=None, fetch_type=None):
if self.conn is None:
raise ConnectionError(
'Need to connect to db before making requests'
)

def _execute_query(self, request, params=None, fetch_type=None):
with self.conn.cursor(cursor_factory=NamedTupleCursor) as curs:
curs.execute(request, params)

match fetch_type:
case FETCH_TYPES.ALL:
case 'all':
data = curs.fetchall()
case FETCH_TYPES.ONE:
case 'one':
data = curs.fetchone()
case _:
data = []
data = None
return data

def _execute_query_with_fetch(self, request, params=None, fetch_type=None):
pass

def get_all_urls(self):
return self._execute_query(
return self._make_request(
'SELECT * FROM urls ORDER BY id DESC', fetch_type='all'
)

def get_url_by_name(self, url):
return self._execute_query(
return self._make_request(
'SELECT * FROM urls WHERE name=%s', [url], fetch_type='one'
)

def get_url_by_id(self, id_):
return self._execute_query(
return self._make_request(
'SELECT * FROM urls WHERE id=%s', [id_], fetch_type='one'
)

def add_new_url(self, url):
added_url = self._execute_query(
'INSERT INTO urls (name) VALUES (%s) RETURNING id',
self._make_request(
'INSERT INTO urls (name) VALUES (%s)',
[url],
fetch_type='one',
)
self.conn.commit()
self.commit()
added_url = self._make_request(
'SELECT * FROM urls WHERE name=%s', [url], fetch_type='one'
)
return added_url.id

def add_check(self, url_id, status_code, tags):
h1, title, desc = tags['h1'], tags['title'], tags['desc']
self._execute_query(
self._make_request(
'INSERT INTO url_checks '
'(url_id, status_code, h1, title, description) '
'VALUES (%s, %s, %s, %s, %s)',
[url_id, status_code, h1, title, desc],
)
self.conn.commit()

def get_all_checks_for_url(self, url_id):
return self._execute_query(
return self._make_request(
'SELECT * FROM url_checks WHERE url_id=%s ORDER BY id DESC',
[url_id],
fetch_type='all',
)

def get_urls_with_code(self):
return self._execute_query(
return self._make_request(
'SELECT DISTINCT ON (urls.id) urls.id, name, '
'url_checks.created_at AS last_check, '
'status_code AS last_code '
Expand All @@ -88,5 +86,12 @@ def get_urls_with_code(self):
fetch_type='all',
)

def connect(self):
self.conn = psycopg2.connect(DATABASE_URL)

def commit(self):
self.conn.commit()

def close(self):
self.conn.commit()
self.conn.close()

0 comments on commit ea75c18

Please sign in to comment.