win scraping
- will scape won games at start to update db for giveaways the bot has entered - schedules a once a day run of scraping won giveaways
This commit is contained in:
parent
de2379587e
commit
48c030f445
10 changed files with 200 additions and 13 deletions
|
@ -11,7 +11,7 @@ from sqlalchemy.orm import Session, joinedload
|
|||
from sqlalchemy_utils import database_exists
|
||||
|
||||
from .log import get_logger
|
||||
from .models import TableNotification, TableGiveaway, TableSteamItem, TableTask
|
||||
from .models import TableNotification, TableGiveaway, TableSteamItem
|
||||
|
||||
logger = get_logger(__name__)
|
||||
engine = sqlalchemy.create_engine(f"{os.getenv('BOT_DB_URL', 'sqlite:///./config/sqlite.db')}", echo=False)
|
||||
|
@ -116,6 +116,16 @@ class GiveawayHelper:
|
|||
with Session(engine) as session:
|
||||
return session.query(TableGiveaway).filter_by(entered=True).count()
|
||||
|
||||
@classmethod
|
||||
def total_won(cls):
|
||||
with Session(engine) as session:
|
||||
return session.query(TableGiveaway).filter_by(won=True).count()
|
||||
|
||||
@classmethod
|
||||
def get_by_giveaway_id(cls, game_id):
|
||||
with Session(engine) as session:
|
||||
return session.query(TableGiveaway).filter_by(giveaway_id=game_id).one_or_none()
|
||||
|
||||
@classmethod
|
||||
def unix_timestamp_to_utc_datetime(cls, timestamp):
|
||||
return datetime.utcfromtimestamp(timestamp)
|
||||
|
@ -126,6 +136,17 @@ class GiveawayHelper:
|
|||
return session.query(TableGiveaway).filter_by(giveaway_id=giveaway.giveaway_game_id,
|
||||
steam_id=giveaway.steam_app_id).all()
|
||||
|
||||
@classmethod
|
||||
def mark_game_as_won(cls, game_id):
|
||||
with Session(engine) as session:
|
||||
result = session.query(TableGiveaway).filter_by(giveaway_id=game_id).all()
|
||||
if result:
|
||||
won_giveaway = result[0]
|
||||
if not won_giveaway.won:
|
||||
won_giveaway.won = True
|
||||
session.add(won_giveaway)
|
||||
session.commit()
|
||||
|
||||
@classmethod
|
||||
def insert(cls, giveaway, entered, won):
|
||||
with Session(engine) as session:
|
||||
|
@ -149,7 +170,7 @@ class GiveawayHelper:
|
|||
giveaway_ended_at=GiveawayHelper.unix_timestamp_to_utc_datetime(giveaway.time_remaining_timestamp),
|
||||
cost=giveaway.cost,
|
||||
copies=giveaway.copies,
|
||||
contributor_level=giveaway._contributor_level,
|
||||
contributor_level=giveaway.contributor_level,
|
||||
entered=entered,
|
||||
won=won,
|
||||
game_entries=giveaway.game_entries)
|
||||
|
@ -157,7 +178,7 @@ class GiveawayHelper:
|
|||
session.commit()
|
||||
|
||||
@classmethod
|
||||
def upsert_giveaway(cls, giveaway, entered, won):
|
||||
def upsert_giveaway_with_details(cls, giveaway, entered, won):
|
||||
result = GiveawayHelper.get_by_ids(giveaway)
|
||||
if not result:
|
||||
GiveawayHelper.insert(giveaway, entered, won)
|
||||
|
@ -172,7 +193,7 @@ class GiveawayHelper:
|
|||
giveaway_ended_at=GiveawayHelper.unix_timestamp_to_utc_datetime(giveaway.time_remaining_timestamp),
|
||||
cost=giveaway.cost,
|
||||
copies=giveaway.copies,
|
||||
contributor_level=giveaway._contributor_level,
|
||||
contributor_level=giveaway.contributor_level,
|
||||
entered=entered,
|
||||
won=won,
|
||||
game_entries=giveaway.game_entries)
|
||||
|
@ -195,7 +216,7 @@ class GiveawayHelper:
|
|||
giveaway_ended_at=GiveawayHelper.unix_timestamp_to_utc_datetime(giveaway.time_remaining_timestamp),
|
||||
cost=giveaway.cost,
|
||||
copies=giveaway.copies,
|
||||
contributor_level=giveaway._contributor_level,
|
||||
contributor_level=giveaway.contributor_level,
|
||||
game_entries=giveaway.game_entries)
|
||||
session.merge(g)
|
||||
session.commit()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue