Can add stocks and view table output on discord
This commit is contained in:
parent
dcb1e5d1e9
commit
b3af02125c
6 changed files with 126 additions and 12 deletions
35
#database.py#
Normal file
35
#database.py#
Normal file
|
@ -0,0 +1,35 @@
|
|||
import json
|
||||
import asyncio
|
||||
from config import config
|
||||
db = config['database']
|
||||
|
||||
def write_file(data):
|
||||
with open(db, 'w') as f:
|
||||
f.write(data)
|
||||
|
||||
def get_stocks(user):
|
||||
with open(db) as f:
|
||||
data = json.loads(f.read())
|
||||
|
||||
if user in data.keys():
|
||||
user_data = data[user]
|
||||
return user_data['portfolio']
|
||||
else:
|
||||
data[user] = {"portfolio": {}}
|
||||
write_file(json.dumps(data))
|
||||
return get_stocks(user)
|
||||
|
||||
def add_stock(user, stock, amount):
|
||||
with open(db) as f:
|
||||
data = json.loads(f.read())
|
||||
if user in data.keys():
|
||||
print(data.keys())
|
||||
if stock in data[user]['portfolio'].keys():
|
||||
data[user]['portfolio'][stock] += amount
|
||||
else:
|
||||
data[user]['portfolio'][stock] = amount
|
||||
write_file(json.dumps(data))
|
||||
return True
|
||||
else:
|
||||
get_stocks(user)
|
||||
return add_stock(user, stock, amount)
|
1
.#database.py
Symbolic link
1
.#database.py
Symbolic link
|
@ -0,0 +1 @@
|
|||
socks@meme-machine.555521:1612898779
|
50
database.py
Normal file
50
database.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
import json
|
||||
import asyncio
|
||||
import yfinance as yf
|
||||
from decimal import Decimal
|
||||
from config import config
|
||||
db = config['database']
|
||||
|
||||
def stock_exists(ticker):
|
||||
try:
|
||||
yf.Ticker(ticker).info
|
||||
return True
|
||||
except KeyError:
|
||||
return False
|
||||
|
||||
def write_file(data):
|
||||
with open(db, 'w') as f:
|
||||
f.write(data)
|
||||
|
||||
def get_stocks(user):
|
||||
with open(db) as f:
|
||||
data = json.loads(f.read())
|
||||
|
||||
data = dict(data)
|
||||
print("USER" + str(user))
|
||||
if user in list(data.keys()):
|
||||
user_data = data[user]
|
||||
return user_data['portfolio']
|
||||
else:
|
||||
print(data)
|
||||
data[user] = {"portfolio": {}}
|
||||
write_file(json.dumps(data))
|
||||
return get_stocks(user)
|
||||
|
||||
def add_stock(user, stock, amount):
|
||||
|
||||
with open(db) as f:
|
||||
data = dict(json.loads(f.read()))
|
||||
print(data)
|
||||
print(type(data))
|
||||
if user in data.keys():
|
||||
if stock in data[user]['portfolio'].keys():
|
||||
data[user]['portfolio'][stock] = str(Decimal(amount) + Decimal( data[user]['portfolio'][stock] ))
|
||||
elif stock_exists(stock):
|
||||
data[user]['portfolio'][stock] = str(Decimal(amount))
|
||||
else: return False
|
||||
write_file(json.dumps(data))
|
||||
return True
|
||||
else:
|
||||
get_stocks(user)
|
||||
return add_stock(user, stock, amount)
|
32
main.py
32
main.py
|
@ -1,5 +1,37 @@
|
|||
import discord
|
||||
import database, table
|
||||
from discord.ext import commands
|
||||
from config import config
|
||||
|
||||
intents = discord.Intents.default()
|
||||
|
||||
bot = commands.Bot(
|
||||
command_prefix="$",
|
||||
intents=intents,
|
||||
)
|
||||
|
||||
|
||||
@bot.command()
|
||||
async def ping(ctx):
|
||||
await ctx.send("pong!")
|
||||
|
||||
|
||||
@bot.command()
|
||||
async def add(ctx, stock, amount):
|
||||
try:
|
||||
float(amount)
|
||||
except ValueError:
|
||||
await ctx.send("amoutn not a number")
|
||||
return
|
||||
|
||||
user = str(ctx.message.author)
|
||||
if database.add_stock(user, stock, amount):
|
||||
return await ctx.send(str( "```" + table.generate_table(database.get_stocks(user)) + "```" ))
|
||||
else:
|
||||
return await ctx.send("Stock **{0}** does not exist!".format(stock))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bot.run('ODA5ODg1MjA2OTAyOTk3MDI0.YCbmYA.mFwhaYwHfRx-MLRAK1Qr0Ch2XjE')
|
||||
|
|
1
stonks.txt
Normal file
1
stonks.txt
Normal file
|
@ -0,0 +1 @@
|
|||
{"Katte#3575": {"portfolio": {"AAPL": "0.37795852", "GME": "6.43", "AMD": "0.2661378", "AMZN": "0.00534034", "ETH-USD": "0.02", "HMMJ.TO": "14.14"}}, "Haedrien#6134": {"portfolio": {"UAMY": "150", "GME": "92.922", "ZOM": "100", "otlk": "100", "zsan": "100", "CTRM": "13.48"}}, "Forchex#1473": {"portfolio": {"AMC": "12", "GME": "19.7", "TGB": "56.52"}}, "Stonks#4179": {"portfolio": {"TANH": "100", "MICT": "100", "CEPU": "100", "NEXT": "100", "FTK": "100", "AVCO": "100", "CJJD": "100"}}}
|
19
table.py
19
table.py
|
@ -1,4 +1,5 @@
|
|||
from tabulate import tabulate
|
||||
from decimal import Decimal
|
||||
import yfinance as yf
|
||||
|
||||
|
||||
|
@ -11,7 +12,7 @@ def get_current_price(ticker):
|
|||
stonks = {
|
||||
'MSFT': 4.3,
|
||||
'AAPL': 13,
|
||||
'GME' : 212
|
||||
'GME' : 212
|
||||
}
|
||||
|
||||
|
||||
|
@ -20,16 +21,10 @@ def generate_table(stonks):
|
|||
total = 0
|
||||
for ticker in stonks.keys():
|
||||
price = get_current_price(ticker)
|
||||
table_data.append([ticker, price * stonks[ticker]])
|
||||
total += (price * stonks[ticker])
|
||||
shares_count = stonks[ticker]
|
||||
table_data.append([ticker, shares_count, Decimal(price) * Decimal(stonks[ticker])])
|
||||
total += (Decimal(price) * Decimal(stonks[ticker]))
|
||||
|
||||
table_data.append(['Total', total])
|
||||
|
||||
return table_data
|
||||
table_data.append(['Total', '', total])
|
||||
|
||||
|
||||
print( tabulate(
|
||||
generate_table(stonks),
|
||||
['Ticker', 'Value'],
|
||||
tablefmt="fancy_grid"
|
||||
))
|
||||
return str( tabulate(table_data, ['Ticker', 'Shares', 'Value ($)'], tablefmt="fancy_grid") )
|
||||
|
|
Loading…
Add table
Reference in a new issue