player.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from dataclasses import dataclass, field
  2. from typing import List, Dict
  3. from selenium import webdriver
  4. from selenium.webdriver.common.keys import Keys
  5. from selenium.webdriver.common.by import By
  6. from datetime import datetime
  7. import pandas as pd
  8. from tinydb import Query
  9. import re
  10. from config import Config
  11. from logger import logger
  12. from db import PlayerDB
  13. @dataclass
  14. class Player():
  15. url:str = 'https://play.boobylegends.com/player/'
  16. driver:webdriver = None
  17. Cards:List[dict] = field(default_factory=list)
  18. asOf:datetime = datetime.utcnow()
  19. df:pd.DataFrame = field(default_factory=pd.DataFrame)
  20. rank:int = 0
  21. score:int = 0
  22. def fetchData(self):
  23. logger.debug("Fetch Player Data.")
  24. self.loadPage()
  25. self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  26. userInfo=self.driver.find_element(By.CLASS_NAME, 'user-infos')
  27. self.score=int(userInfo.find_element(By.CLASS_NAME,'my-score').text)
  28. regex=re.compile('[^0-9]')
  29. self.rank=int(regex.sub('',userInfo.find_element(By.TAG_NAME,'span').text))
  30. collection=self.driver.find_element(By.CLASS_NAME, 'container-collection')
  31. self.Cards=[]
  32. cards=collection.find_elements(By.CLASS_NAME, 'wrapper-card')
  33. for card in cards:
  34. try:
  35. Card={}
  36. Card['card-num']=int(card.find_element(By.CLASS_NAME,'pornstar-number').text)
  37. Card['name']=card.find_element(By.TAG_NAME,'h2').text
  38. Card['card-id'] = int(card.get_attribute('card-id'))
  39. Card['phid'] = card.get_attribute('phid')
  40. Card['level'] = card.get_attribute('level')
  41. tierCard = card.find_element(By.CLASS_NAME,'tier-card')
  42. tierLink = tierCard.get_attribute('src')
  43. Card['tier'] = tierLink[:-4][-1].upper()
  44. Card['link'] = card.get_attribute('href')
  45. try:
  46. Card['stock']=card.find_element(By.CLASS_NAME,'card-nb').text
  47. except:
  48. Card['stock']="0"
  49. self.Cards.append(Card)
  50. except Exception as e:
  51. logger.error(f"{e}\n{card.text}")
  52. self.loadDF()
  53. def initPage(self):
  54. logger.debug("Init Player Page.")
  55. self.driver = webdriver.Chrome()
  56. self.driver.get(self.url)
  57. self.driver.add_cookie(Config["cookie"])
  58. def loadPage(self):
  59. logger.debug("Load Player Page.")
  60. if self.driver is None: self.initPage()
  61. self.driver.get(self.url)
  62. self.asOf=datetime.utcnow()
  63. def closePage(self):
  64. logger.debug("Close Player Page.")
  65. self.driver.close()
  66. def loadDF(self):
  67. logger.debug("Load Player pandas DF.")
  68. self.df=pd.DataFrame(self.Cards)
  69. self.df.sort_values(by='card-num', ascending=True, inplace=True)
  70. def show(self,fields=['card-num','name','level','stock'],filter=''):
  71. logger.debug("Show Player Data.")
  72. print("Rank: {Rank}\tScore: {Score}\n".format(Rank=self.rank,Score=self.score))
  73. if filter != '':
  74. pdf=self.df.query(filter)
  75. else:
  76. pdf=self.df
  77. print(pdf[fields].to_string(index=False))
  78. def commit(self):
  79. logger.debug("Player Commit.")
  80. today=self.asOf.strftime('%Y-%m-%d')
  81. for Card in self.Cards:
  82. thisCard={}
  83. thisCard['date'] = today
  84. thisCard['card-num'] = Card['card-num']
  85. thisCard['name'] = Card['name']
  86. thisCard['level'] = Card['level']
  87. thisCard['tier'] = Card['tier']
  88. thisCard['stock'] = Card['stock']
  89. thisCard['card-id'] = Card['card-id']
  90. thisCard['phid'] = Card['phid']
  91. thisCard['link'] = Card['link']
  92. PlayerDB.upsert(thisCard,Query().fragment({'date':today,'card-num':Card['card-num']}))