-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfolio.py
101 lines (85 loc) · 3.55 KB
/
portfolio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import sys
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# The ID and range of a sample spreadsheet.
#SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
SAMPLE_SPREADSHEET_ID = '1TE4mjYDjDBeLbHbSlOEeU5k-H9lP2A9DEae7sktgGbo'
SAMPLE_RANGE_NAME = 'A2:H12'
class InvestmentPortfolio():
def __init__(self):
self.google_spreadsheet_id = '1TE4mjYDjDBeLbHbSlOEeU5k-H9lP2A9DEae7sktgGbo'
self.google_sheet_id = 0
self.service = ''
self.portfolio = dict() #need to read csv file and then add to it
def __validate_google_credentials(self):
"""validates user credentials
user must give authorization of user data
"""
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
self.service = build('sheets', 'v4', credentials=creds)
def company_share_owned(self, ticker):
"""returns true if company share is owned"""
self.__validate_google_credentials()
sheet = self.service.spreadsheets()
result = sheet.values().get(spreadsheetId=self.google_spreadsheet_id,
range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])
for row in values:
if ticker == row[1]:
return True
return False
def add_option_purchase(self, ticker):
#get updated price
#update excel sheet
pass
def add_share_purchase(self, ticker):
pass
def print_portfolio(self):
"""printing out values pulled from sheet"""
self.__validate_google_credentials()
sheet = self.service.spreadsheets()
result = sheet.values().get(spreadsheetId=self.google_spreadsheet_id,
range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
print('pulled data:')
print('----------------')
print('ALL VALUES\n', '-----------------\n', values)
print('ONLY PRICES\n', '----------------')
print('{:25} {}'.format('name', 'price'))
for row in values:
if len(row) < 8 or row[2] != '[OWN]':
continue
else:
print('{:25} {}'.format(row[0], row[6]))
def print_update_queue():
pass
def update_portfolio():
pass
if __name__ == "__main__":
test = InvestmentPortfolio()
ticker = sys.argv[1]
if test.company_share_owned(ticker):
test.print_portfolio()
else:
print('You do not own shares of this company yet!')