Skip to content

Commit

Permalink
Updates to exporter code.
Browse files Browse the repository at this point in the history
  • Loading branch information
frossm committed Dec 9, 2020
1 parent ccbc7d3 commit e5c4e48
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 67 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.1.1</version>
<version>2.1.2</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.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.
Expand Down
156 changes: 156 additions & 0 deletions src/main/java/org/fross/quoter/FileExporter.java
Original file line number Diff line number Diff line change
@@ -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<String> 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();
}

}
73 changes: 8 additions & 65 deletions src/main/java/org/fross/quoter/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -176,7 +160,6 @@ public static void main(String[] args) {
// Loop through each entered symbol and display it's data
Iterator<String> j = symbolList.iterator();
String currentSymbol = "";
boolean exportHeaderWritten = false;

while (j.hasNext()) {
currentSymbol = j.next();
Expand Down Expand Up @@ -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<String> 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);
}
}

Expand All @@ -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]);
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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
Expand Down

0 comments on commit e5c4e48

Please sign in to comment.