Skip to content

Commit

Permalink
Implement SUB Football registration page handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jordansimsmith committed Nov 14, 2024
1 parent 7584bf3 commit 4b542dc
Show file tree
Hide file tree
Showing 13 changed files with 600 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class PriceTrackerTestModule {
@Provides
@Singleton
public DynamoDbTable<PriceTrackerItem> immersionTrackerTable(
public DynamoDbTable<PriceTrackerItem> priceTrackerTable(
DynamoDbEnhancedClient dynamoDbEnhancedClient) {
var schema = TableSchema.fromBean(PriceTrackerItem.class);
return dynamoDbEnhancedClient.table("price_tracker", schema);
Expand Down
95 changes: 95 additions & 0 deletions subfootball_tracker_api/BUILD.bazel
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",
],
)
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.jordansimsmith.subfootballtracker;

public interface SubfootballClient {
String getRegistrationContent();
}
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();
}
}
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;
}
}
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();
}
}
Loading

0 comments on commit 4b542dc

Please sign in to comment.