From e5c4e4862a898dba892f543ad334791dfb8d6c12 Mon Sep 17 00:00:00 2001 From: Michael Fross Date: Tue, 8 Dec 2020 21:00:34 -0600 Subject: [PATCH] Updates to exporter code. --- pom.xml | 2 +- snap/snapcraft.yaml | 2 +- .../java/org/fross/quoter/FileExporter.java | 156 ++++++++++++++++++ src/main/java/org/fross/quoter/Main.java | 73 +------- 4 files changed, 166 insertions(+), 67 deletions(-) create mode 100644 src/main/java/org/fross/quoter/FileExporter.java diff --git a/pom.xml b/pom.xml index fd0fad9..d573066 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.fross quoter - 2.1.1 + 2.1.2 jar quoter diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 7fb5911..3ea67ef 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: quoter -version: '2.1.1' +version: '2.1.2' 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/FileExporter.java b/src/main/java/org/fross/quoter/FileExporter.java new file mode 100644 index 0000000..c7a96a3 --- /dev/null +++ b/src/main/java/org/fross/quoter/FileExporter.java @@ -0,0 +1,156 @@ +/************************************************************************************************************** + * Quoter.jar + * + * Quoter is a command line program that display stock quotes and index data. + * + * Copyright (c) 2019 Michael Fross + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + ***************************************************************************************************************/ +package org.fross.quoter; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.List; + +import org.fross.library.Output; +import org.fusesource.jansi.Ansi; + +public class FileExporter { + File exportFile = null; + FileWriter exportFileFW = null; + boolean exportSymbolHeaderWritten = false; + boolean exportIndexHeaderWritten = false; + + /** + * FileExporter(): FileExporter Constructor + * + * @param fileName + */ + public FileExporter(String fileName) { + // Create the export file + try { + this.exportFile = new File(fileName); + if (this.exportFile.createNewFile() == false) { + Output.fatalError("Could not create export file: '" + fileName + "'", 4); + } + } catch (IOException ex) { + Output.fatalError("Could not create export file: '" + fileName + "'", 4); + } + + // Create the FileWriter object for writing to the export file + try { + exportFileFW = new FileWriter(this.exportFile); + } catch (IOException ex) { + Output.printColorln(Ansi.Color.RED, "Error writing to export file '" + fileName + "'\n" + ex.getMessage()); + } + } + + /** + * canWrite(): Return true or false depending on the ability to write to the export file + * + * @return + */ + public boolean canWrite() { + try { + return exportFile.canWrite(); + } catch (Exception ex) { + return false; + } + } + + /** + * exportSecurities(): Dump the security symbol data to the export file + * + * @param symbolData + * @return + */ + public boolean exportSecurities(Symbol symbolData) { + try { + // Export the header row + List fields = symbolData.queryAllFieldNames(); + if (this.exportSymbolHeaderWritten == false) { + for (String i : fields) { + exportFileFW.append(i + ","); + } + exportFileFW.append("\n"); + this.exportSymbolHeaderWritten = true; + } + + // Export the symbol data + for (String i : fields) { + // If the data has a ',' in it remove it + String item = symbolData.query(i).replaceAll(",", ""); + exportFileFW.append(item + ","); + } + exportFileFW.append("\n"); + + } catch (IOException ex) { + Output.printColorln(Ansi.Color.RED, "Error writing data to the export file: " + ex.getMessage()); + } + + return true; + } + + /** + * exportIndexes(): Dump the index data to the export file + */ + public void exportIndexes(String[] indexData) { + try { + // Dump the header information to the export file + if (this.exportIndexHeaderWritten == false) { + exportFileFW.append("\nSymbol,Current,Chng,Chng%\n"); + this.exportIndexHeaderWritten = true; + } + + // Dump the index data + for (int k = 0; k < indexData.length; k++) { + exportFileFW.append(indexData[k] + ","); + } + exportFileFW.append("\n"); + + } catch (IOException ex) { + Output.printColorln(Ansi.Color.RED, "Error writing to export file: " + ex.getMessage()); + } + } + + /** + * close(): Flush and close the export file + */ + public void close() { + try { + this.exportFileFW.flush(); + this.exportFileFW.close(); + } catch (IOException ex) { + Output.printColorln(Ansi.Color.RED, "Error closing export file: " + ex.getMessage()); + } + + } + + /** + * queryExportFilename(): Return the name of the export file as a string + * @return + */ + public String queryExportFilename() { + return this.exportFile.toString(); + } + +} diff --git a/src/main/java/org/fross/quoter/Main.java b/src/main/java/org/fross/quoter/Main.java index 619af97..5f63eb7 100644 --- a/src/main/java/org/fross/quoter/Main.java +++ b/src/main/java/org/fross/quoter/Main.java @@ -27,13 +27,10 @@ package org.fross.quoter; -import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; -import java.util.List; import java.util.Properties; import java.util.Scanner; @@ -57,10 +54,9 @@ public static void main(String[] args) { int optionEntry; String iexCloudToken; String latestTime = "None"; - File exportFile = null; - FileWriter exportFileFW = null; boolean exportFlag = false; boolean trendFlag = false; + FileExporter exporter = null; // Process application level properties file // Update properties from Maven at build time: @@ -103,20 +99,8 @@ public static void main(String[] args) { // Export Data case 'x': - try { - exportFile = new File(optG.getOptarg()); - if (exportFile.createNewFile() == false) { - Output.fatalError("Could not create file: '" + exportFile + "'", 4); - } - } catch (IOException ex) { - Output.fatalError("Could not create file: '" + exportFile + "'", 4); - } exportFlag = true; - try { - exportFileFW = new FileWriter(exportFile); - } catch (IOException ex) { - Output.printColorln(Ansi.Color.RED, "Error writing to export file: " + ex.getMessage()); - } + exporter = new FileExporter(optG.getOptarg()); break; // Display configured IEXCloud Secret Key @@ -176,7 +160,6 @@ public static void main(String[] args) { // Loop through each entered symbol and display it's data Iterator j = symbolList.iterator(); String currentSymbol = ""; - boolean exportHeaderWritten = false; while (j.hasNext()) { currentSymbol = j.next(); @@ -281,27 +264,8 @@ public static void main(String[] args) { Output.println(""); // If export is chosen, dump this security's data to the export file - if (exportFlag == true && exportFile.canWrite()) { - try { - // Export the header row - List fields = symbolData.queryAllFieldNames(); - if (exportHeaderWritten == false) { - for (String i : fields) { - exportFileFW.append(i + ","); - } - exportFileFW.append("\n"); - exportHeaderWritten = true; - } - - // Export the symbol data - for (String i : fields) { - exportFileFW.append(symbolData.query(i) + ","); - } - exportFileFW.append("\n"); - - } catch (IOException ex) { - Output.printColorln(Ansi.Color.RED, "Error writing to export file: " + ex.getMessage()); - } + if (exportFlag == true && exporter.canWrite()) { + exporter.exportSecurities(symbolData); } } @@ -316,7 +280,6 @@ public static void main(String[] args) { // Loop through the three indexes and display the results String[] indexList = { "DOW", "NASDAQ", "S&P" }; try { - boolean exportHeaderWritten = false; for (int i = 0; i < indexList.length; i++) { // Download the web page and return the results array Output.debugPrint("Getting Index data for: " + indexList[i]); @@ -354,24 +317,8 @@ public static void main(String[] args) { Output.println(""); // If export is chosen, dump this index's data to the export file - if (exportFlag == true && exportFile.canWrite()) { - try { - - // Dump the header information to the export file - if (exportHeaderWritten == false) { - exportFileFW.append("\nSymbol,Current,Chng,Chng%\n"); - exportHeaderWritten = true; - } - - // Dump the index data - for (int k = 0; k < result.length; k++) { - exportFileFW.append(result[k] + ","); - } - exportFileFW.append("\n"); - - } catch (IOException ex) { - Output.printColorln(Ansi.Color.RED, "Error writing to export file: " + ex.getMessage()); - } + if (exportFlag == true && exporter.canWrite()) { + exporter.exportIndexes(result); } } @@ -389,12 +336,8 @@ public static void main(String[] args) { // Flush and close export file if needed if (exportFlag == true) { - try { - exportFileFW.flush(); - exportFileFW.close(); - } catch (IOException ex) { - Output.printColorln(Ansi.Color.RED, "Error closing export file: " + ex.getMessage()); - } + exporter.close(); + Output.printColor(Ansi.Color.CYAN, "\nData Export Complete to '" + exporter.queryExportFilename() + "'\n"); } // Display trending data if -t was provided and there is at least one symbol