I'm making a bot that takes in the user input (which is a stock symbol) and then provides information on that specific stock. Thing is, I'm not really sure how to use the client.wait_input()
in my case. Here's my code for the bot:
# bot.py
import os
import SandPwebscraper as stocks
from dotenv import load_dotenv
from discord.ext import commands
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='$')
@bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
@bot.command(name = 'symbol', help = 'Type in all-caps symbol of company (ex. when searching for Apple, type AAPL).')
async def Symbol(ctx, companySymbol: str):
#
#
#
#
#
#
await ctx.send(''.join)
bot.run(TOKEN)
And I'm trying to somehow include the stocks_input()
function from SandPwebscraper.py
:
# Web-scraped S&P 500 data for 500 US stocks.
import requests
import pandas as pd
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62'}
url = 'https://www.slickcharts.com/sp500' # Data from SlickCharts
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')
table1 = soup.find('table', attrs={'class':'table table-hover table-borderless table-sm'})
for row in table1.find_all('tr'):
all_td_tags = row.find_all('td')
if len(all_td_tags) > 0:
company = all_td_tags[1].text
symbol = all_td_tags[2].text
weight = all_td_tags[3].text
price = all_td_tags[4].text
chg = all_td_tags[5].text
perChg = all_td_tags[6].text
# print(company, '|', symbol, '|', weight, '|', price, '|', chg, '|', perChg)
df = pd.read_html(str(table1))[0] # Makes data into an html readable and can output and look through data
df.drop(['#'], axis = 1, inplace = True) # Removes an extra column of numbers
# pd.set_option('display.max_rows', None) # Removes cap on number of rows in viewall
def viewall():
allStocks = df
return allStocks
def appletest():
apple = df[df['Symbol'] == 'AAPL']
return apple
def stocks_input():
userinput = input('Enter company name or symbol: ')
if userinput.isupper():
symbol = (df[df['Symbol'] == userinput])
return symbol
else:
if userinput == 'viewall':
allStocks = df
return allStocks
else:
company = (df[df['Company'] == userinput])
return company
In this case, if userinput
is something like "AAPL", then the following would be returned:
Company Symbol Weight Price Chg % Chg
0 Apple Inc. AAPL 6.888732 177.65 -1.73 (-0.96%)
What should I include in the commented section of bot.py
?
CodePudding user response:
The companySymbol
variable already stores the user's input, so you can modify the stocks_input()
function to take input.
def stocks_input(userinput):
if userinput.isupper():
symbol = (df[df['Symbol'] == userinput])
return symbol
else:
if userinput == 'viewall':
allStocks = df
return allStocks
else:
company = (df[df['Company'] == userinput])
return company
Now you can run this function with the companySymbol
as an input:
@bot.command(name = 'symbol', help = 'Type in all-caps symbol of company (ex. when searching for Apple, type AAPL).')
async def Symbol(ctx, companySymbol: str):
values = stocks.stocks_input(companySymbol) # stocks_input function will return values and store it in 'values' variable
await ctx.send(values) # send values