-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-yahoo-quotes.sh
83 lines (70 loc) · 2.35 KB
/
get-yahoo-quotes.sh
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
#!/bin/bash
##
## Script to download Yahoo historical quotes using the new cookie authenticated site.
##
## Usage: get-yahoo-quotes SYMBOL
##
##
## Author: Brad Lucas [email protected]
## Latest: https://github.com/bradlucas/get-yahoo-quotes
##
## Copyright (c) 2017 Brad Lucas - All Rights Reserved
##
##
## History
##
## 06-03-2017 : Created script
##
## ----------------------------------------------------------------------------------------------------
SYMBOL=$1
if [[ -z $SYMBOL ]]; then
echo "Please enter a SYMBOL as the first parameter to this script"
exit
fi
# echo "Downloading quotes for $SYMBOL"
function log () {
# To remove logging comment echo statement and uncomment the :
# To enable logging comment : statement and uncomment the echo statement
# echo $1
:
}
# Period values are 'Seconds since 1970-01-01 00:00:00 UTC'. Also known as Unix time or epoch time.
# Let's just assume we want it all and ask for a date range that starts at 1/1/1970.
# NOTE: This doesn't work for old synbols like IBM which has Yahoo has back to 1962
# START_DATE=0
# END_DATE=$(date +%s)
YEAR_SECS=31622400 #seconds in a year
END_DATE=$(date +%s)
START_DATE=`expr $END_DATE - $YEAR_SECS`
# Store the cookie in a temp file
cookieJar=$(mktemp)
# Get the crumb value
function getCrumb () {
# Sometimes the value has an octal character
# echo will convert it
# https://stackoverflow.com/a/28328480
# curl the url then replace the } characters with line feeds. This takes the large json one line and turns it into about 3000 lines
# grep for the CrumbStore line
# then copy out the value
# lastly, remove any quotes
echo -en "$(curl -s --cookie-jar $cookieJar $1)" | tr "}" "\n" | grep CrumbStore | cut -d':' -f 3 | sed 's+"++g'
}
# TODO If crumb is blank then we probably don't have a valid symbol
URL="https://finance.yahoo.com/quote/$SYMBOL/?p=$SYMBOL"
log $URL
crumb=$(getCrumb $URL)
log $crumb
log "CRUMB: $crumb"
if [[ -z $crumb ]]; then
echo "Error finding a valid crumb value"
exit
fi
# Build url with SYMBOL, START_DATE, END_DATE
BASE_URL="https://query1.finance.yahoo.com/v7/finance/download/$SYMBOL?period1=$START_DATE&period2=$END_DATE&interval=1d&events=history"
log $BASE_URL
# Add the crumb value
URL="$BASE_URL&crumb=$crumb"
log "URL: $URL"
# Download to
curl -s --cookie $cookieJar $URL # >$SYMBOL.csv
# echo "Data dowmloaded to $SYMBOL.csv"