Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
52 lines (42 loc) · 1.71 KB

File metadata and controls

52 lines (42 loc) · 1.71 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
52
from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database
from sqlalchemy import MetaData, Table, Column, Integer, String
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlalchemy.sql import exists
base = declarative_base()
class New(base):
__tablename__ = "news"
id = Column(Integer(), primary_key=True)
time = Column(String(), nullable=False)
title = Column(String(), nullable=False)
text = Column(String(), nullable=False)
class Database:
def __init__(self):
self.url = f"postgresql://postgres:1@localhost:5432/news_database"
self.engine = create_engine(self.url, echo=True)
def create(self):
if not database_exists(self.url):
create_database(self.url)
metadata = MetaData()
news_table = Table('news', metadata,
Column('id', Integer(), primary_key=True),
Column('time', String(), nullable=False),
Column('title', String(), nullable=False),
Column('text', String(), nullable=False)
)
metadata.create_all(self.engine)
def add(self, news_list):
self.create()
Session = sessionmaker(bind=self.engine)
session = Session()
for new in news_list:
if session.query(exists().where(New.title == new.title)).scalar() is False:
session.add(new)
session.commit()
session.close()
def get(self, new_id):
Session = sessionmaker(bind=self.engine)
session = Session()
res = session.query(New).filter_by(id=new_id).first()
session.close()
return res
Morty Proxy This is a proxified and sanitized view of the page, visit original site.