-
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.
Implement SUB Football registration page handler
- Loading branch information
1 parent
7584bf3
commit 4b542dc
Showing
13 changed files
with
600 additions
and
1 deletion.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
load("@contrib_rules_jvm//java:defs.bzl", "JUNIT5_DEPS", "java_test_suite") | ||
load("@dagger//:workspace_defs.bzl", "dagger_rules") | ||
|
||
dagger_rules() | ||
|
||
HANDLERS = glob(["src/main/java/**/*Handler.java"]) | ||
|
||
INTEGRATION_TESTS = glob(["src/test/java/**/*IntegrationTest.java"]) | ||
|
||
java_library( | ||
name = "lib", | ||
srcs = glob( | ||
["src/main/java/**/*.java"], | ||
exclude = HANDLERS, | ||
), | ||
deps = [ | ||
":dagger", | ||
"//lib/dynamodb:lib", | ||
"//lib/notifications:lib", | ||
"//lib/time:lib", | ||
"@maven//:com_google_guava_guava", | ||
"@maven//:org_jsoup_jsoup", | ||
"@maven//:software_amazon_awssdk_dynamodb", | ||
"@maven//:software_amazon_awssdk_dynamodb_enhanced", | ||
], | ||
) | ||
|
||
java_binary( | ||
name = "update-page-content-handler", | ||
srcs = glob(["src/main/java/com/jordansimsmith/subfootballtracker/UpdatePageContentHandler.java"]), | ||
create_executable = False, | ||
resources = [ | ||
"src/main/resources/logback.xml", | ||
], | ||
deps = [ | ||
":lib", | ||
"//lib/notifications:lib", | ||
"//lib/time:lib", | ||
"@maven//:ch_qos_logback_logback_classic", | ||
"@maven//:ch_qos_logback_logback_core", | ||
"@maven//:com_amazonaws_aws_lambda_java_core", | ||
"@maven//:com_amazonaws_aws_lambda_java_events", | ||
"@maven//:com_google_code_findbugs_jsr305", | ||
"@maven//:com_google_guava_guava", | ||
"@maven//:software_amazon_awssdk_dynamodb_enhanced", | ||
], | ||
) | ||
|
||
java_library( | ||
name = "test-lib", | ||
srcs = glob( | ||
["src/test/java/**/*.java"], | ||
exclude = INTEGRATION_TESTS, | ||
), | ||
deps = [ | ||
":dagger", | ||
":lib", | ||
"//lib/dynamodb:lib", | ||
"//lib/notifications:lib", | ||
"//lib/time:lib", | ||
"@maven//:com_google_guava_guava", | ||
"@maven//:software_amazon_awssdk_dynamodb", | ||
"@maven//:software_amazon_awssdk_dynamodb_enhanced", | ||
], | ||
) | ||
|
||
java_test_suite( | ||
name = "integration-tests", | ||
size = "medium", | ||
srcs = INTEGRATION_TESTS, | ||
env = { | ||
"AWS_ACCESS_KEY_ID": "fake", | ||
"AWS_SECRET_ACCESS_KEY": "fake", | ||
"AWS_REGION": "ap-southeast-2", | ||
}, | ||
runner = "junit5", | ||
test_suffixes = ["IntegrationTest.java"], | ||
runtime_deps = JUNIT5_DEPS, | ||
deps = [ | ||
":lib", | ||
":test-lib", | ||
":update-page-content-handler", | ||
"//lib/dynamodb:lib", | ||
"//lib/notifications:lib", | ||
"//lib/testcontainers:lib", | ||
"//lib/time:lib", | ||
"@maven//:com_amazonaws_aws_lambda_java_core", | ||
"@maven//:com_amazonaws_aws_lambda_java_events", | ||
"@maven//:org_assertj_assertj_core", | ||
"@maven//:org_junit_jupiter_junit_jupiter_api", | ||
"@maven//:org_testcontainers_junit_jupiter", | ||
"@maven//:software_amazon_awssdk_dynamodb", | ||
"@maven//:software_amazon_awssdk_dynamodb_enhanced", | ||
], | ||
) |
26 changes: 26 additions & 0 deletions
26
...acker_api/src/main/java/com/jordansimsmith/subfootballtracker/JsoupSubfootballClient.java
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.jordansimsmith.subfootballtracker; | ||
|
||
import com.google.common.base.Preconditions; | ||
import org.jsoup.Jsoup; | ||
|
||
public class JsoupSubfootballClient implements SubfootballClient { | ||
@Override | ||
public String getRegistrationContent() { | ||
try { | ||
return doGetRegistrationContent(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private String doGetRegistrationContent() throws Exception { | ||
var doc = Jsoup.connect("https://subfootball.com/register").get(); | ||
|
||
var content = doc.selectFirst(".page.content-item"); | ||
Preconditions.checkNotNull(content); | ||
|
||
content.select("br").before("\\n"); | ||
content.select("p").before("\\n"); | ||
return content.text().replaceAll(" *\\\\n *", "\n").trim(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ll_tracker_api/src/main/java/com/jordansimsmith/subfootballtracker/SubfootballClient.java
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.jordansimsmith.subfootballtracker; | ||
|
||
public interface SubfootballClient { | ||
String getRegistrationContent(); | ||
} |
32 changes: 32 additions & 0 deletions
32
...er_api/src/main/java/com/jordansimsmith/subfootballtracker/SubfootballTrackerFactory.java
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.jordansimsmith.subfootballtracker; | ||
|
||
import com.jordansimsmith.dynamodb.DynamoDbModule; | ||
import com.jordansimsmith.lib.notifications.NotificationModule; | ||
import com.jordansimsmith.lib.notifications.NotificationPublisher; | ||
import com.jordansimsmith.time.Clock; | ||
import com.jordansimsmith.time.ClockModule; | ||
import dagger.Component; | ||
import javax.inject.Singleton; | ||
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; | ||
|
||
@Singleton | ||
@Component( | ||
modules = { | ||
ClockModule.class, | ||
NotificationModule.class, | ||
DynamoDbModule.class, | ||
SubfootballTrackerModule.class | ||
}) | ||
public interface SubfootballTrackerFactory { | ||
Clock clock(); | ||
|
||
NotificationPublisher notificationPublisher(); | ||
|
||
DynamoDbTable<SubfootballTrackerItem> subfootballTrackerTable(); | ||
|
||
SubfootballClient subfootballClient(); | ||
|
||
static SubfootballTrackerFactory create() { | ||
return DaggerSubfootballTrackerFactory.create(); | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
...acker_api/src/main/java/com/jordansimsmith/subfootballtracker/SubfootballTrackerItem.java
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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package com.jordansimsmith.subfootballtracker; | ||
|
||
import java.util.Objects; | ||
import software.amazon.awssdk.enhanced.dynamodb.extensions.annotations.DynamoDbVersionAttribute; | ||
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbAttribute; | ||
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean; | ||
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey; | ||
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey; | ||
|
||
@DynamoDbBean | ||
public class SubfootballTrackerItem { | ||
public enum Page { | ||
REGISTRATION | ||
} | ||
|
||
public static final String DELIMITER = "#"; | ||
public static final String PAGE_PREFIX = "PAGE" + DELIMITER; | ||
public static final String TIMESTAMP_PREFIX = "TIMESTAMP" + DELIMITER; | ||
|
||
private static final String PK = "pk"; | ||
private static final String SK = "sk"; | ||
private static final String PAGE = "page"; | ||
private static final String TIMESTAMP = "timestamp"; | ||
private static final String CONTENT = "content"; | ||
private static final String VERSION = "version"; | ||
|
||
private String pk; | ||
private String sk; | ||
private Page page; | ||
private Long timestamp; | ||
private String content; | ||
private Long version; | ||
|
||
@DynamoDbPartitionKey | ||
@DynamoDbAttribute(PK) | ||
public String getPk() { | ||
return pk; | ||
} | ||
|
||
public void setPk(String pk) { | ||
this.pk = pk; | ||
} | ||
|
||
@DynamoDbSortKey | ||
@DynamoDbAttribute(SK) | ||
public String getSk() { | ||
return sk; | ||
} | ||
|
||
public void setSk(String sk) { | ||
this.sk = sk; | ||
} | ||
|
||
@DynamoDbAttribute(PAGE) | ||
public Page getPage() { | ||
return page; | ||
} | ||
|
||
public void setPage(Page page) { | ||
this.page = page; | ||
} | ||
|
||
@DynamoDbAttribute(TIMESTAMP) | ||
public Long getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public void setTimestamp(Long timestamp) { | ||
this.timestamp = timestamp; | ||
} | ||
|
||
@DynamoDbAttribute(CONTENT) | ||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
|
||
@DynamoDbVersionAttribute | ||
@DynamoDbAttribute(VERSION) | ||
public Long getVersion() { | ||
return version; | ||
} | ||
|
||
public void setVersion(Long version) { | ||
this.version = version; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SubfootballTrackerItem{" | ||
+ "pk='" | ||
+ pk | ||
+ '\'' | ||
+ ", sk='" | ||
+ sk | ||
+ '\'' | ||
+ ", page='" | ||
+ page | ||
+ '\'' | ||
+ ", timestamp=" | ||
+ timestamp | ||
+ ", content='" | ||
+ content | ||
+ '\'' | ||
+ ", version=" | ||
+ version | ||
+ '}'; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) return false; | ||
SubfootballTrackerItem that = (SubfootballTrackerItem) o; | ||
return Objects.equals(pk, that.pk) | ||
&& Objects.equals(sk, that.sk) | ||
&& Objects.equals(page, that.page) | ||
&& Objects.equals(timestamp, that.timestamp) | ||
&& Objects.equals(content, that.content) | ||
&& Objects.equals(version, that.version); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(pk, sk, page, timestamp, content, version); | ||
} | ||
|
||
public static String formatPk(Page page) { | ||
return PAGE_PREFIX + page; | ||
} | ||
|
||
public static String formatSk(long timestamp) { | ||
return TIMESTAMP_PREFIX + timestamp; | ||
} | ||
|
||
public static SubfootballTrackerItem create(Page page, long timestamp, String content) { | ||
var subfootballTrackerItem = new SubfootballTrackerItem(); | ||
subfootballTrackerItem.setPk(formatPk(page)); | ||
subfootballTrackerItem.setSk(formatSk(timestamp)); | ||
subfootballTrackerItem.setContent(content); | ||
subfootballTrackerItem.setTimestamp(timestamp); | ||
subfootballTrackerItem.setPage(page); | ||
return subfootballTrackerItem; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ker_api/src/main/java/com/jordansimsmith/subfootballtracker/SubfootballTrackerModule.java
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.jordansimsmith.subfootballtracker; | ||
|
||
import dagger.Module; | ||
import dagger.Provides; | ||
import javax.inject.Singleton; | ||
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; | ||
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; | ||
import software.amazon.awssdk.enhanced.dynamodb.TableSchema; | ||
|
||
@Module | ||
public class SubfootballTrackerModule { | ||
@Provides | ||
@Singleton | ||
public DynamoDbTable<SubfootballTrackerItem> subfootballTrackerTable( | ||
DynamoDbEnhancedClient dynamoDbEnhancedClient) { | ||
var schema = TableSchema.fromBean(SubfootballTrackerItem.class); | ||
return dynamoDbEnhancedClient.table("subfootball_tracker", schema); | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
public SubfootballClient subfootballClient() { | ||
return new JsoupSubfootballClient(); | ||
} | ||
} |
Oops, something went wrong.