market.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 json
  10. import sys
  11. from player import Player
  12. from config import Config
  13. from logger import logger
  14. from db import MarketDB
  15. from db import PlayerDB
  16. @dataclass
  17. class Market():
  18. url:str = 'https://play.boobylegends.com/market/'
  19. driver:webdriver = None
  20. Cards:List[dict] = field(default_factory=list)
  21. asOf:datetime = datetime.utcnow()
  22. df:pd.DataFrame = field(default_factory=pd.DataFrame)
  23. player:Player = None
  24. def fetchData(self):
  25. logger.debug("Market Fetching Data.")
  26. self.loadPage()
  27. self.Cards=[]
  28. cards=self.driver.find_elements(By.CLASS_NAME, 'wrapper-market-card')
  29. for card in cards:
  30. Card={}
  31. try:
  32. IDs=card.find_element(By.CLASS_NAME,'titre-card').text.split('\n')
  33. Card['card-num']=int(IDs[1])
  34. Card['name']=IDs[0]
  35. link=card.find_element(By.TAG_NAME,'a')
  36. Card['card-id'] = int(link.get_attribute('card-id'))
  37. Card['phid'] = link.get_attribute('phid')
  38. Card['level'] = link.get_attribute('level')
  39. Card['link'] = link.get_attribute('href')
  40. tierCard = card.find_element(By.CLASS_NAME,'tier-card')
  41. tierLink = tierCard.get_attribute('src')
  42. Card['tier'] = tierLink[:-4][-1].upper()
  43. Card['val']= int(card.find_element(By.CLASS_NAME,'score-card').text)
  44. Card['qty']= int(card.find_element(By.CLASS_NAME,'qty').text)
  45. Card['cost']= int(card.find_element(By.CLASS_NAME,'cost').text)
  46. self.Cards.append(Card)
  47. except Exception as e:
  48. logger.error(f"{e}\n{card.text}")
  49. self.loadDF()
  50. def initPage(self):
  51. logger.debug("Market Init Page.")
  52. self.driver = webdriver.Chrome()
  53. self.driver.get(self.url)
  54. self.driver.add_cookie(Config["cookie"])
  55. def loadPage(self):
  56. logger.debug("Market Load Page.")
  57. if self.driver is None: self.initPage()
  58. self.driver.get(self.url)
  59. self.asOf=datetime.utcnow()
  60. def closePage(self):
  61. logger.debug("Market Close Page.")
  62. self.driver.close()
  63. def getPlayerInfo(self):
  64. logger.debug("Market Get Player Info")
  65. if self.player is None: self.player = Player()
  66. self.player.fetchData()
  67. self.player.closePage()
  68. def filterMarketCards(self,force=True):
  69. logger.debug("Market Filter Cards.")
  70. clist=[None]*152
  71. if force:
  72. self.getPlayerInfo()
  73. Cards = self.player.Cards
  74. else:
  75. today=datetime.utcnow().strftime('%Y-%m-%d')
  76. Cards=PlayerDB.search(Query().fragment({'date':today}))
  77. if len(Cards)==0:
  78. self.getPlayerInfo()
  79. Cards = self.player.Cards
  80. for card in Cards: clist[card['card-num']]=card['stock']
  81. js="clist={clist}\n".format(clist=json.dumps(clist))
  82. js+="""
  83. cards=document.getElementsByClassName('wrapper-market-card')
  84. for (var i = 0; i < cards.length; i++) {
  85. cardNum = cards.item(i).getElementsByClassName('pornstar-number')[0].outerText;
  86. innerCard = cards.item(i).getElementsByClassName('inner-market-card')[0]
  87. let qty = document.createElement('div');
  88. qty.innerText = "Stock: " + clist[cardNum]
  89. innerCard.appendChild(qty)
  90. if (clist[cardNum] == 'CRYSTAL'){
  91. cards.item(i).style.setProperty('Display','None');
  92. }
  93. }
  94. """
  95. print(js)
  96. self.driver.execute_script(js)
  97. def loadDF(self):
  98. logger.debug("Market Load DF.")
  99. self.df=pd.DataFrame(self.Cards)
  100. self.df.sort_values(by='card-num', ascending=True, inplace=True)
  101. def show(self,fields=['card-num','name','level','qty','cost'],filter=''):
  102. logger.debug("Market Show Data.")
  103. if filter != '':
  104. pdf=self.df.query(filter)
  105. else:
  106. pdf=self.df
  107. print(pdf[fields].to_string(index=False))
  108. def commit(self):
  109. def newCard(Card):
  110. thisCard={}
  111. thisCard['date'] = today
  112. thisCard['count'] = 1
  113. thisCard['card-num'] = Card['card-num']
  114. thisCard['name'] = Card['name']
  115. thisCard['qty'] = Card['qty']
  116. thisCard['cost'] = Card['cost']
  117. thisCard['cost-min'] = Card['cost']
  118. thisCard['cost-max'] = Card['cost']
  119. thisCard['level'] = Card['level']
  120. thisCard['tier'] = Card['tier']
  121. if Card['cost']!=0: MarketDB.insert(thisCard)
  122. def updateCard(Card):
  123. matchCard=MarketDB.search(Query().fragment({'date':today,'card-num':Card['card-num']}))
  124. if len(matchCard)==0:
  125. newCard(Card)
  126. else:
  127. thisCard=matchCard[0]
  128. newData={}
  129. newData['count']=thisCard['count']+1
  130. newData['qty']=(thisCard['count']*thisCard['qty']+Card['qty'])/(thisCard['count']+1)
  131. newData['cost']=(thisCard['count']*thisCard['cost']+Card['cost'])/(thisCard['count']+1)
  132. if 'cost-min' not in thisCard:
  133. newData['cost-min'] = Card['cost']
  134. elif thisCard['cost-min'] > Card['cost']:
  135. newData['cost-min'] = Card['cost']
  136. if 'cost-max' not in thisCard:
  137. newData['cost-max'] = Card['cost']
  138. elif thisCard['cost-max'] < Card['cost']:
  139. newData['cost-max'] = Card['cost']
  140. if Card['cost']!=0: MarketDB.update(newData,Query().fragment({'date':today,'card-num':Card['card-num']}))
  141. logger.debug("Market Commit.")
  142. today=self.asOf.strftime('%Y-%m-%d')
  143. current=MarketDB.search(Query()['date']==today)
  144. if len(current)==0:
  145. for Card in self.Cards:
  146. newCard(Card)
  147. else:
  148. for Card in self.Cards:
  149. updateCard(Card)