# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/ashesm/ISYS5002_PORTFOLIO/blob/main/sandbox.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="mwCjE8lFOLHX"
# # Algorithm
#
# 1. Get the ticker symbol form the user
# 2. Use the symbol to goto the profile page
# 3. Get the table
# * for each row get the pay
# * Use pay to calulate tax
# * Add tax to table
# 4. Save as CSV
# 5. Save as SQLite

# + id="XSVd2VQVTMhJ"
# Usually run of Google Coolab, so install unique packages
# !pip install kora -q

#load packages
from bs4 import BeautifulSoup
from kora.selenium import wd
import pandas as pd


# + id="OFXJAXX8RzzL"
def build_url():
  ''' This function will build the URL of the profile page'''
  ticker = input("Please type in a ticker symbol ")
  url_pattern = 'https://au.finance.yahoo.com/quote/{}/profile?p={}'
  result = url_pattern.format(ticker, ticker)
  return result


# + id="Gg2ibSI8Ur-v"
def get_record(row):
  cells = row.find_all('td')
  name = cells[0].text
  title =  cells[1].text
  pay = cells[2].text
  return {'name': name, 'title': title, 'pay': pay}



# + id="SQ2hz7drXXHl"
def get_rows(soup):
  # Get the table!
  table = soup.find('table')

  # get to the row
  body = table.find('tbody')
  rows = body.find_all('tr')
  return rows


# + id="jExc7IhtY2ge"
def calculate_tax(income):
  if income <= 18200:
    tax_payable = 0
  elif 18200 < income <= 45000:
    tax_payable = 0.19 * (income - 18200)
  elif 45000 < income < 120000:
    tax_payable = 5092 + 0.32 * (income - 45000)
  elif 120001 < income < 180000:
    tax_payable = 29467 + 0.37 * (income - 120000)
  elif income > 180000:
    tax_payable = 51667 + 0.45 * (income - 180000)


  return tax_payable


# + id="qqCsCO7OZyML"
def get_salary(pay):  
  last_char = pay[-1]
  if last_char == 'M':
    pay_as_num = float(pay[0:-1])
    sal = pay_as_num * 1000000
  elif last_char == 'k':
    pay_as_num = float(pay[0:-1])
    sal = pay_as_num * 1000
  else: # Must be a N/A
    sal = 0
  return sal



# + colab={"base_uri": "https://localhost:8080/", "height": 380} id="7M_zc_8LX6zo" outputId="c5affc47-3b71-46f6-bdf6-a5b6eafa9c8c"
url = build_url()
wd.get(url)
soup = BeautifulSoup(wd.page_source,'html.parser')
rows = get_rows(soup)

# getting all the records
records = []
for row in rows:
  rec = get_record(row)
  records.append(rec)

# adding the tax colum
for rec in records:
  sal = get_salary(rec['pay'])
  rec['tax'] = calculate_tax(sal)

exec_df = pd.DataFrame.from_records(records)
exec_df

# + id="8R_blr5ob5PR"


StarcoderdataJupyterScriptsDedupFiltered