-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.py
35 lines (30 loc) · 1.01 KB
/
database.py
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
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import sessionmaker
from models import Base, Domain
import logging
class BeastModeDB:
def __init__(self, connection_string):
self.logger = logging.getLogger(__name__)
self.session = self.new_session(connection_string)
def new_session(self, connection_string):
engine = create_engine(connection_string, echo=False)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
return Session()
def add_domains(self, domains):
self.session.add_all(domains)
self.commit()
def commit(self):
try:
self.logger.info("committing to DB")
self.session.commit()
except SQLAlchemyError as e:
session.rollback()
self.logger.error("An error occurred while inserting into database")
self.logger.error(e)
def get_new_domains(self, batch_size=None):
query = self.session.query(Domain).filter_by(registrar=None)
if batch_size is not None:
query = query.limit(batch_size)
return query.all()