ADD requirements.txt & Database class
This commit is contained in:
@@ -8,6 +8,7 @@ class RedditConnector(BaseConnector):
|
|||||||
self.url = "https://www.reddit.com/"
|
self.url = "https://www.reddit.com/"
|
||||||
self.source_name = "Reddit"
|
self.source_name = "Reddit"
|
||||||
|
|
||||||
|
# Public Methods #
|
||||||
def get_top_posts(self, limit: int = 10, timeframe: str = 'day') -> list[Post]:
|
def get_top_posts(self, limit: int = 10, timeframe: str = 'day') -> list[Post]:
|
||||||
params = {
|
params = {
|
||||||
'limit': limit,
|
'limit': limit,
|
||||||
@@ -52,6 +53,7 @@ class RedditConnector(BaseConnector):
|
|||||||
data = self._fetch_data(url, params)
|
data = self._fetch_data(url, params)
|
||||||
return self._parse_posts(data)
|
return self._parse_posts(data)
|
||||||
|
|
||||||
|
## Private Methods ##
|
||||||
def _parse_posts(self, data) -> list[Post]:
|
def _parse_posts(self, data) -> list[Post]:
|
||||||
posts = []
|
posts = []
|
||||||
for item in data['data']['children']:
|
for item in data['data']['children']:
|
||||||
|
|||||||
26
db/database.py
Normal file
26
db/database.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# To connect to PostgreSQL database
|
||||||
|
import psycopg2
|
||||||
|
from psycopg2 import sql
|
||||||
|
from psycopg2.extras import RealDictCursor
|
||||||
|
from typing import List, Dict, Any, Optional
|
||||||
|
|
||||||
|
class Database:
|
||||||
|
def __init__(self, db_name: str, user: str, password: str, host: str = 'localhost', port: int = 5432):
|
||||||
|
self.connection = psycopg2.connect(
|
||||||
|
dbname=db_name,
|
||||||
|
user=user,
|
||||||
|
password=password,
|
||||||
|
host=host,
|
||||||
|
port=port
|
||||||
|
)
|
||||||
|
self.connection.autocommit = True
|
||||||
|
|
||||||
|
def execute_query(self, query: str, params: Optional[tuple] = None):
|
||||||
|
with self.connection.cursor(cursor_factory=RealDictCursor) as cursor:
|
||||||
|
cursor.execute(query, params)
|
||||||
|
if cursor.description:
|
||||||
|
return cursor.fetchall()
|
||||||
|
return []
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.connection.close()
|
||||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
psycopg2==2.9.11
|
||||||
|
psycopg2-binary==2.9.11
|
||||||
Reference in New Issue
Block a user