Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

append the crumb to the URL (Fix #206, #209, #211 and #225) #210

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.idea
/*.iml
/classes/
/.java-version
40 changes: 35 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
</properties>

<distributionManagement>
Expand Down Expand Up @@ -113,7 +112,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<version>3.3.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -123,7 +122,38 @@
</execution>
</executions>
</plugin>
</plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<!--filesets>
<fileset>
<directory>${basedir}/shade</directory>
</fileset>
</filesets-->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>${maven.compiler.release}</release>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<trimStackTrace>false</trimStackTrace>
<useFile>false</useFile>
<!--forkCount>0</forkCount-->
</configuration>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
Expand Down Expand Up @@ -274,4 +304,4 @@
</profile>
</profiles>

</project>
</project>
8 changes: 6 additions & 2 deletions src/main/java/yahoofinance/histquotes2/CrumbManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ private static void setCookie() throws IOException {
redirectableRequest.setConnectTimeout(YahooFinance.CONNECTION_TIMEOUT);
redirectableRequest.setReadTimeout(YahooFinance.CONNECTION_TIMEOUT);

Map<String, String> requestProperties = new HashMap<String, String>();
requestProperties.put("Cookie", cookie);
requestProperties.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.2 Safari/605.1.15");

URLConnection connection = redirectableRequest.openConnection();
URLConnection connection = redirectableRequest.openConnection(requestProperties);

for(String headerKey : connection.getHeaderFields().keySet()) {
if("Set-Cookie".equalsIgnoreCase(headerKey)) {
for(String cookieField : connection.getHeaderFields().get(headerKey)) {
for(String cookieValue : cookieField.split(";")) {
if(cookieValue.matches("B=.*")) {
if(cookieValue.matches("A1S=.*")) {
cookie = cookieValue;
log.debug("Set cookie from http request: {}", cookie);
return;
Expand Down Expand Up @@ -162,6 +165,7 @@ private static void setCrumb() throws IOException {

Map<String, String> requestProperties = new HashMap<String, String>();
requestProperties.put("Cookie", cookie);
requestProperties.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5.2 Safari/605.1.15");

URLConnection crumbConnection = redirectableCrumbRequest.openConnection(requestProperties);
InputStreamReader is = new InputStreamReader(crumbConnection.getInputStream());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import yahoofinance.histquotes2.CrumbManager;

/**
*
Expand Down Expand Up @@ -61,7 +62,9 @@ public List<T> getResult() throws IOException {
params.put("symbols", this.symbols);

String url = YahooFinance.QUOTES_QUERY1V7_BASE_URL + "?" + Utils.getURLParameters(params);

if (!CrumbManager.getCrumb().isEmpty()) {
url = url + "&crumb=" + CrumbManager.getCrumb();
}
// Get JSON from Yahoo
log.info("Sending request: " + url);

Expand Down
19 changes: 19 additions & 0 deletions src/test/java/yahoofinance/CrumbManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package yahoofinance;

import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import java.io.IOException;
import yahoofinance.histquotes2.CrumbManager;

/**
*
* @author gilberto.andrade
*/
public class CrumbManagerTest {

@Test
public void getCrumbTest() throws IOException {
assertNotNull(CrumbManager.getCrumb());
}

}
19 changes: 19 additions & 0 deletions src/test/java/yahoofinance/QuoteRequestTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package yahoofinance;

import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;

/**
*
* @author gilberto.andrade
*/
public class QuoteRequestTest {

@Test
public void getQuoteTest() throws IOException {
Stock stock = YahooFinance.get("INTC");
assertEquals(stock.getStats().getSymbol(), "INTC");

}
}