player.py 4.1 KB

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