Skip to content

Commit

Permalink
feat: skip duplicate comments and log them to a spreadsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
HamzaAburaneh committed Dec 10, 2024
1 parent 393fd88 commit d3a26ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
Binary file modified lib/PageSuccess-0.0.1-SNAPSHOT.jar
Binary file not shown.
51 changes: 42 additions & 9 deletions src/main/java/ca/gc/tbs/GoogleSheetsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
public class GoogleSheetsAPI {

static final String spreadsheetId = "1B16qEbfp7SFCfIsZ8fcj7DneCy1WkR0GPh4t9L9NRSg";
static final String range = "A1:A25000";
static final String duplicateCommentsSpreadsheetId = "1cR2mih5sBwl3wUjniwdyVA0xZcqV2Wl9yhghJfMG5oM"; // Template ID to
// be replaced
static final String range = "A1:A50000";
private static final String APPLICATION_NAME = "My Google Sheets Application";
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private static final String SERVICE_ACCOUNT_EMAIL = "[email protected]";
Expand All @@ -30,9 +32,9 @@ public class GoogleSheetsAPI {
private static NetHttpTransport HTTP_TRANSPORT;

public static void appendURL(String url) throws GeneralSecurityException, IOException {

KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(GoogleSheetsAPI.class.getClassLoader().getResourceAsStream("service-account.p12"), "notasecret".toCharArray());
keystore.load(GoogleSheetsAPI.class.getClassLoader().getResourceAsStream("service-account.p12"),
"notasecret".toCharArray());
PrivateKey pk = (PrivateKey) keystore.getKey("privatekey", "notasecret".toCharArray());

final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Expand All @@ -44,15 +46,13 @@ public static void appendURL(String url) throws GeneralSecurityException, IOExce
.setServiceAccountPrivateKey(pk)
.build();


Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();

ValueRange appendBody = new ValueRange()
.setValues(Arrays.asList(
Arrays.asList(url)
));
Arrays.asList(url)));
try {
AppendValuesResponse appendResult = service.spreadsheets().values()
.append(spreadsheetId, range, appendBody)
Expand All @@ -61,13 +61,46 @@ public static void appendURL(String url) throws GeneralSecurityException, IOExce
.setIncludeValuesInResponse(true)
.execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void appendDuplicateComment(String date, String timestamp, String url, String comment)
throws GeneralSecurityException, IOException {
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(GoogleSheetsAPI.class.getClassLoader().getResourceAsStream("service-account.p12"),
"notasecret".toCharArray());
PrivateKey pk = (PrivateKey) keystore.getKey("privatekey", "notasecret".toCharArray());

final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(SheetsScopes.SPREADSHEETS))
.setServiceAccountPrivateKey(pk)
.build();

Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();

ValueRange appendBody = new ValueRange()
.setValues(Arrays.asList(
Arrays.asList(date, timestamp, url, comment)));
try {
AppendValuesResponse appendResult = service.spreadsheets().values()
.append(duplicateCommentsSpreadsheetId, "A1:D50000", appendBody)
.setValueInputOption("USER_ENTERED")
.setInsertDataOption("INSERT_ROWS")
.setIncludeValuesInResponse(true)
.execute();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws GeneralSecurityException, IOException {
appendURL("test");
}

}
}
19 changes: 7 additions & 12 deletions src/main/java/ca/gc/tbs/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import org.springframework.data.mongodb.datatables.DataTablesRepositoryFactoryBean;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.io.*;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -398,20 +399,14 @@ public void autoTag() {

private void writeDuplicateToFile(String comment, String url, String date, String timeStamp) {
try {
File file = new File("duplicate_comments.txt");
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Date: " + date + "\n");
bw.write("Time (UTC): " + timeStamp + "\n");
bw.write("URL: " + url + "\n");
bw.write("Comment: " + comment + "\n");
bw.write("----------------------------------------\n");
bw.close();
} catch (IOException e) {
System.out.println("Error writing duplicate to file: " + e.getMessage());
GoogleSheetsAPI.appendDuplicateComment(date, timeStamp, url, comment);
} catch (Exception e) {
System.out.println("Error writing duplicate to spreadsheet: " + e.getMessage());
e.printStackTrace();
}
}


// Populates entries to the AirTable bases and Tier 2 spreadsheet (inventory).
@SuppressWarnings("unchecked")
public void airTableSpreadsheetSync() {
Expand Down

0 comments on commit d3a26ef

Please sign in to comment.