Browse Source

Added a database and a process to gather Market Data every 15 minutes.

Mike Greene 5 months ago
parent
commit
2720a85e9f
5 changed files with 54 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 8 0
      README.md
  3. 2 0
      db.py
  4. 19 0
      gatherMarket.py
  5. 24 0
      market.py

+ 1 - 0
.gitignore

@@ -1,5 +1,6 @@
 # Specific to this Project
 db.json
+market.json
 config.json
 
 # Byte-compiled / optimized / DLL files

+ 8 - 0
README.md

@@ -3,6 +3,14 @@
 This is intended as a helper to find trends in the Booby Legends game.
 
 I intend to built a [Discord Bot Interface](DiscordBot.md) to let us share in our guild.
+
+## Running Executables
+
+### Gather Marketing Data
+This will gather marketing data every 15 minutes, and build an average for each day in UTC timezone.
+```bash
+python3 gatherMarket.py
+```
  
 ## Classes
 

+ 2 - 0
db.py

@@ -0,0 +1,2 @@
+from tinydb import TinyDB, Query
+MarketDB = TinyDB('market.json')

+ 19 - 0
gatherMarket.py

@@ -0,0 +1,19 @@
+from market import Market
+import schedule
+import time
+from datetime import datetime
+
+m=Market()
+
+
+def job():
+    print("Gathering Market Data: "+datetime.utcnow().isoformat())
+    m.fetchData()
+    m.commit()
+
+schedule.every(15).minutes.do(job)
+
+job()
+while 1:
+    schedule.run_pending()
+    time.sleep(1)

+ 24 - 0
market.py

@@ -5,9 +5,11 @@ 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 json
 from player import Player
 from config import Config
+from db import MarketDB
 
 @dataclass
 class Market():
@@ -83,3 +85,25 @@ class Market():
     def show(self):
        pdf=self.df.drop(['link','card-id','phid'], axis=1)
        print(pdf.to_string(index=False))
+
+    def commit(self):
+        today=self.asOf.strftime('%Y-%m-%d')
+        current=MarketDB.search(Query()['date']==today)
+        if len(current)==0:
+            for Card in self.Cards:
+                thisCard={}
+                thisCard['date'] = today
+                thisCard['count'] = 1
+                thisCard['card-num'] = Card['card-num']
+                thisCard['name'] = Card['name']
+                thisCard['qty'] = Card['qty']
+                thisCard['cost'] = Card['cost']
+                MarketDB.insert(thisCard)
+        else:
+            for Card in self.Cards:
+                thisCard=MarketDB.search(Query().fragment({'date':today,'card-num':Card['card-num']}))[0]
+                newData={}
+                newData['count']=thisCard['count']+1
+                newData['cost']=(thisCard['count']*thisCard['cost']+Card['cost'])/(thisCard['count']+1)
+                newData['qty']=(thisCard['count']*thisCard['qty']+Card['qty'])/(thisCard['qty']+1)                
+                MarketDB.update(newData,Query().fragment({'date':today,'card-num':Card['card-num']}))