from dataclasses import dataclass, field from typing import List, Dict from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from datetime import datetime import pandas as pd from tinydb import Query import re from config import Config from logger import logger from db import PlayerDB @dataclass class Player(): url:str = 'https://play.boobylegends.com/player/' driver:webdriver = None Cards:List[dict] = field(default_factory=list) asOf:datetime = datetime.utcnow() df:pd.DataFrame = field(default_factory=pd.DataFrame) rank:int = 0 score:int = 0 def fetchData(self): logger.debug("Fetch Player Data.") self.loadPage() self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") userInfo=self.driver.find_element(By.CLASS_NAME, 'user-infos') self.score=int(userInfo.find_element(By.CLASS_NAME,'my-score').text) regex=re.compile('[^0-9]') self.rank=int(regex.sub('',userInfo.find_element(By.TAG_NAME,'span').text)) collection=self.driver.find_element(By.CLASS_NAME, 'container-collection') self.Cards=[] cards=collection.find_elements(By.CLASS_NAME, 'wrapper-card') for card in cards: try: Card={} Card['card-num']=int(card.find_element(By.CLASS_NAME,'pornstar-number').text) Card['name']=card.find_element(By.TAG_NAME,'h2').text Card['card-id'] = int(card.get_attribute('card-id')) Card['phid'] = card.get_attribute('phid') Card['level'] = card.get_attribute('level') tierCard = card.find_element(By.CLASS_NAME,'tier-card') tierLink = tierCard.get_attribute('src') Card['tier'] = tierLink[:-4][-1].upper() Card['link'] = card.get_attribute('href') try: Card['stock']=card.find_element(By.CLASS_NAME,'card-nb').text except: Card['stock']="0" self.Cards.append(Card) except Exception as e: logger.error(f"{e}\n{card.text}") self.loadDF() def initPage(self): logger.debug("Init Player Page.") self.driver = webdriver.Chrome() self.driver.get(self.url) self.driver.add_cookie(Config["cookie"]) def loadPage(self): logger.debug("Load Player Page.") if self.driver is None: self.initPage() self.driver.get(self.url) self.asOf=datetime.utcnow() def closePage(self): logger.debug("Close Player Page.") self.driver.close() def loadDF(self): logger.debug("Load Player pandas DF.") self.df=pd.DataFrame(self.Cards) self.df.sort_values(by='card-num', ascending=True, inplace=True) def show(self,fields=['card-num','name','level','stock'],filter=''): logger.debug("Show Player Data.") print("Rank: {Rank}\tScore: {Score}\n".format(Rank=self.rank,Score=self.score)) if filter != '': pdf=self.df.query(filter) else: pdf=self.df print(pdf[fields].to_string(index=False)) def commit(self): logger.debug("Player Commit.") today=self.asOf.strftime('%Y-%m-%d') for Card in self.Cards: thisCard={} thisCard['date'] = today thisCard['card-num'] = Card['card-num'] thisCard['name'] = Card['name'] thisCard['level'] = Card['level'] thisCard['tier'] = Card['tier'] thisCard['stock'] = Card['stock'] thisCard['card-id'] = Card['card-id'] thisCard['phid'] = Card['phid'] thisCard['link'] = Card['link'] PlayerDB.upsert(thisCard,Query().fragment({'date':today,'card-num':Card['card-num']}))