-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: skip duplicate comments and log them to a spreadsheet
- Loading branch information
1 parent
393fd88
commit d3a26ef
Showing
3 changed files
with
49 additions
and
21 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]"; | ||
|
@@ -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(); | ||
|
@@ -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) | ||
|
@@ -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"); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters