From b3af02125cf0541c0afccad4a34cc38294cb48c7 Mon Sep 17 00:00:00 2001 From: socks Date: Sat, 13 Feb 2021 04:01:05 +0000 Subject: [PATCH] Can add stocks and view table output on discord --- #database.py# | 35 +++++++++++++++++++++++++++++++++++ .#database.py | 1 + database.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 32 ++++++++++++++++++++++++++++++++ stonks.txt | 1 + table.py | 19 +++++++------------ 6 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 #database.py# create mode 120000 .#database.py create mode 100644 database.py create mode 100644 stonks.txt diff --git a/#database.py# b/#database.py# new file mode 100644 index 0000000..297a4dd --- /dev/null +++ b/#database.py# @@ -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) diff --git a/.#database.py b/.#database.py new file mode 120000 index 0000000..38440b9 --- /dev/null +++ b/.#database.py @@ -0,0 +1 @@ +socks@meme-machine.555521:1612898779 \ No newline at end of file diff --git a/database.py b/database.py new file mode 100644 index 0000000..b0cfb91 --- /dev/null +++ b/database.py @@ -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) diff --git a/main.py b/main.py index 2864331..4563eb8 100644 --- a/main.py +++ b/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') diff --git a/stonks.txt b/stonks.txt new file mode 100644 index 0000000..4f23499 --- /dev/null +++ b/stonks.txt @@ -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"}}} diff --git a/table.py b/table.py index 3ebda06..2c37d6e 100644 --- a/table.py +++ b/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") )