Skip to content

Commit

Permalink
stocks: allow multi symbol lookup (#7)
Browse files Browse the repository at this point in the history
* stocks: allow multi symbol lookup
  • Loading branch information
RustyBower authored Nov 1, 2022
1 parent d79a9a7 commit c17464b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ However, if you want or need to configure this plugin manually, you will need to
provider = alphavantage (or iexcloud)

## Requirements
#### API Key
#### API Key (from 1 of the following providers)

https://www.alphavantage.co/support/#api-key
https://iexcloud.io/console/tokens
Expand All @@ -45,8 +45,10 @@ However, if you want or need to configure this plugin manually, you will need to

## Usage

.stock djia
DJIA $26559.5 110 (0.42%)⬆

.stock msft
MSFT $123.37 1.6 (1.31%)⬆
<sopel> MSFT $123.37 1.6 (1.31%)⬆

.stock aapl amzn goog
<sopel> AAPL $150.83 -2.51 (-1.64%)⬇
<sopel> AMZN $97.06 -5.38 (-5.25%)⬇
<sopel> GOOG $90.445 -4.215 (-4.45%)⬇
73 changes: 37 additions & 36 deletions sopel_modules/stocks/stocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,51 +65,52 @@ def get_price(bot, symbol):

@commands('stock')
@example('.stock msft')
@example('.stock amzn msft goog')
def stock(bot, trigger):
"""Get the current price for a given stock."""
"""Get the current price for given stock(s)."""
# If the user types .stock with no arguments, let them know proper usage
if not trigger.group(2):
return
else:
# Get symbol
symbol = trigger.group(2)
symbols = trigger.group(2).split()

# Do regex checking on symbol to ensure it's valid
if not re.match('^([a-zA-Z0-9]{1,10}:[a-zA-Z0-9]{1,10}|[a-zA-Z0-9]{1,10})$', symbol):
bot.say('Invalid Symbol')
return
symbols = [symbol for symbol in symbols if re.match('^([a-zA-Z0-9]{1,10}:[a-zA-Z0-9]{1,10}|[a-zA-Z0-9]{1,10})$', symbol)]

# Get data from API
try:
data = get_price(bot, symbol)
except Exception as e:
return bot.say(str(e))

message = (
'{symbol} ${close:g} '
)

# Change is None, usually on IPOs
if not data['change']:
message = message.format(
symbol=symbol.upper(),
close=float(data['close']),
)
# Otherwise, check change versus previous day
else:
if data['change'] >= 0:
message += color('{change:g} ({percentchange:.2f}%)', colors.GREEN)
message += color(u'\u2b06', colors.GREEN)
else:
message += color('{change:g} ({percentchange:.2f}%)', colors.RED)
message += color(u'\u2b07', colors.RED)

message = message.format(
symbol=symbol.upper(),
close=float(data['close']),
change=float(data['change']),
percentchange=float(data['percentchange']),
for symbol in symbols:
try:
data = get_price(bot, symbol)
except Exception as e:
return bot.say(str(e))

message = (
'{symbol} ${close:g} '
)

# Print results to channel
return bot.say(message)
# Change is None, usually on IPOs
if not data['change']:
message = message.format(
symbol=symbol.upper(),
close=float(data['close']),
)
# Otherwise, check change versus previous day
else:
if data['change'] >= 0:
message += color('{change:g} ({percentchange:.2f}%)', colors.GREEN)
message += color(u'\u2b06', colors.GREEN)
else:
message += color('{change:g} ({percentchange:.2f}%)', colors.RED)
message += color(u'\u2b07', colors.RED)

message = message.format(
symbol=symbol.upper(),
close=float(data['close']),
change=float(data['change']),
percentchange=float(data['percentchange']),
)

# Print results to channel
bot.say(message)
return

0 comments on commit c17464b

Please sign in to comment.