Skip to content

Commit

Permalink
Merge pull request #33 from This-Is-Ko/imageGenConfigs
Browse files Browse the repository at this point in the history
Image gen configs
  • Loading branch information
This-Is-Ko authored Oct 20, 2024
2 parents 9e9ce2a + 8438540 commit a33c998
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 25 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/ko/footballupdater/models/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,23 @@ public Post(PostType postType, Player player, PlayerMatchPerformanceStats player
this.postType = postType;
this.player = player;
this.playerMatchPerformanceStats = playerMatchPerformanceStats;
imagesUrls = new ArrayList<>();
}

public Post(Integer id, boolean postedStatus) {
this.id = id;
this.postedStatus = postedStatus;
imagesUrls = new ArrayList<>();
}

public Post(Player player, List<String> imagesUrls) {
this.player = player;
this.imagesUrls = imagesUrls;
this.postedStatus = false;
}

public List<String> getImagesUrls() {
if (imagesUrls == null) {
imagesUrls = new ArrayList<>();
}
return imagesUrls;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import com.ko.footballupdater.configuration.TeamProperties;
import com.ko.footballupdater.models.ImageStatEntry;
import com.ko.footballupdater.models.PlayerMatchPerformanceStats;
import com.ko.footballupdater.models.PostType;
import com.ko.footballupdater.models.Post;
import com.ko.footballupdater.models.PostType;
import com.ko.footballupdater.models.Team;
import com.ko.footballupdater.models.form.HorizontalTranslation;
import com.ko.footballupdater.models.form.ImageGenParams;
import com.ko.footballupdater.models.form.StatisticEntryGenerateDto;
import com.ko.footballupdater.models.form.VerticalTranslation;
import com.ko.footballupdater.repositories.TeamRepository;
import com.ko.footballupdater.utils.DateTimeHelper;
import com.ko.footballupdater.utils.LogHelper;
import com.ko.footballupdater.utils.PostHelper;
Expand All @@ -22,7 +21,13 @@
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -51,6 +56,7 @@ public class ImageGeneratorService {

@Autowired
private TeamHelpers teamHelpers;

@Autowired
private ImageGeneratorProperties imageGeneratorProperties;

Expand Down Expand Up @@ -351,6 +357,7 @@ public void generateStandoutStatsImage(Post post, List<StatisticEntryGenerateDto
String playerImageBaseFilePath = imageGeneratorProperties.getInputPath() + post.getPlayer().getName().replaceAll(" ", "") + STANDOUT_BASE_IMAGE_FILE_NAME;
image = loadImage(playerImageBaseFilePath);
}
log.atDebug().setMessage("Prepared STANDOUT_STATS_POST image object").addKeyValue("player", post.getPlayer().getName()).log();

// Add player name
Font playerNameFont = new Font("Wagner Modern", Font.PLAIN, 50);
Expand All @@ -376,14 +383,15 @@ public void generateStandoutStatsImage(Post post, List<StatisticEntryGenerateDto
Font matchDateFont = new Font("Chakra Petch", Font.BOLD, 20);
drawXCenteredText(image, matchDateFont, matchDateString, image.getHeight() - 30);
}
log.atDebug().setMessage("Added text to STANDOUT_STATS_POST image").addKeyValue("player", post.getPlayer().getName()).log();

// Save the modified image
saveImage(post, image, generateFileName(post, 1, PostType.STANDOUT_STATS_POST), 1);
} catch (IOException ex) {
log.atWarn().setMessage("Unable to find/read image file").setCause(ex).addKeyValue("player", post.getPlayer().getName()).log();
log.atWarn().setMessage("Unable to find/read image file").addKeyValue("player", post.getPlayer().getName()).log();
throw new Exception(post.getPlayer().getName() + " - Unable to find/read image file ", ex);
} catch (Exception ex) {
log.atWarn().setMessage("Error while generating standout stat image").setCause(ex).addKeyValue("player", post.getPlayer().getName()).log();
log.atWarn().setMessage("Error while generating standout stat image").addKeyValue("player", post.getPlayer().getName()).log();
throw new Exception(post.getPlayer().getName() + " - Error while generating stat image ", ex);
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/com/ko/footballupdater/services/PlayerService.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.ko.footballupdater.services;

import com.amazonaws.services.kms.model.NotFoundException;
import com.ko.footballupdater.configuration.ImageGeneratorProperties;
import com.ko.footballupdater.configuration.InstagramPostProperies;
import com.ko.footballupdater.models.AlternativePlayerName;
import com.ko.footballupdater.models.CheckedStatus;
import com.ko.footballupdater.models.DataSource;
import com.ko.footballupdater.models.DataSourceSiteName;
import com.ko.footballupdater.models.DataSourceType;
import com.ko.footballupdater.models.Hashtag;
import com.ko.footballupdater.models.Player;
import com.ko.footballupdater.models.PlayerMatchPerformanceStats;
import com.ko.footballupdater.models.Post;
import com.ko.footballupdater.models.PostType;
import com.ko.footballupdater.models.Team;
import com.ko.footballupdater.repositories.PlayerRepository;
import com.ko.footballupdater.repositories.PostRepository;
import com.ko.footballupdater.repositories.TeamRepository;
import com.ko.footballupdater.request.RequestDataSource;
import com.ko.footballupdater.request.UpdatePlayerRequest;
import com.ko.footballupdater.responses.UpdatePlayersResponse;
Expand All @@ -40,9 +38,6 @@ public class PlayerService {
@Autowired
private PlayerRepository playerRepository;

@Autowired
private TeamRepository teamRepository;

@Autowired
private PostRepository postRepository;

Expand All @@ -64,6 +59,9 @@ public class PlayerService {
@Autowired
private InstagramPostProperies instagramPostProperies;

@Autowired
private ImageGeneratorProperties imageGeneratorProperties;

public Player addPlayer(Player newPlayer, DataSourceSiteName dataSourceSiteName) throws IllegalArgumentException {
if (!playerRepository.findByNameEquals(newPlayer.getName()).isEmpty()) {
throw new IllegalArgumentException("Player already exists");
Expand Down Expand Up @@ -148,10 +146,12 @@ public UpdatePlayersResponse updateDataForPlayers(List<Player> requestPlayersToU
// Generate post and caption
Post post = new Post(PostType.ALL_STAT_POST, player, playerMatchPerformanceStats);
try {
// Generate stat images
imageGeneratorService.generatePlayerStatImage(post);
// Upload stat images to s3
amazonS3Service.uploadToS3(post);
if (imageGeneratorProperties.isEnabled()) {
// Generate stat images
imageGeneratorService.generatePlayerStatImage(post);
// Upload stat images to s3
amazonS3Service.uploadToS3(post);
}
// Generate any additional team hashtags
String hashtags = "";
if (post.getPlayerMatchPerformanceStats().getMatch().getRelevantTeam() != null && !post.getPlayerMatchPerformanceStats().getMatch().getRelevantTeam().isEmpty()) {
Expand Down Expand Up @@ -198,6 +198,7 @@ public UpdatePlayersResponse updateDataForPlayers(List<Player> requestPlayersToU
}
playerRepository.saveAll(playersToUpdate);
log.atInfo().setMessage("Updated player entries with latest data").log();

// Populate response
response.setPlayersUpdated(playersToUpdate);
response.setNumPlayersUpdated(playersToUpdate.size());
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ tiktok.api.clientKey=${TIKTOK_API_CLIENT_KEY}
tiktok.api.clientSecret=${TIKTOK_API_CLIENT_SECRET}
tiktok.api.scope=${TIKTOK_API_SCOPE}
tiktok.api.redirectUri=${TIKTOK_API_REDIRECT_URI}
tiktok.api.privacyLevel=${TIKTOK_API_PRIVACY_LEVEL}

# Team
team.nameSuffixesToRemove=${TEAM_NAME_SUFFIXES_TO_REMOVE}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.ko.footballupdater.unit.services;

import com.amazonaws.services.kms.model.NotFoundException;
import com.ko.footballupdater.configuration.ImageGeneratorProperties;
import com.ko.footballupdater.configuration.InstagramPostProperies;
import com.ko.footballupdater.models.CheckedStatus;
import com.ko.footballupdater.models.Hashtag;
import com.ko.footballupdater.models.Match;
import com.ko.footballupdater.models.Player;
import com.ko.footballupdater.models.PlayerMatchPerformanceStats;
import com.ko.footballupdater.models.Post;
import com.ko.footballupdater.models.Team;
import com.ko.footballupdater.repositories.PlayerRepository;
import com.ko.footballupdater.repositories.PostRepository;
import com.ko.footballupdater.repositories.TeamRepository;
import com.ko.footballupdater.responses.UpdatePlayersResponse;
import com.ko.footballupdater.services.AmazonS3Service;
import com.ko.footballupdater.services.EmailService;
Expand All @@ -27,11 +25,9 @@

import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -43,6 +39,8 @@
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class PlayerServiceTest {
Expand All @@ -53,9 +51,6 @@ public class PlayerServiceTest {
@Mock
private PlayerRepository playerRepository;

@Mock
private TeamRepository teamRepository;

@Mock
private PostRepository postRepository;

Expand All @@ -77,12 +72,16 @@ public class PlayerServiceTest {
@Mock
private InstagramPostProperies instagramPostProperies;

@Mock
private ImageGeneratorProperties imageGeneratorProperties;

@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
when(instagramPostProperies.getVersion()).thenReturn(2);
when(instagramPostProperies.getDefaultHashtags()).thenReturn("#default");
when(instagramPostProperies.getAccountName()).thenReturn("Insta account name");
when(imageGeneratorProperties.isEnabled()).thenReturn(true);
}

@Test
Expand Down Expand Up @@ -197,6 +196,51 @@ public void updateDataForAllPlayers_playersFound_successful() throws Exception {
assertEquals(2, response.getPlayersUpdated().size());
assertEquals(playerToUpdate1, response.getPlayersUpdated().get(0));
assertEquals(playerToUpdate2, response.getPlayersUpdated().get(1));

// Image gen and upload should be called for each player
verify(imageGeneratorService, times(2)).generatePlayerStatImage(any(Post.class));
verify(amazonS3Service, times(2)).uploadToS3(any(Post.class));
}

@Test
public void updateDataForAllPlayers_playersFound_imageGenIsDisabled_successful() throws Exception {
when(imageGeneratorProperties.isEnabled()).thenReturn(false);

List<Player> players = new ArrayList<>();
Player playerToUpdate1 = new Player("Player1");
playerToUpdate1.setCheckedStatus(new CheckedStatus());
playerToUpdate1.setId(1);
Player playerToUpdate2 = new Player("Player1");
playerToUpdate2.setCheckedStatus(new CheckedStatus());
playerToUpdate2.setId(2);
players.add(playerToUpdate1);
players.add(playerToUpdate2);
when(playerRepository.findAll()).thenReturn(players);

PlayerMatchPerformanceStats mockPerformanceStats = mock(PlayerMatchPerformanceStats.class);
when(parsingService.parsePlayerMatchData(playerToUpdate1)).thenReturn(mockPerformanceStats);
when(parsingService.parsePlayerMatchData(playerToUpdate2)).thenReturn(mockPerformanceStats);

Match match = new Match("https://url", Date.from(Instant.now()), "homeTeamName", "awayTeamName", "relevantTeamName");
when(mockPerformanceStats.getMatch()).thenReturn(match);

when(emailService.sendEmailUpdate(anyList())).thenReturn(true);

when(postRepository.save(any(Post.class))).thenReturn(new Post());

when(playerRepository.saveAll(any())).thenReturn(List.of(playerToUpdate1));
UpdatePlayersResponse response = playerService.updateDataForAllPlayers();

assertTrue(response.isEmailSent());
assertEquals(2, response.getNumPlayersUpdated());
assertNotNull(response.getPlayersUpdated());
assertEquals(2, response.getPlayersUpdated().size());
assertEquals(playerToUpdate1, response.getPlayersUpdated().get(0));
assertEquals(playerToUpdate2, response.getPlayersUpdated().get(1));

// Should not call image gen
verify(imageGeneratorService, times(0)).generatePlayerStatImage(any(Post.class));
verify(amazonS3Service, times(0)).uploadToS3(any(Post.class));
}

@Test
Expand Down

0 comments on commit a33c998

Please sign in to comment.