Skip to content

Commit

Permalink
Merge pull request #121 from PAException/dev
Browse files Browse the repository at this point in the history
Minor Bugfixes
  • Loading branch information
PAException authored Apr 6, 2024
2 parents c45d85e + 843da4b commit ba8aa38
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -114,7 +115,9 @@ public List<String> sendMulticastNotification(String title, String body,
SendResponse response = responsesResponses.get(i);
if (response.isSuccessful()) continue;

if (response.getException().getMessagingErrorCode().equals(MessagingErrorCode.UNREGISTERED))
if (response.getException() != null
&& response.getException().getMessagingErrorCode() != null
&& response.getException().getMessagingErrorCode().equals(MessagingErrorCode.UNREGISTERED))
invalidTokens.add(tokens.get(i));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ private static List<SubstituteNotificationDTO> combineDTOs(List<SubstituteDTO> d
dtos.get(same.get(same.size() - 1)),
low + " - " + high
));
int indexOffset = 0;
for (Integer element : same) {
dtos.remove(element.intValue());
dtos.remove(element - indexOffset++);
}
} else {
notificationDTOs.add(SubstituteNotificationDTO.fromSubstituteDTO(dto, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -39,6 +40,8 @@ public class ArticleUpdateService extends JsonFetchingService implements Logging
private static final Logger LOGGER = LoggerFactory.getLogger(ArticleUpdateService.class);
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
private static int counter = 0;
private static long lastArticleTime = 0;

private final ArticleController articleController;
private final NotificationService notificationService;

Expand All @@ -51,7 +54,7 @@ public void fetchNewArticles() {

if ("false".equals(System.getProperty("app.scheduling.enable"))) return;
LOGGER.debug("[ARTICLE] Fetching...");
List<ArticleDTO> articles = this.updateArticles(DATE_FORMAT.format(System.currentTimeMillis() - 60 * 1000), 1);
List<ArticleDTO> articles = this.updateArticles(DATE_FORMAT.format(lastArticleTime + 1), 1);
if (articles.isEmpty()) LOGGER.debug("[ARTICLE] Not updated");
else LOGGER.debug("[ARTICLE] Fetched " + articles.size());

Expand Down Expand Up @@ -107,12 +110,19 @@ private List<ArticleDTO> updateArticles(String date, int page) {

if (json.toString().startsWith("[")) { //Errors start with {
LOGGER.info("[ARTICLE] Still fetching articles (current count: " + counter + ")");
this.updateArticles(date, page + 1).forEach(this.articleController::createOrUpdateArticle);
if (jsonArticles.size() == 100) {
this.updateArticles(date, page + 1).forEach(this.articleController::createOrUpdateArticle);
}
}
}
} catch (IOException | ParseException e) {
this.logError("[ARTICLE] Couldn't fetch", e, LOGGER);
this.logExpectedError("[ARTICLE] Couldn't fetch", e, LOGGER);
}

if (!dtos.isEmpty()) {
lastArticleTime = dtos.get(0).getDate();
}

return dtos;
}

Expand All @@ -134,8 +144,8 @@ private ArticleDTO createArticleDTO(int articleId, JsonElement article) throws I
try {
mediaUrl = WordPressAPI.getFeaturedMedia(article.getAsJsonObject().get("featured_media").getAsInt(),
content);
} catch (Exception e) {
this.logError("[ARTICLE] Couldn't load media: " + articleId, e, LOGGER);
} catch (IOException e) {
this.logExpectedError("[ARTICLE] Couldn't load media: " + articleId, e, LOGGER);
}

if (Environment.BLURHASH) {
Expand All @@ -144,7 +154,7 @@ private ArticleDTO createArticleDTO(int articleId, JsonElement article) throws I
//content = WordpressAPI.applyBlurHashToAllImages(Jsoup.parse(content)).toString(); --> not needed
blurHash = mediaUrl != null ? WordPressAPI.getBlurHash(mediaUrl) : null;
} catch (IOException e) {
this.logError("[ARTICLE] Couldn't load blur hash of image", e, LOGGER);
this.logExpectedError("[ARTICLE] Couldn't load blur hash of image", e, LOGGER);
}
}

Expand All @@ -168,6 +178,7 @@ public void loadPastArticles() {
LOGGER.debug("[ARTICLE] Starting fetching past articles");
Result<GetArticlesResponseDTO> lastArticle = this.articleController.getArticlesAfter(-1, new Paging(0, 1));
if (lastArticle.isResultPresent()) { //Empty would be an error
lastArticleTime = lastArticle.getResult().getArticles().get(0).getDate();
this.updateArticles(DATE_FORMAT.format(lastArticle.getResult().getArticles().get(0).getDate()), 1)
.forEach(this.articleController::createOrUpdateArticle);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
Expand Down Expand Up @@ -46,14 +47,14 @@ public void updateCafeteriaInformation() {
content = WordPressAPI.applyBlurHashToAllImages(Jsoup.parse(content)).toString();
blurHash = mediaUrl != null ? WordPressAPI.getBlurHash(mediaUrl) : null;
} catch (IOException e) {
this.logError("[CAFETERIA] Couldn't load blur hash of image", e, LOGGER);
this.logExpectedError("[CAFETERIA] Couldn't load blur hash of image", e, LOGGER);
}

this.cafeteriaController.update(new CafeteriaInformationDTO(content, link, mediaUrl, blurHash));
LOGGER.info("[CAFETERIA] Updated");
} else LOGGER.debug("[CAFETERIA] Not changed");
} catch (IOException e) {
this.logError("[CAFETERIA] Couldn't fetch", e, LOGGER);
this.logExpectedError("[CAFETERIA] Couldn't fetch", e, LOGGER);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void updateEvents() {
LOGGER.info("[EVENT] Updated");
} else LOGGER.debug("[EVENT] Not changed");
} catch (IOException e) {
this.logError("[EVENT] Couldn't fetch", e, LOGGER);
this.logExpectedError("[EVENT] Couldn't fetch", e, LOGGER);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.DataInputStream;
import java.net.URL;

Expand Down Expand Up @@ -74,7 +75,7 @@ public void updateSolarSystemInfo() {

if (this.checkChanges(html, "text")) this.solarSystemController.updateText(html);
} catch (Exception e) { //IO and NullPointer
this.logError("[SOLAR] Couldn't fetch", e, LOGGER);
this.logExpectedError("[SOLAR] Couldn't fetch", e, LOGGER);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ else if (entry.text().startsWith("Abwesende Klassen"))
if (count > 0) LOGGER.info("[SUBSTITUTE] Fetched " + count);
else LOGGER.debug("[SUBSTITUTE] Not changed");
} catch (IOException | ParseException e) {
this.logError("[SUBSTITUTE] Couldn't fetch", e, LOGGER);
this.logExpectedError("[SUBSTITUTE] Couldn't fetch", e, LOGGER);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public interface LoggingComponent {
default void logError(String msg, Throwable throwable, Logger logger) {
logger.error(msg, throwable);
}

default void logExpectedError(String msg, Exception exception, Logger logger) {
logger.error(msg + " because of " + exception.getClass().getCanonicalName() + ": " + exception.getMessage());
}
}

0 comments on commit ba8aa38

Please sign in to comment.