update function now getting share data from json and updating on all shares
This commit is contained in:
parent
536bdf3bd6
commit
e4423a76b7
7
telegram_bot/api_handler.py
Normal file
7
telegram_bot/api_handler.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
"""
|
||||||
|
script for communicating with webservice to get data from database
|
||||||
|
"""
|
||||||
|
__author__ = "Florian Kellermann, Linus Eickhoff"
|
||||||
|
__date__ = "16.03.2022"
|
||||||
|
__version__ = "0.0.1"
|
||||||
|
__license__ = "None"
|
@ -20,6 +20,7 @@ import telebot
|
|||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
|
import json
|
||||||
|
|
||||||
import news.news_fetcher as news
|
import news.news_fetcher as news
|
||||||
import shares.share_fetcher as share_fetcher
|
import shares.share_fetcher as share_fetcher
|
||||||
@ -30,7 +31,7 @@ from dotenv import load_dotenv
|
|||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
bot_version = "0.1.1"
|
bot_version = "0.2.1"
|
||||||
user_list = []
|
user_list = []
|
||||||
|
|
||||||
class User: # Currently saving users in this class to test functionality -> later database
|
class User: # Currently saving users in this class to test functionality -> later database
|
||||||
@ -161,15 +162,30 @@ def send_update(message):
|
|||||||
"""
|
"""
|
||||||
user_id = int(message.from_user.id)
|
user_id = int(message.from_user.id)
|
||||||
|
|
||||||
share_fetcher = share_fetcher.Share_Handler()
|
#Can be deleted when getting from database
|
||||||
|
dirname = os.path.dirname(__file__)
|
||||||
|
json_path = os.path.join(dirname, 'shares/shares_example.json')
|
||||||
|
|
||||||
#Get Information for user with this id
|
with open(json_path) as json_file:
|
||||||
#call Share_Handler
|
json_share_data = json.load(json_file)
|
||||||
bot.send_message(chat_id=user_id, text='This is your update')
|
int_share_count = int(json_share_data['share_count'])
|
||||||
|
|
||||||
|
for i in range(int_share_count):
|
||||||
|
|
||||||
|
my_share = json_share_data['shares'][i]
|
||||||
|
my_share_symbol = str(my_share['symbol'])
|
||||||
|
my_share_amount = float(my_share['amount_bought'])
|
||||||
|
my_share_buy_price = float(my_share['price_bought'])
|
||||||
|
my_share_course = float(share_fetcher.get_share_price(my_share_symbol))
|
||||||
|
|
||||||
|
|
||||||
|
my_update_message = f'Symbol: {my_share_symbol}\nPrice: {my_share_course}\nBought for: {my_share_buy_price}\n\
|
||||||
|
Amount owned: {my_share_amount}\nWin/Lose: {(my_share_amount*my_share_course) - (my_share_amount*my_share_buy_price)}'
|
||||||
|
bot.send_message(chat_id=user_id, text=my_update_message)
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['share'])
|
@bot.message_handler(commands=['share'])
|
||||||
def send_update(message):
|
def send_share_update(message):
|
||||||
|
|
||||||
""" Send price of a specific share
|
""" Send price of a specific share
|
||||||
:type message: message object bot
|
:type message: message object bot
|
||||||
@ -187,12 +203,10 @@ def send_update(message):
|
|||||||
bot.register_next_step_handler(message, send_share_price)
|
bot.register_next_step_handler(message, send_share_price)
|
||||||
|
|
||||||
def send_share_price(message):
|
def send_share_price(message):
|
||||||
share_fetcher_obj = share_fetcher.Share_Handler()
|
str_share_price = share_fetcher.get_share_price(str(message.text))
|
||||||
str_share_price = share_fetcher_obj.get_share_price(str(message.text))
|
|
||||||
bot.reply_to(message, str_share_price)
|
bot.reply_to(message, str_share_price)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['news'])
|
@bot.message_handler(commands=['news'])
|
||||||
def send_news(message):
|
def send_news(message):
|
||||||
|
|
||||||
|
@ -7,26 +7,9 @@ __version__ = "0.0.2"
|
|||||||
__license__ = "None"
|
__license__ = "None"
|
||||||
|
|
||||||
import yfinance
|
import yfinance
|
||||||
|
import json
|
||||||
|
|
||||||
|
def get_share_price(str_symbol):
|
||||||
class Share_Handler:
|
|
||||||
def __init__(self):
|
|
||||||
return
|
|
||||||
|
|
||||||
def all_share_prices_for_user(self, int_user_id):
|
|
||||||
|
|
||||||
""" Get all share prices for a certain user with his id
|
|
||||||
:type int_user_id: integer
|
|
||||||
:param int_user_id: user_id to get all share prices for
|
|
||||||
|
|
||||||
:raises: none
|
|
||||||
|
|
||||||
:rtype: ###tbd### (maybe dictonary)
|
|
||||||
"""
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def get_share_price(self, str_symbol):
|
|
||||||
|
|
||||||
""" get current share price for a certain symbol
|
""" get current share price for a certain symbol
|
||||||
:type str_symbol: string
|
:type str_symbol: string
|
||||||
@ -39,18 +22,6 @@ class Share_Handler:
|
|||||||
|
|
||||||
my_share_info = yfinance.Ticker(str_symbol)
|
my_share_info = yfinance.Ticker(str_symbol)
|
||||||
my_share_data = my_share_info.info
|
my_share_data = my_share_info.info
|
||||||
my_return_string = f'{my_share_data["regularMarketPrice"]} {my_share_data["currency"]}'
|
#my_return_string = f'{my_share_data["regularMarketPrice"]} {my_share_data["currency"]}'
|
||||||
|
my_return_string = f'{my_share_data["regularMarketPrice"]}'
|
||||||
return my_return_string
|
return my_return_string
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
|
||||||
""" test object and get share price
|
|
||||||
:raises: none
|
|
||||||
|
|
||||||
:rtype: none
|
|
||||||
"""
|
|
||||||
|
|
||||||
new_handler = Share_Handler()
|
|
||||||
print(new_handler.get_share_price("TL0.DE"))
|
|
16
telegram_bot/shares/shares_example.json
Normal file
16
telegram_bot/shares/shares_example.json
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"user": "NormalParameter",
|
||||||
|
"share_count": 2,
|
||||||
|
"shares": [
|
||||||
|
{
|
||||||
|
"symbol": "APC.DE",
|
||||||
|
"price_bought": "50.06",
|
||||||
|
"amount_bought": "5.1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"symbol": "TL0.DE",
|
||||||
|
"price_bought": "450.06",
|
||||||
|
"amount_bought": "5.13"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user