Add timestamp checking to YouTube feeds

This commit is contained in:
2026-06-27 01:26:31 -05:00
parent 45a8b010fc
commit 594c71b37e
5 changed files with 88 additions and 33 deletions

32
pom.xml
View File

@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.fpghoti</groupId> <groupId>com.fpghoti</groupId>
<artifactId>Biscuit</artifactId> <artifactId>Biscuit</artifactId>
<version>2.0 Beta</version> <version>2.1</version>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -78,7 +78,7 @@
<dependency> <dependency>
<groupId>org.fusesource.jansi</groupId> <groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId> <artifactId>jansi</artifactId>
<version>2.4.1</version> <version>2.4.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.GhotiMayo</groupId> <groupId>com.github.GhotiMayo</groupId>
@@ -88,7 +88,7 @@
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-configuration2</artifactId> <artifactId>commons-configuration2</artifactId>
<version>2.11.0</version> <version>2.15.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
@@ -104,7 +104,7 @@
<dependency> <dependency>
<groupId>ch.qos.logback</groupId> <groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<version>1.5.17</version> <version>1.5.37</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.jcabi</groupId> <groupId>com.jcabi</groupId>
@@ -114,12 +114,12 @@
<dependency> <dependency>
<groupId>joda-time</groupId> <groupId>joda-time</groupId>
<artifactId>joda-time</artifactId> <artifactId>joda-time</artifactId>
<version>2.13.1</version> <version>2.14.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.dv8tion</groupId> <groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId> <artifactId>JDA</artifactId>
<version>6.4.1</version> <version>6.4.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.cage</groupId> <groupId>com.github.cage</groupId>
@@ -127,34 +127,34 @@
<version>1.0</version> <version>1.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.activation</groupId> <groupId>com.sun.activation</groupId>
<artifactId>activation</artifactId> <artifactId>javax.activation</artifactId>
<version>1.1.1</version> <version>1.2.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.mail</groupId> <groupId>com.sun.mail</groupId>
<artifactId>mail</artifactId> <artifactId>javax.mail</artifactId>
<version>1.4.7</version> <version>1.6.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>dev.arbjerg</groupId> <groupId>dev.arbjerg</groupId>
<artifactId>lavaplayer</artifactId> <artifactId>lavaplayer</artifactId>
<version>2.2.4</version> <version>2.2.6</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>dev.lavalink.youtube</groupId> <groupId>dev.lavalink.youtube</groupId>
<artifactId>v2</artifactId> <artifactId>v2</artifactId>
<version>1.13.3</version> <version>1.18.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.pf4j</groupId> <groupId>org.pf4j</groupId>
<artifactId>pf4j</artifactId> <artifactId>pf4j</artifactId>
<version>3.13.0</version> <version>3.15.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>xom</groupId> <groupId>xom</groupId>
<artifactId>xom</artifactId> <artifactId>xom</artifactId>
<version>1.3.9</version> <version>1.4.6</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -203,6 +203,7 @@ public class BiscuitGuild {
String youTubeChannelURL = "null"; String youTubeChannelURL = "null";
String message = "null"; String message = "null";
String lastVideo = "null"; String lastVideo = "null";
String lastVideoTimestamp = "null";
Properties prop = new Properties(); Properties prop = new Properties();
InputStream input = null; InputStream input = null;
@@ -221,6 +222,7 @@ public class BiscuitGuild {
youTubeChannelURL = prop.getProperty("YouTubeChannelURL"); youTubeChannelURL = prop.getProperty("YouTubeChannelURL");
message = prop.getProperty("Message"); message = prop.getProperty("Message");
lastVideo = prop.getProperty("LastVideo"); lastVideo = prop.getProperty("LastVideo");
lastVideoTimestamp = prop.getProperty("LastVideoTimestamp");
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} finally { } finally {
@@ -240,6 +242,7 @@ public class BiscuitGuild {
} }
YTFeed feed = new YTFeed(alias, textChannel, youTubeChannelURL, message); YTFeed feed = new YTFeed(alias, textChannel, youTubeChannelURL, message);
feed.setLastVideo(lastVideo); feed.setLastVideo(lastVideo);
feed.setLastVideoTimestamp(lastVideoTimestamp);
return new YTFeedConfig(this, feed); return new YTFeedConfig(this, feed);
} }
@@ -273,7 +276,7 @@ public class BiscuitGuild {
YTFeedConfig config = ytfeeds.get(s); YTFeedConfig config = ytfeeds.get(s);
YTFeed feed = config.getFeed(); YTFeed feed = config.getFeed();
feed.post(); feed.post();
config.setLastPosted(feed.getLastVideo()); config.setLastPosted(feed.getLastVideo(), feed.getLastVideoTimestamp());
} }
} }

View File

@@ -13,10 +13,11 @@ public class YTEntry {
private String id; private String id;
private String title; private String title;
private String author; private String author;
private String timestamp;
private String description; private String description;
private String thumbnail; private String thumbnail;
public YTEntry(String id, String title, String author, String description, String thumbnail) { public YTEntry(String id, String title, String author, String timestamp, String description, String thumbnail) {
this.id = id; this.id = id;
this.title = title; this.title = title;
this.author = author; this.author = author;
@@ -36,6 +37,10 @@ public class YTEntry {
return author; return author;
} }
public String getTimestamp() {
return timestamp;
}
public String getDescription() { public String getDescription() {
return description; return description;
} }

View File

@@ -2,6 +2,9 @@ package com.fpghoti.biscuit.rss;
import java.awt.Color; import java.awt.Color;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.format.DateTimeParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -23,6 +26,7 @@ public class YTFeed {
private String channelURL; private String channelURL;
private String message; private String message;
private String lastVideo; private String lastVideo;
private String lastVideoTimestamp;
public YTFeed(String alias, TextChannel channel, String channelURL, String message) { public YTFeed(String alias, TextChannel channel, String channelURL, String message) {
this.guild = BiscuitGuild.getBiscuitGuild(channel.getGuild()); this.guild = BiscuitGuild.getBiscuitGuild(channel.getGuild());
@@ -31,6 +35,7 @@ public class YTFeed {
this.channelURL = channelURL; this.channelURL = channelURL;
this.message = message; this.message = message;
lastVideo = ""; lastVideo = "";
lastVideoTimestamp = "";
} }
public String getAlias() { public String getAlias() {
@@ -53,10 +58,22 @@ public class YTFeed {
return lastVideo; return lastVideo;
} }
public String getLastVideoTimestamp() {
return lastVideoTimestamp;
}
public Instant getLastInstant() {
return parseTimestamp(lastVideoTimestamp);
}
public void setLastVideo(String link) { public void setLastVideo(String link) {
lastVideo = link; lastVideo = link;
} }
public void setLastVideoTimestamp(String link) {
lastVideoTimestamp = link;
}
public void post(){ public void post(){
List<YTEntry> ytentries = getEntries(); List<YTEntry> ytentries = getEntries();
int index = 0; int index = 0;
@@ -70,9 +87,20 @@ public class YTFeed {
} }
index = 0; index = 0;
for(YTEntry entry : ytentries) { for(YTEntry entry : ytentries) {
if(index > lastVidIndex) { String timestamp = entry.getTimestamp();
Instant entryTimestamp = parseTimestamp(timestamp);
if(entryTimestamp == null) {
guild.error("Could not retrieve YouTube feed entry timestamp!");
return;
}
if(getLastInstant() == null) {
guild.error("Could not retrieve YouTube feed last timestamp!");
return;
}
if(index > lastVidIndex && entryTimestamp.isAfter(getLastInstant())) {
String link = entry.getURL(); String link = entry.getURL();
lastVideo = link; lastVideo = link;
lastVideoTimestamp = timestamp;
MessageText.send(channel, message); MessageText.send(channel, message);
MessageText.send(channel, entry.getEmbedMessage()); MessageText.send(channel, entry.getEmbedMessage());
} }
@@ -100,6 +128,7 @@ public class YTFeed {
String id = ""; String id = "";
String title = ""; String title = "";
String author = ""; String author = "";
String timestamp = "";
String description = ""; String description = "";
String thumbnail = ""; String thumbnail = "";
@@ -119,6 +148,9 @@ public class YTFeed {
} }
} }
} }
if(value.getLocalName().equals("published")) {
timestamp = value.getValue();
}
if(value.getLocalName().equals("group")) { if(value.getLocalName().equals("group")) {
Elements subvalues = value.getChildElements(); Elements subvalues = value.getChildElements();
for(Element subvalue : subvalues) { for(Element subvalue : subvalues) {
@@ -132,7 +164,7 @@ public class YTFeed {
} }
} }
ytentries.add(new YTEntry(id, title, author, description, thumbnail)); ytentries.add(new YTEntry(id, title, author, timestamp, description, thumbnail));
} }
} }
@@ -140,4 +172,16 @@ public class YTFeed {
} }
private Instant parseTimestamp(String timestamp) {
if (timestamp == null || timestamp.trim().isEmpty()) {
return null;
}
try {
return OffsetDateTime.parse(timestamp.trim()).toInstant();
} catch (DateTimeParseException e) {
throw new IllegalArgumentException("Invalid YouTube timestamp: " + timestamp, e);
}
}
} }

View File

@@ -54,7 +54,7 @@ public class YTFeedConfig {
} }
public void setLastPosted(String link) { public void setLastPosted(String link, String timestamp) {
PropertiesConfiguration prop = new PropertiesConfiguration(); PropertiesConfiguration prop = new PropertiesConfiguration();
PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(); PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout();
prop.setLayout(layout); prop.setLayout(layout);
@@ -62,7 +62,9 @@ public class YTFeedConfig {
layout.load(prop, new FileReader(config)); layout.load(prop, new FileReader(config));
FileWriter fw = new FileWriter(config); FileWriter fw = new FileWriter(config);
prop.setProperty("LastVideo", link); prop.setProperty("LastVideo", link);
prop.setProperty("LastVideoTimestamp", timestamp);
feed.setLastVideo(link); feed.setLastVideo(link);
feed.setLastVideoTimestamp(timestamp);
layout.save(prop, fw); layout.save(prop, fw);
} catch (ConfigurationException e) { } catch (ConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
@@ -105,6 +107,7 @@ public class YTFeedConfig {
addProperty("YouTubeChannelURL", feed.getChannelURL(), prop); addProperty("YouTubeChannelURL", feed.getChannelURL(), prop);
addProperty("Message", feed.getMesage(), prop); addProperty("Message", feed.getMesage(), prop);
addProperty("LastVideo", "", prop); addProperty("LastVideo", "", prop);
addProperty("LastVideoTimestamp", "", prop);
} }
private void addProperty(String key, String value, PropertiesConfiguration prop) { private void addProperty(String key, String value, PropertiesConfiguration prop) {