Skip to content

Commit

Permalink
DB table name and attribute can be specified on command line:
Browse files Browse the repository at this point in the history
java -jar build/libs/email-parser-all-<VERSION>.jar <emails_folder_file> <DATABASE_NAME> <TABLE_NAME> <ATTRIBUTE_NAME> <DB_PORT>
  • Loading branch information
hermann77 committed Sep 23, 2018
1 parent 0dbbf07 commit deb6691
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'application'

mainClassName = 'de.schwarz.emailparser.ParserMain'

version = '0.3.0'
version = '0.4.0'


repositories {
Expand Down
35 changes: 32 additions & 3 deletions src/main/java/de/schwarz/emailparser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,44 @@ public List<String> readLines(Scanner input) {

List<String> emailAddresses = new ArrayList<String>();

String address = ""; // to save email address till we get smtp status code
String statusCode = "";

while(input.hasNextLine()) {
String nextLine = input.nextLine();
if(nextLine.contains("Final-Recipient")) { // line where to find E-Mail address
String address = extractEmailAddress(nextLine);
address = extractEmailAddress(nextLine);

emailAddresses.add(address);
System.out.println(address);
System.out.println("address to delete: " + address);
}
// only after we read diagnostic code
// and only on status codes: 550,
// we harves email address to later delete
else if(nextLine.contains("Diagnostic-Code")) {
// 550 - mailbox unavailable
// 511 - user unknown
// 520 - user unknown
// 553 - no such user
// 554 - no account with this address
// 552 - exceeded storage ==> don't delete for now
if(nextLine.contains("550") ||
nextLine.contains("511") ||
nextLine.contains("520") ||
nextLine.contains("553") ||
nextLine.contains("554") ||
nextLine.contains("Host or domain name not found")) {
// System.out.println("STATUS CODE: 550 or domain name not found");
emailAddresses.add(address);
}
else {
// System.out.println("STATUS CODE: " + nextLine);
}
}

}



input.close();

return emailAddresses;
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/de/schwarz/emailparser/ParserMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,42 @@ public static void main(String[] args) {
if(args.length == 1) {
parser.readLines(input);
}
// emails file AND DATABASE name passed in parameters
else if(args.length == 2) {
// emails file AND DATABASE name + table passed in parameters
else if(args.length == 4) {
String databaseName = args[1];
String tableName = args[2];
String attributeName = args[3];

List<String> emailAddressesToDelete = new ArrayList<String>();
emailAddressesToDelete = parser.readLines(input);

deleteEmailAddressesFromDB(databaseName, emailAddressesToDelete);
deleteEmailAddressesFromDB(databaseName, tableName, attributeName, emailAddressesToDelete);
}
// DATABASE name AND port passed in parameters
else if(args.length == 3) {
else if(args.length == 5) {
String databaseName = args[1];
String port = args[2];
String tableName = args[2];
String attributeName = args[3];
String port = args[4];

List<String> emailAddressesToDelete = new ArrayList<String>();
emailAddressesToDelete = parser.readLines(input);

deleteEmailAddressesFromDB(databaseName, emailAddressesToDelete, port);
deleteEmailAddressesFromDB(databaseName, tableName, attributeName, emailAddressesToDelete, port);

}
}

}


private static void deleteEmailAddressesFromDB(String databaseName, List<String> emailAddressesToDelete) {
private static void deleteEmailAddressesFromDB(String databaseName, String tableName, String attributeName, List<String> emailAddressesToDelete) {
// use standard port 3306 (standard port on MacOS: 3307)
deleteEmailAddressesFromDB(databaseName, emailAddressesToDelete, "3306");
deleteEmailAddressesFromDB(databaseName, tableName, attributeName, emailAddressesToDelete, "3306");
}


private static void deleteEmailAddressesFromDB(String databaseName, List<String> emailAddressesToDelete, String port) {
private static void deleteEmailAddressesFromDB(String databaseName, String tableName, String attributeName, List<String> emailAddressesToDelete, String port) {

Connection connection = DBConnection.getConnection(databaseName, port);
if(connection == null) {
Expand All @@ -77,7 +81,7 @@ private static void deleteEmailAddressesFromDB(String databaseName, List<String>
try {
//stmt = connection.createStatement();
for(String address : emailAddressesToDelete) {
stmt = connection.prepareStatement("DELETE FROM lid_subscribers WHERE nl_email IN (?)");
stmt = connection.prepareStatement("DELETE FROM " + tableName +" WHERE " + attributeName + " IN (?)");

stmt.setString(1, address);
int updateCount = stmt.executeUpdate();
Expand Down

0 comments on commit deb6691

Please sign in to comment.