Example #1
0
    def __init__(self, sources):
        self.news_map = {}
        self.readed_news = {}
        self.full_news_list = []
        self.sources = sources
        self.logger = logging.getLogger(self.__class__.__name__)

        self.db_session = Session()
        self.last_update_time = 0
Example #2
0
    def add_user(username: str, password: str) -> User:
        # with Session as session:
        session = Session()

        username = username.lower()
        user = User(username, password)
        session.add(user)

        session.commit()
        session.close()
        return UserService.get_user(username=username)
Example #3
0
    def add_max(title: str, value: str) -> Maximum:
        # with Session as session:
        session = Session()

        maximum = Maximum(title, value)

        session.add(maximum)

        session.commit()
        session.close()
        return Maximum.get_max(title=title)
Example #4
0
 def update_games(user_id: int, **kwargs) -> User:
     session = Session()
     user = session.query(User).filter(User.id.is_(user_id)).first()
     for key, value in kwargs.items():
         setattr(user, key, value)
     session.commit()
     session.close()
     return user
Example #5
0
    def update_maximum(
        value: str,
        id: int = None,
        title: str = None,
    ) -> Maximum:
        session = Session()

        maximum: Maximum = None

        if id:
            maximum = session.query(Maximum).filter(Maximum.id.is_(id)).first()
        elif title:
            maximum = session.query(Maximum).filter(
                Maximum.title.is_(title)).first()

        if maximum:
            maximum.value = value
            session.commit()
        session.close()
Example #6
0
    def get_user(id: int = None, username: str = None) -> User:
        session = Session()

        user: User = None

        if id:
            user = session.query(User).filter(User.id.is_(id)).first()
        elif username:
            username = username.lower()
            user = session.query(User).filter(User.username.is_(username)).first()

        session.close()
        return user
Example #7
0
    def get_max(id: int = None, title: str = None) -> Maximum:
        session = Session()

        maximum: Maximum = None

        if id:
            maximum = session.query(Maximum).filter(Maximum.id.is_(id)).first()
        elif title:
            maximum = session.query(Maximum).filter(
                Maximum.title.is_(title)).first()

        session.close()
        return maximum
Example #8
0
class StreamingNews:
    def __init__(self, sources):
        self.news_map = {}
        self.readed_news = {}
        self.full_news_list = []
        self.sources = sources
        self.logger = logging.getLogger(self.__class__.__name__)

        self.db_session = Session()
        self.last_update_time = 0

    def _is_db_cached(self, link):
        news_entries = self.db_session.query(NewsLink) \
            .filter_by(link=link).one_or_none()
        return news_entries is not None

    def _cache_2_db(self, news_list):
        if not news_list:
            return

        for news in news_list:
            db_entry = NewsLink(link=news.link, ts=news.ts)
            self.db_session.add(db_entry)

        self.db_session.commit()
        self.logger.info('Commited to database')

    def _print_history(self):
        self.logger.info('--- History ---')
        history_entries = self.db_session.query(ReadHistory).all()
        for history in history_entries:
            self.logger.info('{}'.format(history))

    def _print_news_table(self):
        self.logger.info('--- News Links ---')
        news_entries = self.db_session.query(NewsLink).all()
        for news in news_entries:
            self.logger.info('{}'.format(news))

    def _commit_2_history_db(self, news_db_entry, user_id):
        history_db_entry = ReadHistory(link_id=news_db_entry.id,
                                       user_id=user_id)
        self.db_session.add(history_db_entry)
        self.db_session.commit()

        self.logger.info('Commited to history database')

    def _get_last_fresh_news(self, user_id):
        subquery = self.db_session.query(
            ReadHistory.link_id).filter(ReadHistory.user_id == user_id).all()
        new_history_entries = self.db_session.query(NewsLink) \
            .filter(~NewsLink.id.in_(subquery)) \
            .order_by(NewsLink.ts.desc()) \
            .first()

        return new_history_entries

    def _is_in_history(self, link, id_):
        if id_ not in self.readed_news:
            return False

        if link not in self.readed_news[id_]:
            return False

        return True

    def _update_last_news(self):
        news_2_cache = []

        self.logger.info('Called _update_last_news()')

        links_2_get_info = {}

        sources_workers = min(len(self.sources), 10)
        with concurrent.futures.ThreadPoolExecutor(
                max_workers=sources_workers) as executor:
            futures_2_source = {
                executor.submit(source.get_last_news): source
                for source in self.sources
            }

            for future in concurrent.futures.as_completed(futures_2_source):
                source = futures_2_source[future]
                news_records = future.result()
                for news_rec in news_records:
                    if self._is_db_cached(news_rec.link):
                        continue

                    if news_rec.ts < 0:
                        links_2_get_info[news_rec.link] = source
                    else:
                        news_2_cache.append(news_rec)

        # Concurrently receive times
        attributes_workers = min(len(links_2_get_info), 10)
        if attributes_workers > 0:
            with concurrent.futures.ThreadPoolExecutor(
                    max_workers=attributes_workers) as executor:
                future_to_url = {
                    executor.submit(source.get_time, link): link
                    for link, source in links_2_get_info.items()
                }

                for future in concurrent.futures.as_completed(future_to_url):
                    link = future_to_url[future]
                    ts = future.result()

                    news_rec = NewsRecord(link=link, ts=ts)
                    self.logger.info('Received: {}'.format(news_rec))

                    news_2_cache.append(news_rec)

            self._cache_2_db(news_2_cache)

    def get_last_fresh_news(self, user_id):
        if time.time() - self.last_update_time > 5 * 60:
            self._update_last_news()
            self.last_update_time = time.time()

        fresh_news = self._get_last_fresh_news(user_id)
        if fresh_news is None:
            return None

        self._commit_2_history_db(fresh_news, user_id)

        return fresh_news.link
Example #9
0
from datetime import datetime
from src.models.countries import Country
from src.models.users import User
from src.models.transactions import Transaction
from src.models.tiles import Tile
from src.models.transaction_details import TransactionDetail
from db_base import Base, Session, engine

Session.configure(bind=engine)
#---------Create DB-----------------
Base.metadata.create_all(engine)
#--------Create a session-----------

#-------Initialize models-----------

#create a few countries
usa = Country("United States of America", 0, "US", 1.0)
mexico = Country("Mexico", 0, "MEX", 1.0)
canada = Country("Canada", 0, "CA", 1.0)

Session.add(usa)
Session.add(mexico)
Session.add(canada)
Session.commit()

#create a few users
user1 = User("test1", "*****@*****.**", "test123", 1, "Test", "Testovic",
             "+38762000000", 1, 1, "Testoo", usa.id)
user2 = User("test2", "*****@*****.**", "test123", 2, "Teston", "Testony",
             "+38565123123", 1, 1, "Testorony", canada.id)
Example #10
0
import getpass
import hashlib
from db_base import Session, User, Goods, VipUser

session = Session()

# *****************************************#
# ************提示信息**********************#
# ******************************************#


def show_menu(type):
    main_menu = """
********欢迎使用wjy系统**********
0) 销售系统\n1) 会员系统\n2) 商品系统\n3) 退出\n请选择: """

    vip_menu = """
************会员系统************
0) 新增\n1) 修改\n2) 查询\n3) 返回\n请选择: """

    goods_menu = """
************商品系统************
0) 新增\n1) 修改\n2) 查询\n3) 返回\n请选择: """
    all_info = {'main': main_menu, 'goods': goods_menu, 'vip': vip_menu}

    return all_info[type]


def show_goods_info():
    c = 1
    nums = []