Add timestamp checking to YouTube feeds
This commit is contained in:
@@ -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