-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathQueryBuilder.py
77 lines (68 loc) · 2.38 KB
/
QueryBuilder.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
#
# Copyright 2019, Ruben Afonso - http://www.github.com/rubenafo
# Licensed under the Apache License (see the LICENSE file)
#
#
# This class builds the URL using Yahoo conventions.
#
import urllib.request
from datetime import datetime
class Query:
BASE_URL = "http://finance.yahoo.com/quote/AAPL"
HIST_URL = "https://query1.finance.yahoo.com/v7/finance/download/{}?{}";
def refreshCookie (self):
self.cookie = None
self.crub = None
cookier = urllib.request.HTTPCookieProcessor()
opener = urllib.request.build_opener(cookier)
urllib.request.install_opener(opener)
f = urllib.request.urlopen(self.BASE_URL)
alines = f.read().decode()
cs = alines.find('CrumbStore')
cr = alines.find('crumb', cs + 10)
cl = alines.find(':', cr + 5)
q1 = alines.find('"', cl + 1)
q2 = alines.find('"', q1 + 1)
crumb = alines[q1 + 1:q2]
self.crumb = crumb
for c in cookier.cookiejar:
if c.domain != '.yahoo.com':
continue
if c.name != 'B':
continue
self.cookie = c.value
def __init__(self):
self.refreshCookie()
#
# Historical data methods
#
# Date params are in format yyyymmdd
def getHistURL (self, symbol, begindate, enddate, event, verbose=False):
tb = datetime(int(begindate[0:4]), int(begindate[4:6]), int(begindate[6:8]), 0, 0)
te = datetime(int(enddate[0:4]), int(enddate[4:6]), int(enddate[6:8]), 0, 0)
param = dict()
param['period1'] = int(tb.timestamp())
param['period2'] = int(te.timestamp())
param['interval'] = '1d'
if event == 'quote':
param['events'] = 'history'
elif event == 'div':
param['events'] = 'div'
elif event == 'split':
param['events'] = 'split'
for i in [1,5]:
try:
param['crumb'] = self.crumb
params = urllib.parse.urlencode(param)
url = self.HIST_URL.format(symbol, params)
if verbose:
print (url)
f = urllib.request.urlopen(url)
alines = f.readlines()
return [a.decode("UTF-8").strip() for a in alines[1:] if len(a) > 0]
except (urllib.error.HTTPError, urllib.error.URLError):
# this handles the spurious HTTPError unauthorized, perhaps the yahoo side didn't process the cookie
# so the crumb gets rejected. 5 times for this should be enough
self.refreshCookie()
print ("Warning: unable to retrieve Yahoo data for " + symbol)
return []