-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle_finance.py
executable file
·175 lines (143 loc) · 5.33 KB
/
google_finance.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
"""
Get financial data from Google Finance.
Requirment:
pyquery 1.2.6. (1.2.1 did not work)
Report types:
inc - income statement
bal - balance sheet
cas - cash flow
usage: google_finance.py [-h] [-m MARKET] [-r {inc,bal,cas}]
[-t {annual,interim}] [-c CSV]
[symbol]
positional arguments:
symbol symbol
optional arguments:
-h, --help show this help message and exit
-m MARKET, --market MARKET
market string
-r {inc,bal,cas}, --report-type {inc,bal,cas}
report type
-t {annual,interim}, --term {annual,interim}
report term
-c CSV, --csv CSV CSV file name
"""
import csv
import re
import sys
from datetime import date
from decimal import Decimal
from pyquery import PyQuery as pq
GOOGLE_FINANCE_REPORT_TYPES = {
'inc': 'Income Statement',
'bal': 'Balance Sheet',
'cas': 'Cash Flow',
}
DATE = re.compile(".*(\d{4})-(\d{2})-(\d{2}).*")
class GoogleFinance(object):
"""
Get financial data from Google Finance.
aapl = GoogleFinance('NASDAQ', 'AAPL')
print aapl.cash_flow()
"""
GOOGLE_FINANCE_URL = "https://www.google.com/finance?q={}:{}&fstype=ii"
def __init__(self, market, symbol):
self.market = market.upper()
self.symbol = symbol.upper()
self._financial = None
@staticmethod
def _parse_number(s):
"""
return decimal object if the given string is parseable as number.
return None if the string is -
otherwise return the string as is
"""
if s == '-':
return None
try:
return Decimal(s.replace(',', ''))
except Exception, _:
pass
return s
@staticmethod
def _parse_date(s):
"""
return datetime object if the given string contains YYYY-MM-DD string
otherwise return the string as is
"""
m = DATE.match(s)
if m:
return date(*[int(e) for e in m.groups()])
return s
@staticmethod
def to_csv(csv_file_name, report):
with open(csv_file_name, 'w') as fp:
writer = csv.writer(fp, delimiter=',', quotechar='"',
quoting=csv.QUOTE_NONNUMERIC)
for row in report:
writer.writerow(row)
def _get_from_google(self):
return pq(self.GOOGLE_FINANCE_URL.format(self.market, self.symbol))
def _get_table(self, report_type, term):
assert term in ('interim', 'annual')
assert report_type in ('inc', 'bal', 'cas')
if not self._financial:
self._financial = self._get_from_google()
div_id = report_type + term + 'div'
return self._financial('div#{} table#fs-table'.format(div_id))
def _statement(self, stmt_type, term):
tbl = self._get_table(stmt_type, term)
ret = []
for row in tbl.items('tr'):
data = [self._parse_number(i.text()) for i in row.items('th, td')]
if not ret:
data = [self._parse_date(e) for e in data]
ret.append(data)
return zip(*ret)
def income_statement(self, term='annual'):
return self._statement('inc', term)
def balance_sheet(self, term='annual'):
return self._statement('bal', term)
def cash_flow(self, term='annual'):
return self._statement('cas', term)
def main(args):
google_finance = GoogleFinance(args.market, args.symbol)
financial_report = None
if args.report_type == 'inc':
financial_report = google_finance.income_statement(args.report_term)
elif args.report_type == 'bal':
financial_report = google_finance.balance_sheet(args.report_term)
elif args.report_type == 'cas':
financial_report = google_finance.cash_flow(args.report_term)
if not financial_report:
print "{} {} financial report not available for {}:{}".format(
args.report_term.title(), GOOGLE_FINANCE_REPORT_TYPES.get(args.report_type, 'Unknown'),
google_finance.market, google_finance.symbol)
sys.exit()
if args.csv:
google_finance.to_csv(args.csv, financial_report)
else:
print financial_report
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser(description='get financial data from Google Finance')
parser.add_argument('-m', '--market', type=str,
action='store', dest='market',
help='market string', default='NASDAQ')
parser.add_argument('-r', '--report-type', type=str,
help='report type', default='inc',
action='store', dest='report_type',
choices=['inc', 'bal', 'cas'])
parser.add_argument('-t', '--term', type=str,
help='report term', default='annual',
action='store', dest='report_term',
choices=['annual', 'interim'])
parser.add_argument('-c', '--csv', type=str,
help='CSV file name', action='store')
parser.add_argument('symbol', action='store', type=str, nargs='?',
help='symbol')
args = parser.parse_args()
if not args.symbol:
print "please supply symbol"
sys.exit()
main(args)