Skip to content

Commit

Permalink
rewrite in python
Browse files Browse the repository at this point in the history
  • Loading branch information
madnight committed Dec 16, 2023
1 parent b05d58d commit e4899d2
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 2,668 deletions.
22 changes: 0 additions & 22 deletions .eslintrc.js

This file was deleted.

3 changes: 0 additions & 3 deletions .prettierrc.json

This file was deleted.

2 changes: 2 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Symbols]
stocks = AAPL,MSFT,GOOGL,INTC,AMD,PEP,MU,TSLA,NFLX,DIS,AMZN,SPY,QQQ
240 changes: 0 additions & 240 deletions index.js

This file was deleted.

54 changes: 54 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from colorama import Fore, Style
import requests
import configparser
from tabulate import tabulate

def parse_price(details):
price = round(float(details['Price'][1:]), 2)
return '${:.2f}'.format(price)

def parse_change(value):
change = round(float(value.rstrip('%')), 2)
change_str = '{:.2f}%'.format(change)
return Fore.RED + change_str + Style.RESET_ALL if float(change) < 0 else change_str

def read_config_file(filename):
config = configparser.ConfigParser()
config.read(filename)
return config.get('Symbols', 'stocks').split(',')

def get_stock_data(symbols):
url = 'https://finance.beuke.org/{}'.format(','.join(symbols))
return requests.get(url).json()


def parse_price(details):
price = round(float(details['Price'][1:]), 2)
return price # return as float

def calculate_absolute_price(base_price, percentage):
return base_price * (1 + percentage / 100)

def format_stock_data(data):
return [
{
'Symbol': symbol,
'Price': '${:.2f}'.format(parse_price(details)),
'Change': parse_change(details['Change']),
'Average Volume': details['Avg Volume'],
'P/E': details['P/E'],
'Market Cap': details['Market Cap'],
'52W Low': '${:.2f}'.format(calculate_absolute_price(parse_price(details), float(details['52W High'].rstrip('%')))),
'52W High': '${:.2f}'.format(calculate_absolute_price(parse_price(details), float(details['52W Low'].rstrip('%')))),
'YTD Change': parse_change(details['Perf YTD']),
}
for stock in data for symbol, details in stock.items()
]

def print_stock_data(data):
print(tabulate(data, headers='keys', stralign='right', tablefmt='simple_outline'))

symbols = read_config_file('config.ini')
data = get_stock_data(symbols)
formatted_data = format_stock_data(data)
print_stock_data(formatted_data)
34 changes: 0 additions & 34 deletions package.json

This file was deleted.

Loading

0 comments on commit e4899d2

Please sign in to comment.