-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
51 lines (45 loc) · 1.21 KB
/
db.py
File metadata and controls
51 lines (45 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from config import Config
from sqlalchemy.orm import sessionmaker
from sqlalchemy import (
Table,
Column,
Integer,
MetaData,
DateTime,
create_engine,
String,
Text,
)
metadata = MetaData()
subreddit_posts = Table(
"subreddit_posts",
metadata,
Column("id", Integer, primary_key=True),
Column("post_id", String),
Column("post_title", Text),
Column("post_url", String),
Column("subreddit_name", String),
Column("post_datetime", DateTime),
)
engine = create_engine(Config.SQLALCHEMY_DATABASE_URI)
connection = engine.connect()
Session = sessionmaker(bind=engine)
session = Session()
def does_post_exist(post_id: str):
cursor = connection.execute(f"SELECT post_id from subreddit_posts where post_id = '{post_id}'")
result = cursor.fetchone()
if result:
return True
else:
return False
async def insert_post_to_db(submission):
ins = subreddit_posts.insert().values(
post_id=submission.id,
post_title=submission.title,
post_url=submission.url,
subreddit_name=submission.subreddit.display_name,
)
try:
connection.execute(ins)
except Exception as e:
print(f"Failed: {e} \nQ:{ins}")