market.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. var stock = innerCard.getElementsByClassName('Stock')[0]
  88. if (stock){stock.innerText = "Stock: " + clist[cardNum]}
  89. else {
  90. let qty = document.createElement('div');
  91. qty.classList.add('Stock')
  92. qty.innerText = "Stock: " + clist[cardNum]
  93. innerCard.appendChild(qty)
  94. }
  95. if (clist[cardNum].includes('CRYSTAL')){
  96. cards.item(i).style.setProperty('Display','None');
  97. }
  98. }
  99. """
  100. print(js)
  101. self.driver.execute_script(js)
  102. def loadDF(self):
  103. logger.debug("Market Load DF.")
  104. self.df=pd.DataFrame(self.Cards)
  105. self.df.sort_values(by='card-num', ascending=True, inplace=True)
  106. def show(self,fields=['card-num','name','level','qty','cost'],filter=''):
  107. logger.debug("Market Show Data.")
  108. if filter != '':
  109. pdf=self.df.query(filter)
  110. else:
  111. pdf=self.df
  112. print(pdf[fields].to_string(index=False))
  113. def commit(self):
  114. def newCard(Card):
  115. thisCard={}
  116. thisCard['date'] = today
  117. thisCard['count'] = 1
  118. thisCard['card-num'] = Card['card-num']
  119. thisCard['name'] = Card['name']
  120. thisCard['qty'] = Card['qty']
  121. thisCard['cost'] = Card['cost']
  122. thisCard['cost-min'] = Card['cost']
  123. thisCard['cost-max'] = Card['cost']
  124. thisCard['level'] = Card['level']
  125. thisCard['tier'] = Card['tier']
  126. if Card['cost']!=0: MarketDB.insert(thisCard)
  127. def updateCard(Card):
  128. matchCard=MarketDB.search(Query().fragment({'date':today,'card-num':Card['card-num']}))
  129. if len(matchCard)==0:
  130. newCard(Card)
  131. else:
  132. thisCard=matchCard[0]
  133. newData={}
  134. newData['count']=thisCard['count']+1
  135. newData['qty']=(thisCard['count']*thisCard['qty']+Card['qty'])/(thisCard['count']+1)
  136. newData['cost']=(thisCard['count']*thisCard['cost']+Card['cost'])/(thisCard['count']+1)
  137. if 'cost-min' not in thisCard:
  138. newData['cost-min'] = Card['cost']
  139. elif thisCard['cost-min'] > Card['cost']:
  140. newData['cost-min'] = Card['cost']
  141. if 'cost-max' not in thisCard:
  142. newData['cost-max'] = Card['cost']
  143. elif thisCard['cost-max'] < Card['cost']:
  144. newData['cost-max'] = Card['cost']
  145. if Card['cost']!=0: MarketDB.update(newData,Query().fragment({'date':today,'card-num':Card['card-num']}))
  146. logger.debug("Market Commit.")
  147. today=self.asOf.strftime('%Y-%m-%d')
  148. current=MarketDB.search(Query()['date']==today)
  149. if len(current)==0:
  150. for Card in self.Cards:
  151. newCard(Card)
  152. else:
  153. for Card in self.Cards:
  154. updateCard(Card)