Implemented Pinboard.in integration.

This commit is contained in:
Erik C. Thauvin 2017-05-17 17:38:14 -07:00
parent 16953c8f98
commit 73dbd6c0d8
12 changed files with 214 additions and 157 deletions

View file

@ -18,12 +18,12 @@ public final class ReleaseInfo {
public final static String PROJECT = "mobibot";
public final static LocalDateTime BUILDDATE =
LocalDateTime.ofInstant(Instant.ofEpochMilli(1494906988487L), ZoneId.systemDefault());
LocalDateTime.ofInstant(Instant.ofEpochMilli(1495067438992L), ZoneId.systemDefault());
public final static int MAJOR = 0;
public final static int MINOR = 7;
public final static int PATCH = 1;
public final static String PRERELEASE = "beta";
public final static String BUILDMETA = "020";
public final static String BUILDMETA = "021";
/**
* The full version string.

View file

@ -206,11 +206,11 @@ public class EntryLink implements Serializable {
}
/**
* Returns the tags formatted for del.icio.us.
* Returns the tags formatted for pinboard.in
*
* @return The tags as a comma-delimited string.
*/
public final String getDeliciousTags() {
public final String getPinboardTags() {
final StringBuilder tags = new StringBuilder(nick);
for (final Object tag : this.tags) {

View file

@ -32,7 +32,6 @@
package net.thauvin.erik.mobibot;
import com.rometools.rome.io.FeedException;
import del.icio.us.DeliciousConstants;
import net.thauvin.erik.mobibot.modules.*;
import net.thauvin.erik.semver.Version;
import org.apache.commons.cli.*;
@ -167,22 +166,16 @@ public class Mobibot extends PircBot {
// The default tags/categories.
private String defaultTags = "";
// The del.icio.us posts handler.
private DeliciousPoster delicious = null;
// The feed URL.
private String feedURL = "";
// The ident message.
private String identMsg = "";
// The ident nick.
private String identNick = "";
// The NickServ ident password.
private String identPwd = "";
// The pinboard posts handler.
private Pinboard pinboard = null;
// Today's date.
private String today = Utils.today();
@ -356,10 +349,8 @@ public class Mobibot extends PircBot {
final String identPwd = p.getProperty("ident", "");
final String tags = p.getProperty("tags", "");
// Get the del.icio.us properties
final String dname = p.getProperty("delicious-user");
final String dpwd = p.getProperty("delicious-pwd");
final String dapi = p.getProperty("declicious-api-endpoint", DeliciousConstants.API_ENDPOINT);
// Get the pinboard properties
final String pinApiToken = p.getProperty("pinboard-api-token");
// Create the bot
final Mobibot bot = new Mobibot(server, port, nickname, channel, logsDir);
@ -380,8 +371,8 @@ public class Mobibot extends PircBot {
bot.setFeedURL(feedURL);
bot.setBacklogsUrl(backlogsURL);
// Set the del.icio.us authentication
bot.setDeliciousAuth(dapi, dname, dpwd);
// Set the pinboard authentication
bot.setPinboardAuth(pinApiToken);
// Load the modules properties
MODULES.stream().filter(AbstractModule::hasProperties).forEach(
@ -1044,8 +1035,8 @@ public class Mobibot extends PircBot {
final EntryLink entry = entries.get(index);
send(channel, Utils.buildLink(index, entry));
if (delicious != null) {
delicious.addPost(entry);
if (pinboard != null) {
pinboard.addPost(entry);
}
saveEntries(isBackup);
@ -1152,8 +1143,8 @@ public class Mobibot extends PircBot {
final EntryLink entry = entries.get(index);
if (entry.getLogin().equals(login) || isOp(sender)) {
if (delicious != null) {
delicious.deletePost(entry);
if (pinboard != null) {
pinboard.deletePost(entry);
}
entries.remove(index);
@ -1169,8 +1160,8 @@ public class Mobibot extends PircBot {
final EntryLink entry = entries.get(index);
entry.setTitle(cmd.substring(1).trim());
if (delicious != null) {
delicious.updatePost(entry.getLink(), entry);
if (pinboard != null) {
pinboard.updatePost(entry.getLink(), entry);
}
send(channel, Utils.buildLink(index, entry));
@ -1189,8 +1180,8 @@ public class Mobibot extends PircBot {
entry.setLink(link);
if (delicious != null) {
delicious.updatePost(oldLink, entry);
if (pinboard != null) {
pinboard.updatePost(oldLink, entry);
}
send(channel, Utils.buildLink(index, entry));
@ -1239,8 +1230,8 @@ public class Mobibot extends PircBot {
if (entry.getLogin().equals(login) || isOp(sender)) {
entry.setTags(cmd);
if (delicious != null) {
delicious.updatePost(entry.getLink(), entry);
if (pinboard != null) {
pinboard.updatePost(entry.getLink(), entry);
}
send(channel, Utils.buildTags(index, entry));
@ -1439,7 +1430,7 @@ public class Mobibot extends PircBot {
private void recapResponse(final String sender, final boolean isPrivate) {
if (this.recap.size() > 0) {
for (final String recap : this.recap) {
send(sender, recap, isPrivate);
send(sender, recap, isPrivate);
}
} else {
send(sender, "Sorry, nothing to recap.", true);
@ -1491,19 +1482,6 @@ public class Mobibot extends PircBot {
send(sender, message, false);
}
/**
* Sets the del.icio.us authentication.
*
* @param apiEndPoint The API end point.
* @param username The del.icio.us user name.
* @param password The del.icio.us password.
*/
private void setDeliciousAuth(final String apiEndPoint, final String username, final String password) {
if (Utils.isValidString(username) && Utils.isValidString(password)) {
delicious = new DeliciousPoster(apiEndPoint, username, password, ircServer);
}
}
/**
* Sets the feed URL.
*
@ -1541,6 +1519,17 @@ public class Mobibot extends PircBot {
}
}
/**
* Sets the pinboard authentication.
*
* @param apiToken The API token
*/
private void setPinboardAuth(final String apiToken) {
if (Utils.isValidString(apiToken)) {
pinboard = new Pinboard(this, apiToken, ircServer);
}
}
/**
* Sets the default tags/categories.
*

View file

@ -1,5 +1,5 @@
/*
* DeliciousPoster.java
* Pinboard.java
*
* Copyright (c) 2004-2017, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
@ -31,32 +31,46 @@
*/
package net.thauvin.erik.mobibot;
import del.icio.us.Delicious;
import net.thauvin.erik.pinboard.PinboardPoster;
import javax.swing.*;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* The class to handle posts to del.icio.us.
* The class to handle posts to pinbard.in.
*
* @author <a href="http://erik.thauvin.net/" target="_blank">Erik C. Thauvin</a>
* @created Mar 5, 2005
* @created 2017-05-17
* @since 1.0
*/
class DeliciousPoster {
private final Delicious delicious;
class Pinboard {
private final String ircServer;
private final PinboardPoster pinboard;
/**
* Creates a new {@link DeliciousPoster} instance.
* Creates a new {@link Pinboard} instance.
*
* @param apiEndPoint The API end point.
* @param username The del.icio.us user name.
* @param password The del.icio.us password.
* @param bot The bot's instance.
* @param apiToken The API end point.
* @param ircServer The IRC server.
*/
public DeliciousPoster(final String apiEndPoint, final String username, final String password, final String ircServer) {
delicious = new Delicious(username, password, apiEndPoint);
public Pinboard(final Mobibot bot, final String apiToken, final String ircServer) {
pinboard = new PinboardPoster(apiToken);
this.ircServer = ircServer;
if (bot.getLogger().isDebugEnabled()) {
final ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.FINE);
final Logger logger = pinboard.getLogger();
logger.addHandler(consoleHandler);
logger.setLevel(Level.FINE);
}
}
/**
@ -69,11 +83,11 @@ class DeliciousPoster {
@Override
protected Boolean doInBackground()
throws Exception {
return delicious.addPost(entry.getLink(),
return pinboard.addPin(entry.getLink(),
entry.getTitle(),
postedBy(entry),
entry.getDeliciousTags(),
entry.getDate());
entry.getPinboardTags(),
formatDate(entry.getDate()));
}
};
@ -92,13 +106,23 @@ class DeliciousPoster {
@Override
protected Boolean doInBackground()
throws Exception {
return delicious.deletePost(link);
return pinboard.deletePin(link);
}
};
worker.execute();
}
/**
* Format a date to a UTC timestamp.
*
* @param date The date.
* @return The date in {@link DateTimeFormatter#ISO_INSTANT} format.
*/
private String formatDate(final Date date) {
return ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).format(DateTimeFormatter.ISO_INSTANT);
}
/**
* Returns he del.icio.us extended attribution line.
*
@ -121,19 +145,19 @@ class DeliciousPoster {
protected Boolean doInBackground()
throws Exception {
if (!oldUrl.equals(entry.getLink())) {
delicious.deletePost(oldUrl);
pinboard.deletePin(oldUrl);
return delicious.addPost(entry.getLink(),
return pinboard.addPin(entry.getLink(),
entry.getTitle(),
postedBy(entry),
entry.getDeliciousTags(),
entry.getDate());
entry.getPinboardTags(),
formatDate(entry.getDate()));
} else {
return delicious.addPost(entry.getLink(),
return pinboard.addPin(entry.getLink(),
entry.getTitle(),
postedBy(entry),
entry.getDeliciousTags(),
entry.getDate(),
entry.getPinboardTags(),
formatDate(entry.getDate()),
true,
true);
}

View file

@ -140,7 +140,7 @@ final public class Utils {
* @return The entry's tags.
*/
static String buildTags(final int entryIndex, final EntryLink entry) {
return (Commands.LINK_CMD + (entryIndex + 1) + "T: " + entry.getDeliciousTags().replaceAll(",", ", "));
return (Commands.LINK_CMD + (entryIndex + 1) + "T: " + entry.getPinboardTags().replaceAll(",", ", "));
}
/**