Skip to content

Commit

Permalink
Had to replace cnbc with market watch for index data
Browse files Browse the repository at this point in the history
CNBC changed their format and I can no longer easily pull the index data
from them.  Switched to MarketWatch and we'll see if that's any better.
Downside is that I can't get 52 week high and low from them.  Need to
keep looking for a better source.
  • Loading branch information
frossm committed Jan 7, 2022
1 parent 566d763 commit 5d57da5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.fross</groupId>
<artifactId>quoter</artifactId>
<version>2.7.14</version>
<version>2.7.15</version>
<packaging>jar</packaging>

<name>quoter</name>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: quoter
version: '2.7.14'
version: '2.7.15'
summary: Command line utility to pull stock and index quotes
description: |
Quote fetches stock quotes and index data from IEXCloud.IO.
Expand Down
34 changes: 20 additions & 14 deletions src/main/java/org/fross/quoter/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,29 @@

public class Index {
/**
* GetIndex: Returns an array of Strings that contains the Dow, Nasdaq, and S&P data. Unfortunately
* I have to scrape a web page for this information as IEX Cloud does not contain index data.
* GetIndex: Returns an array of Strings that contains the Dow, Nasdaq, and S&P data. Unfortunately I have to scrape a
* web page for this information as IEX Cloud does not contain index data.
*
* @param idx
* @return
*/
protected static String[] getIndex(String idx) {
String[] retArray = new String[6];
String[] retArray = new String[4];
String idxPage;
String URLTEMPLATE = "https://www.cnbc.com/quotes/?symbol=SYMBOLHERE";
String URLTEMPLATE = "https://www.marketwatch.com/investing/index/SYMBOLHERE";
String URL = "ERROR";
String[] searchPatterns = new String[6];
String[] searchPatterns = new String[4];

// Ensure a valid value was passed
switch (idx.toUpperCase()) {
case "DOW":
URL = URLTEMPLATE.replaceAll("SYMBOLHERE", ".dji");
URL = URLTEMPLATE.replaceAll("SYMBOLHERE", "djia");
break;
case "NASDAQ":
URL = URLTEMPLATE.replaceAll("SYMBOLHERE", ".ixic");
URL = URLTEMPLATE.replaceAll("SYMBOLHERE", "comp");
break;
case "S&P":
URL = URLTEMPLATE.replaceAll("SYMBOLHERE", ".inx");
URL = URLTEMPLATE.replaceAll("SYMBOLHERE", "spx");
break;
default:
Output.fatalError("Call to getIndex() must be 'DOW', 'NASDAQ', or 'S&P'", 4);
Expand All @@ -72,18 +72,24 @@ protected static String[] getIndex(String idx) {
idxPage = URLOperations.ReadURL(URL);

// Define the regular expression patterns to look for in the URL provided above
searchPatterns[1] = "\"last\":\"(.*?)\"";
searchPatterns[2] = "\"change\":\"(.*?)\"";
searchPatterns[3] = "\"change_pct\":\"(.*?)\"";
searchPatterns[4] = "QuoteStrip-fiftyTwoWeekRange\"\\>(.*?)\\<";
searchPatterns[5] = "QuoteStrip-fiftyTwoWeekRange.*? - .*?\\>(.*?)\\<";
// Current Price
searchPatterns[1] = "\"price\"\\s+content=\"(.*?)\"";
// Change
searchPatterns[2] = "\"priceChange\"\\s+content=\"(.*?)\"";
// Change Percent
searchPatterns[3] = "\"priceChangePercent\"\\s+content=\"(.*?)\"";
// 52Week Low
// searchPatterns[4] = "0";
// 52Week High
// searchPatterns[5] = "0";

retArray[0] = idx;
for (int i = 1; i < searchPatterns.length; i++) {
Pattern pat = Pattern.compile(searchPatterns[i]);
Matcher m = pat.matcher(idxPage);
if (m.find()) {
retArray[i] = m.group(1).trim();
// Remove any commas / percent signs and return
retArray[i] = m.group(1).replaceAll("%", "").trim();
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/fross/quoter/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public static void main(String[] args) {
// Loop through the three indexes and display the results
String[] indexList = { "DOW", "NASDAQ", "S&P" };
for (int i = 0; i < indexList.length; i++) {
String[] outString = new String[6];
String[] outString = new String[4];
String[] result = Index.getIndex(indexList[i]);
try {
// Download the web page and return the results array
Expand All @@ -446,9 +446,9 @@ public static void main(String[] args) {
// Change Percentage
outString[3] = String.format("%+,10.2f%%", Float.valueOf(result[3].replace("%", "")));
// 52Week High
outString[4] = String.format("%,14.2f", Float.valueOf(result[4].replace(",", "")));
// outString[4] = String.format("%,14.2f", Float.valueOf(result[4].replace(",", "")));
// 52Week Low
outString[5] = String.format("%,14.2f", Float.valueOf(result[5].replace(",", "")));
// outString[5] = String.format("%,14.2f", Float.valueOf(result[5].replace(",", "")));

// Display Index results to the screen
for (int k = 0; k < outString.length; k++) {
Expand All @@ -462,9 +462,8 @@ public static void main(String[] args) {
if (exportFlag == true && exporter.canWrite()) {
exporter.exportIndexes(result);
}
} catch (Exception Ex) {
} catch (Exception ex) {
Output.printColorln(Ansi.Color.RED, outString[0] + ": No Data");

}
}
}
Expand Down

0 comments on commit 5d57da5

Please sign in to comment.