Add timestamp checking to YouTube feeds
This commit is contained in:
52
pom.xml
52
pom.xml
@@ -5,7 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.fpghoti</groupId>
|
||||
<artifactId>Biscuit</artifactId>
|
||||
<version>2.0 Beta</version>
|
||||
<version>2.1</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
@@ -64,10 +64,10 @@
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>arbjergDev-releases</id>
|
||||
<name>Lavalink Repository</name>
|
||||
<url>https://maven.lavalink.dev/releases</url>
|
||||
</repository>
|
||||
<id>arbjergDev-releases</id>
|
||||
<name>Lavalink Repository</name>
|
||||
<url>https://maven.lavalink.dev/releases</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
@@ -78,7 +78,7 @@
|
||||
<dependency>
|
||||
<groupId>org.fusesource.jansi</groupId>
|
||||
<artifactId>jansi</artifactId>
|
||||
<version>2.4.1</version>
|
||||
<version>2.4.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.GhotiMayo</groupId>
|
||||
@@ -88,7 +88,7 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-configuration2</artifactId>
|
||||
<version>2.11.0</version>
|
||||
<version>2.15.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.spigotmc</groupId>
|
||||
@@ -104,7 +104,7 @@
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.5.17</version>
|
||||
<version>1.5.37</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jcabi</groupId>
|
||||
@@ -114,12 +114,12 @@
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>2.13.1</version>
|
||||
<version>2.14.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.dv8tion</groupId>
|
||||
<artifactId>JDA</artifactId>
|
||||
<version>6.4.1</version>
|
||||
<version>6.4.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.cage</groupId>
|
||||
@@ -127,34 +127,34 @@
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.activation</groupId>
|
||||
<artifactId>activation</artifactId>
|
||||
<version>1.1.1</version>
|
||||
<groupId>com.sun.activation</groupId>
|
||||
<artifactId>javax.activation</artifactId>
|
||||
<version>1.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.mail</groupId>
|
||||
<artifactId>mail</artifactId>
|
||||
<version>1.4.7</version>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>1.6.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>dev.arbjerg</groupId>
|
||||
<artifactId>lavaplayer</artifactId>
|
||||
<version>2.2.4</version>
|
||||
<version>2.2.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>dev.lavalink.youtube</groupId>
|
||||
<artifactId>v2</artifactId>
|
||||
<version>1.13.3</version>
|
||||
</dependency>
|
||||
<groupId>dev.lavalink.youtube</groupId>
|
||||
<artifactId>v2</artifactId>
|
||||
<version>1.18.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.pf4j</groupId>
|
||||
<artifactId>pf4j</artifactId>
|
||||
<version>3.13.0</version>
|
||||
<version>3.15.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xom</groupId>
|
||||
<artifactId>xom</artifactId>
|
||||
<version>1.3.9</version>
|
||||
<dependency>
|
||||
<groupId>xom</groupId>
|
||||
<artifactId>xom</artifactId>
|
||||
<version>1.4.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -203,6 +203,7 @@ public class BiscuitGuild {
|
||||
String youTubeChannelURL = "null";
|
||||
String message = "null";
|
||||
String lastVideo = "null";
|
||||
String lastVideoTimestamp = "null";
|
||||
|
||||
Properties prop = new Properties();
|
||||
InputStream input = null;
|
||||
@@ -221,6 +222,7 @@ public class BiscuitGuild {
|
||||
youTubeChannelURL = prop.getProperty("YouTubeChannelURL");
|
||||
message = prop.getProperty("Message");
|
||||
lastVideo = prop.getProperty("LastVideo");
|
||||
lastVideoTimestamp = prop.getProperty("LastVideoTimestamp");
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
@@ -240,6 +242,7 @@ public class BiscuitGuild {
|
||||
}
|
||||
YTFeed feed = new YTFeed(alias, textChannel, youTubeChannelURL, message);
|
||||
feed.setLastVideo(lastVideo);
|
||||
feed.setLastVideoTimestamp(lastVideoTimestamp);
|
||||
return new YTFeedConfig(this, feed);
|
||||
}
|
||||
|
||||
@@ -273,7 +276,7 @@ public class BiscuitGuild {
|
||||
YTFeedConfig config = ytfeeds.get(s);
|
||||
YTFeed feed = config.getFeed();
|
||||
feed.post();
|
||||
config.setLastPosted(feed.getLastVideo());
|
||||
config.setLastPosted(feed.getLastVideo(), feed.getLastVideoTimestamp());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,10 +13,11 @@ public class YTEntry {
|
||||
private String id;
|
||||
private String title;
|
||||
private String author;
|
||||
private String timestamp;
|
||||
private String description;
|
||||
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.title = title;
|
||||
this.author = author;
|
||||
@@ -36,6 +37,10 @@ public class YTEntry {
|
||||
return author;
|
||||
}
|
||||
|
||||
public String getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package com.fpghoti.biscuit.rss;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -23,6 +26,7 @@ public class YTFeed {
|
||||
private String channelURL;
|
||||
private String message;
|
||||
private String lastVideo;
|
||||
private String lastVideoTimestamp;
|
||||
|
||||
public YTFeed(String alias, TextChannel channel, String channelURL, String message) {
|
||||
this.guild = BiscuitGuild.getBiscuitGuild(channel.getGuild());
|
||||
@@ -31,6 +35,7 @@ public class YTFeed {
|
||||
this.channelURL = channelURL;
|
||||
this.message = message;
|
||||
lastVideo = "";
|
||||
lastVideoTimestamp = "";
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
@@ -52,11 +57,23 @@ public class YTFeed {
|
||||
public String getLastVideo() {
|
||||
return lastVideo;
|
||||
}
|
||||
|
||||
|
||||
public String getLastVideoTimestamp() {
|
||||
return lastVideoTimestamp;
|
||||
}
|
||||
|
||||
public Instant getLastInstant() {
|
||||
return parseTimestamp(lastVideoTimestamp);
|
||||
}
|
||||
|
||||
public void setLastVideo(String link) {
|
||||
lastVideo = link;
|
||||
}
|
||||
|
||||
public void setLastVideoTimestamp(String link) {
|
||||
lastVideoTimestamp = link;
|
||||
}
|
||||
|
||||
public void post(){
|
||||
List<YTEntry> ytentries = getEntries();
|
||||
int index = 0;
|
||||
@@ -70,9 +87,20 @@ public class YTFeed {
|
||||
}
|
||||
index = 0;
|
||||
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();
|
||||
lastVideo = link;
|
||||
lastVideoTimestamp = timestamp;
|
||||
MessageText.send(channel, message);
|
||||
MessageText.send(channel, entry.getEmbedMessage());
|
||||
}
|
||||
@@ -100,6 +128,7 @@ public class YTFeed {
|
||||
String id = "";
|
||||
String title = "";
|
||||
String author = "";
|
||||
String timestamp = "";
|
||||
String description = "";
|
||||
String thumbnail = "";
|
||||
|
||||
@@ -119,6 +148,9 @@ public class YTFeed {
|
||||
}
|
||||
}
|
||||
}
|
||||
if(value.getLocalName().equals("published")) {
|
||||
timestamp = value.getValue();
|
||||
}
|
||||
if(value.getLocalName().equals("group")) {
|
||||
Elements subvalues = value.getChildElements();
|
||||
for(Element subvalue : subvalues) {
|
||||
@@ -132,12 +164,24 @@ public class YTFeed {
|
||||
}
|
||||
}
|
||||
|
||||
ytentries.add(new YTEntry(id, title, author, description, thumbnail));
|
||||
ytentries.add(new YTEntry(id, title, author, timestamp, description, thumbnail));
|
||||
}
|
||||
}
|
||||
|
||||
return ytentries.reversed();
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,15 +54,17 @@ public class YTFeedConfig {
|
||||
}
|
||||
|
||||
|
||||
public void setLastPosted(String link) {
|
||||
public void setLastPosted(String link, String timestamp) {
|
||||
PropertiesConfiguration prop = new PropertiesConfiguration();
|
||||
PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout();
|
||||
prop.setLayout(layout);
|
||||
try {
|
||||
layout.load(prop, new FileReader(config));
|
||||
FileWriter fw = new FileWriter(config);
|
||||
prop.setProperty("LastVideo", link);
|
||||
prop.setProperty("LastVideo", link);
|
||||
prop.setProperty("LastVideoTimestamp", timestamp);
|
||||
feed.setLastVideo(link);
|
||||
feed.setLastVideoTimestamp(timestamp);
|
||||
layout.save(prop, fw);
|
||||
} catch (ConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
@@ -105,6 +107,7 @@ public class YTFeedConfig {
|
||||
addProperty("YouTubeChannelURL", feed.getChannelURL(), prop);
|
||||
addProperty("Message", feed.getMesage(), prop);
|
||||
addProperty("LastVideo", "", prop);
|
||||
addProperty("LastVideoTimestamp", "", prop);
|
||||
}
|
||||
|
||||
private void addProperty(String key, String value, PropertiesConfiguration prop) {
|
||||
|
||||
Reference in New Issue
Block a user