From 5d57da5e1a602b1640252f896f374be7e10d1e87 Mon Sep 17 00:00:00 2001 From: Michael Fross Date: Fri, 7 Jan 2022 11:40:13 -0600 Subject: [PATCH] Had to replace cnbc with market watch for index data 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. --- pom.xml | 2 +- snap/snapcraft.yaml | 2 +- src/main/java/org/fross/quoter/Index.java | 34 +++++++++++++---------- src/main/java/org/fross/quoter/Main.java | 9 +++--- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index 8e0a558..51af688 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.fross quoter - 2.7.14 + 2.7.15 jar quoter diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index a6f3c86..a2abd33 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -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. diff --git a/src/main/java/org/fross/quoter/Index.java b/src/main/java/org/fross/quoter/Index.java index dcc0e25..0471437 100644 --- a/src/main/java/org/fross/quoter/Index.java +++ b/src/main/java/org/fross/quoter/Index.java @@ -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); @@ -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(); } } diff --git a/src/main/java/org/fross/quoter/Main.java b/src/main/java/org/fross/quoter/Main.java index d911931..c5e5a4a 100644 --- a/src/main/java/org/fross/quoter/Main.java +++ b/src/main/java/org/fross/quoter/Main.java @@ -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 @@ -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++) { @@ -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"); - } } }