forked from mop-tracker/mop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yahoo_crumb.go
167 lines (142 loc) · 4.88 KB
/
yahoo_crumb.go
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
// Copyright (c) 2013-2024 by Michael Dvorkin and contributors. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.
package mop
import (
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
)
const crumbURL = "https://query1.finance.yahoo.com/v1/test/getcrumb"
const cookieURL = "https://finance.yahoo.com/"
const userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0"
const euConsentURL = "https://consent.yahoo.com/v2/collectConsent?sessionId="
func fetchCrumb(cookies string) string {
client := http.Client{}
request, err := http.NewRequest("GET", crumbURL, nil)
if err != nil {
panic(err)
}
request.Header = http.Header{
"Accept": {"*/*"},
"Accept-Encoding": {"gzip, deflate, br"},
"Accept-Language": {"en-US,en;q=0.5"},
"Connection": {"keep-alive"},
"Content-Type": {"text/plain"},
"Cookie": {cookies},
"Host": {"query1.finance.yahoo.com"},
"Sec-Fetch-Dest": {"empty"},
"Sec-Fetch-Mode": {"cors"},
"Sec-Fetch-Site": {"same-site"},
"TE": {"trailers"},
"User-Agent": {userAgent},
}
response, err := client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
return string(body[:])
}
func fetchCookies() string {
client := http.Client{}
var cookies []*http.Cookie
// Get the session ID from the first request
request, err := http.NewRequest("GET", cookieURL, nil)
if err != nil {
panic(err)
}
request.Header = http.Header{
"Authority": {"finance.yahoo.com"},
"Accept": {"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"},
"Accept-Encoding": {"gzip, deflate, br"},
"Accept-Language": {"en-US,en;q=0.9"},
"Sec-Fetch-Dest": {"document"},
"Sec-Fetch-Mode": {"navigate"},
"Sec-Fetch-Site": {"none"},
"Sec-Fetch-User": {"?1"},
"Upgrade-Insecure-Requests": {"1"},
"User-Agent": {userAgent},
}
response, err := client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
cookieA1 := getA1Cookie(response.Cookies())
if cookieA1 != "" {
return cookieA1
}
// first pass failed - try EU shenanigans
sessionRegex := regexp.MustCompile("sessionId=(?:([A-Za-z0-9_-]*))")
sessionID := sessionRegex.FindStringSubmatch(response.Request.URL.RawQuery)[1]
csrfRegex := regexp.MustCompile("gcrumb=(?:([A-Za-z0-9_]*))")
csrfToken := csrfRegex.FindStringSubmatch(response.Request.Response.Request.URL.RawQuery)[1]
gucsCookie := response.Request.Response.Request.Response.Cookies()
var gucsCookieString string = ""
for _, cookie := range gucsCookie {
gucsCookieString += cookie.Name + "=" + cookie.Value + "; "
}
gucsCookieString = strings.TrimSuffix(gucsCookieString, "; ")
if len(gucsCookie) == 0 {
panic(err)
}
// Create a new request to agree to the EU consent request
form := url.Values{}
form.Add("csrfToken", csrfToken)
form.Add("sessionId", sessionID)
form.Add("namespace", "yahoo")
form.Add("agree", "agree")
request2, err := http.NewRequest("POST", euConsentURL+sessionID, strings.NewReader(form.Encode()))
if err != nil {
panic(err)
}
contentLength := strconv.FormatInt(int64(len(form.Encode())), 10)
request2.Header = http.Header{
"Accept": {"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"},
"Accept-Encoding": {"gzip, deflate, br"},
"Accept-Language": {"en-US,en;q=0.9"},
"Connection": {"keep-alive"},
"Cookie": {gucsCookieString},
"Content-Length": {contentLength},
"Content-Type": {"application/x-www-form-urlencoded"},
"DNT": {"1"},
"Host": {"consent.yahoo.com"},
"Origin": {"https://consent.yahoo.com"},
"Referer": {euConsentURL + sessionID},
"Sec-Fetch-Dest": {"document"},
"Sec-Fetch-Mode": {"navigate"},
"Sec-Fetch-Site": {"same-origin"},
"Sec-Fetch-User": {"?1"},
"Upgrade-Insecure-Requests": {"1"},
"User-Agent": {userAgent},
}
response2, err := client.Do(request2)
if err != nil {
panic(err)
}
defer response2.Body.Close()
// redirect festival
cookies = response2.Request.Response.Request.Response.Request.Response.Cookies()
cookieA1 = getA1Cookie(cookies)
if cookieA1 != "" {
return cookieA1
} else {
panic(err)
}
}
func getA1Cookie(cookies []*http.Cookie) string {
for _, cookie := range cookies {
if cookie.Name == "A1" {
return cookie.Name + "=" + cookie.Value + "; "
}
}
return ""
}