-
Notifications
You must be signed in to change notification settings - Fork 18
/
get-yahoo-quotes.py
executable file
·98 lines (71 loc) · 2.89 KB
/
get-yahoo-quotes.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
#!/usr/bin/env python
"""
get-yahoo-quotes.py: Script to download Yahoo historical quotes using the new cookie authenticated site.
Usage: get-yahoo-quotes SYMBOL
History
06-03-2017 : Created script
"""
__author__ = "Brad Luicas"
__copyright__ = "Copyright 2017, Brad Lucas"
__license__ = "MIT"
__version__ = "1.0.0"
__maintainer__ = "Brad Lucas"
__email__ = "[email protected]"
__status__ = "Production"
import re
import sys
import time
import datetime
import requests
def split_crumb_store(v):
return v.split(':')[2].strip('"')
def find_crumb_store(lines):
# Looking for
# ,"CrumbStore":{"crumb":"9q.A4D1c.b9
for l in lines:
if re.findall(r'CrumbStore', l):
return l
print("Did not find CrumbStore")
def get_cookie_value(r):
return {'B': r.cookies['B']}
def get_page_data(symbol):
url = "https://finance.yahoo.com/quote/%s/?p=%s" % (symbol, symbol)
headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:74.0) Gecko/20100101 Firefox/74.0'}
r = requests.get(url,headers=headers)
cookie = get_cookie_value(r)
# Code to replace possible \u002F value
# ,"CrumbStore":{"crumb":"FWP\u002F5EFll3U"
# FWP\u002F5EFll3U
lines = r.content.decode('unicode-escape').strip(). replace('}', '\n')
return cookie, lines.split('\n')
def get_cookie_crumb(symbol):
cookie, lines = get_page_data(symbol)
crumb = split_crumb_store(find_crumb_store(lines))
return cookie, crumb
def get_data(symbol, start_date, end_date, cookie, crumb):
filename = '%s.csv' % (symbol)
headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:74.0) Gecko/20100101 Firefox/74.0'}
url = "https://query1.finance.yahoo.com/v7/finance/download/%s?period1=%s&period2=%s&interval=1d&events=history&crumb=%s" % (symbol, start_date, end_date, crumb)
response = requests.get(url, cookies=cookie, headers=headers)
with open (filename, 'wb') as handle:
for block in response.iter_content(1024):
handle.write(block)
def get_now_epoch():
# @see https://www.linuxquestions.org/questions/programming-9/python-datetime-to-epoch-4175520007/#post5244109
return int(time.time())
def download_quotes(symbol):
start_date = 0
end_date = get_now_epoch()
cookie, crumb = get_cookie_crumb(symbol)
get_data(symbol, start_date, end_date, cookie, crumb)
if __name__ == '__main__':
# If we have at least one parameter go ahead and loop overa all the parameters assuming they are symbols
if len(sys.argv) == 1:
print("\nUsage: get-yahoo-quotes.py SYMBOL\n\n")
else:
for i in range(1, len(sys.argv)):
symbol = sys.argv[i]
print("--------------------------------------------------")
print("Downloading %s to %s.csv" % (symbol, symbol))
download_quotes(symbol)
print("--------------------------------------------------")