From 5a711b34d517b200d52821b1d0b82b23ccf4e984 Mon Sep 17 00:00:00 2001 From: Jason Ji Date: Sun, 24 Sep 2017 17:31:03 -0700 Subject: [PATCH] stock plugin mk1 --- .gitlab-ci.yml | 2 +- Dockerfile | 2 +- Dockerfile-test-env | 2 +- app/dragon-bot.py | 9 ++++++++- app/stock.py | 21 +++++++++++++++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 app/stock.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6435955e..4683a946 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ services: before_script: - apk add --no-cache python3 - - pip3 install pylint requests discord.py docker pylint wolframalpha pyowm beautifulsoup4 + - pip3 install pylint requests discord.py docker pylint wolframalpha pyowm beautifulsoup4 yahoo-finance stages: - test diff --git a/Dockerfile b/Dockerfile index dc8e08cd..f363aca9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM python:3.6.2-alpine3.6 LABEL name="Dragon Bot" RUN apk update && apk add --no-cache docker -RUN pip install requests discord.py docker wolframalpha pyowm beautifulsoup4 +RUN pip install requests discord.py docker wolframalpha pyowm beautifulsoup4 yahoo-finance ADD app /app CMD python app/dragon-bot.py diff --git a/Dockerfile-test-env b/Dockerfile-test-env index a56e5f94..d47534e8 100644 --- a/Dockerfile-test-env +++ b/Dockerfile-test-env @@ -2,7 +2,7 @@ FROM python:3.6.2-alpine3.6 LABEL name="Dragon Bot Test environment" RUN apk update && apk add --no-cache vim docker -RUN pip install requests discord.py docker pylint wolframalpha pyowm beautifulsoup4 +RUN pip install requests discord.py docker pylint wolframalpha pyowm beautifulsoup4 yahoo-finance ADD app /app RUN printf "\n\nTesting your python code for errors\n\n" && \ diff --git a/app/dragon-bot.py b/app/dragon-bot.py index d25ce402..49739c06 100644 --- a/app/dragon-bot.py +++ b/app/dragon-bot.py @@ -22,6 +22,7 @@ import role_check import wallpaper import weather import wolfram +import stock # Client object client = discord.Client() @@ -121,7 +122,13 @@ async def on_message(message): if message.content.startswith('!weather'): await client.send_message( message.channel, - weather.get_weather(message.content) + weather.parse_loc(message.content) + ) + + if message.content.startswith('!stock'): + await client.send_message( + message.channel, + stock.parse_share(message.content) ) if message.content.startswith('!pout'): diff --git a/app/stock.py b/app/stock.py new file mode 100644 index 00000000..ce6bb875 --- /dev/null +++ b/app/stock.py @@ -0,0 +1,21 @@ +import yahoo_finance + +def parse_share(msg): + if len(msg.split()) > 1: + print(msg.split()[1:]) + try: + res = '' + for s in msg.split()[1:]: + res = '{}\n\n{}'.format(res, get_stock(s)) + except yahoo_finance.YQLResponseMalformedError: + res = '```Please input valid shares: !stock [share_name]```' + return res + return '```Please input at least one valid share: !stock [share_name]```' + +def get_stock(share_name): + share = yahoo_finance.Share(share_name) + open_price = share.get_open() + curr_price = share.get_price() + change = curr_price - open_price + return '```Stock: {}\n\nOpen: ${}\nCurrently: ${}\nChange:```'.format( + share_name.upper(), open_price, curr_price)