commit 42bfc69ab8fa4c3bfafb4c79d698a26b54ddc91c
parent 3ef8fa783a2143a8e5f308266c58bb24ea35f1df
Author: Kebigon <git@kebigon.xyz>
Date: Wed, 29 Jan 2020 20:25:21 +0900
Add dateBefore and dateAfter parameters
Diffstat:
2 files changed, 35 insertions(+), 10 deletions(-)
diff --git a/src/main/java/xyz/kebigon/yuzurss/FeedController.java b/src/main/java/xyz/kebigon/yuzurss/FeedController.java
@@ -1,5 +1,6 @@
package xyz.kebigon.yuzurss;
+import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -11,11 +12,13 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
+import lombok.extern.slf4j.Slf4j;
import xyz.kebigon.yuzurss.json.Feed;
import xyz.kebigon.yuzurss.json.Item;
@RestController
@CrossOrigin
+@Slf4j
public class FeedController
{
@Autowired
@@ -29,17 +32,25 @@ public class FeedController
@PostMapping
public Feed getFeeds(@RequestBody FeedRequestBody body)
{
- List<Item> items = body.getUrls().parallelStream() //
- .flatMap(this::getItems) //
- .sorted() //
- .collect(Collectors.toList());
+ Stream<Item> itemsStream = body.getUrls().parallelStream().flatMap(this::getItems);
- if (body.getLimit() < items.size())
- items = items.subList(0, body.getLimit());
+ // Date filters
+ if (body.getDateBefore() != null)
+ itemsStream = itemsStream.filter(i -> !i.getDatePublished().toInstant().truncatedTo(ChronoUnit.DAYS).isAfter(body.getDateBefore().toInstant()));
+ if (body.getDateAfter() != null)
+ itemsStream = itemsStream.filter(i -> !i.getDatePublished().toInstant().truncatedTo(ChronoUnit.DAYS).isBefore(body.getDateAfter().toInstant()));
- if (replaceYoutubeByInvidious)
+ // Sorting the items before applying the limit
+ itemsStream = itemsStream.sequential().sorted();
+
+ if (body.getLimit() != null)
+ itemsStream = itemsStream.limit(body.getLimit().intValue());
+
+ final List<Item> items = itemsStream.collect(Collectors.toList());
+
+ if (this.replaceYoutubeByInvidious)
items.parallelStream().filter(item -> item.getUrl().contains("https://www.youtube.com"))
- .forEach(item -> item.setUrl(item.getUrl().replace("https://www.youtube.com", invidiousUrl)));
+ .forEach(item -> item.setUrl(item.getUrl().replace("https://www.youtube.com", this.invidiousUrl)));
return new Feed(items);
}
diff --git a/src/main/java/xyz/kebigon/yuzurss/FeedRequestBody.java b/src/main/java/xyz/kebigon/yuzurss/FeedRequestBody.java
@@ -1,12 +1,26 @@
package xyz.kebigon.yuzurss;
+import java.util.Date;
import java.util.List;
+import com.fasterxml.jackson.annotation.JsonFormat;
+
import lombok.Data;
@Data
public class FeedRequestBody
{
- private final List<String> urls;
- private final int limit;
+ public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
+
+ public static final String DATE_FORMAT_TINEZONE = "UTC";
+
+ private List<String> urls;
+
+ private Integer limit;
+
+ @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "UTC")
+ private Date dateBefore;
+
+ @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "UTC")
+ private Date dateAfter;
}
\ No newline at end of file