Skip to content

Commit

Permalink
Merge pull request #76 from mattwright324/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mattwright324 authored Mar 17, 2021
2 parents 1e11eed + 556d512 commit a76e612
Show file tree
Hide file tree
Showing 55 changed files with 1,648 additions and 958 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ target/
*.sqlite3-journal
exports/
*.~vsdx
*.log
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.mattw.youtube</groupId>
<artifactId>youtube-comment-suite</artifactId>
<version>1.4.5</version>
<version>1.4.6</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/mattw/youtube/commentsuite/CommentSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class CommentSuite extends Application {

private static CommentDatabase database;
private static YouTube youTube;
private static String youTubeApiKey;
private static OAuth2Manager oauth2Manager;

public static void main(String[] args) {
Expand All @@ -62,7 +61,6 @@ public void start(final Stage stage) {
youTube = new YouTube.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), null)
.setApplicationName("youtube-comment-suite")
.build();
youTubeApiKey = config.getDataObject().getYoutubeApiKey();
database = new CommentDatabase("commentsuite.sqlite3");
oauth2Manager = new OAuth2Manager();

Expand Down
18 changes: 5 additions & 13 deletions src/main/java/io/mattw/youtube/commentsuite/ImageCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import io.mattw.youtube.commentsuite.db.YouTubeObject;
import io.mattw.youtube.commentsuite.oauth2.YouTubeAccount;
import javafx.scene.image.Image;
import org.apache.logging.log4j.LogManager;
Expand All @@ -27,14 +26,10 @@ public interface ImageCache {
String thumbFormat = "jpg";

Cache<Object, Image> thumbCache = CacheBuilder.newBuilder()
.maximumSize(500)
.expireAfterAccess(5, TimeUnit.MINUTES)
.maximumSize(1000)
.expireAfterAccess(30, TimeUnit.MINUTES)
.build();

static Image toLetterAvatar(final YouTubeObject object) {
return toLetterAvatar(object.getTitle());
}

static Image toLetterAvatar(final String s) {
if (s == null || s.isEmpty()) {
return toLetterAvatar(" ");
Expand Down Expand Up @@ -93,15 +88,12 @@ static Image findOrGetImage(final String id, final String imageUrl) {
return image;
}

static Image findOrGetImage(final YouTubeObject object) {
return findOrGetImage(object.getId(), object.getThumbUrl());
}

static Image findOrGetImage(final YouTubeAccount account) {
return findOrGetImage(account.getChannelId(), account.getThumbUrl());
}

static boolean hasImageCached(final YouTubeObject object) {
return thumbCache.getIfPresent(object.getId()) != null;
static boolean hasImageCached(final String id) {
return thumbCache.getIfPresent(id) != null;
}

}
43 changes: 28 additions & 15 deletions src/main/java/io/mattw/youtube/commentsuite/db/ChannelsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
Expand All @@ -11,10 +13,15 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import static io.mattw.youtube.commentsuite.db.SQLLoader.INSERT_IGNORE_CHANNELS;
import static io.mattw.youtube.commentsuite.db.SQLLoader.INSERT_OR_CHANNELS;

public class ChannelsTable extends TableHelper<YouTubeChannel> {

private static final Logger logger = LogManager.getLogger();

private static final String INSERT_IGNORE = INSERT_OR_CHANNELS.toString().replace(":method", "IGNORE");
private static final String INSERT_REPLACE = INSERT_OR_CHANNELS.toString().replace(":method", "REPLACE");

private final Cache<String, YouTubeChannel> cache = CacheBuilder.newBuilder()
.expireAfterAccess(5, TimeUnit.MINUTES)
.build();
Expand All @@ -25,9 +32,11 @@ public ChannelsTable(final Connection connection) {

@Override
public YouTubeChannel to(ResultSet resultSet) throws SQLException {
return new YouTubeChannel(resultSet.getString("channel_id"),
resultSet.getString("channel_name"),
resultSet.getString("channel_profile_url"));
return new YouTubeChannel()
.setId(resultSet.getString("channel_id"))
.setTitle(resultSet.getString("channel_name"))
.setThumbUrl(resultSet.getString("channel_profile_url"))
.setType(YouTubeType.CHANNEL);
}

public void check(String id) {
Expand Down Expand Up @@ -95,16 +104,7 @@ public boolean exists(String id) throws SQLException {

@Override
public void insertAll(List<YouTubeChannel> objects) throws SQLException {
try (PreparedStatement ps = preparedStatement(INSERT_IGNORE_CHANNELS.toString())) {
for (YouTubeChannel c : objects) {
ps.setString(1, c.getId());
ps.setString(2, c.getTitle());
ps.setString(3, c.getThumbUrl());
ps.setBoolean(4, false);
ps.addBatch();
}
ps.executeBatch();
}
insert(objects, INSERT_IGNORE);
}

@Override
Expand All @@ -122,7 +122,20 @@ public void deleteAll(List<YouTubeChannel> objects) throws SQLException {

@Override
public void updateAll(List<YouTubeChannel> objects) throws SQLException {
// not updating, ignoring if exists
insert(objects, INSERT_REPLACE);
}

private void insert(List<YouTubeChannel> objects, String insertSql) throws SQLException {
try (PreparedStatement ps = preparedStatement(insertSql)) {
for (YouTubeChannel c : objects) {
ps.setString(1, c.getId());
ps.setString(2, c.getTitle());
ps.setString(3, c.getThumbUrl());
ps.setBoolean(4, false);
ps.addBatch();
}
ps.executeBatch();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public void prepForExport() {
if (groupItem.isPresent()) {
GroupItem item = groupItem.get();

this.withGroupItem = String.format("%s / %s", item.getId(), item.getTitle());
this.withGroupItem = String.format("%s / %s", item.getId(), item.getDisplayName());
} else {
this.withGroupItem = "All Item(s)";
}
Expand Down
54 changes: 33 additions & 21 deletions src/main/java/io/mattw/youtube/commentsuite/db/CommentsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.mattw.youtube.commentsuite.CommentSuite;
import io.mattw.youtube.commentsuite.events.TagsChangeEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
Expand All @@ -10,11 +12,16 @@
import java.util.ArrayList;
import java.util.List;

import static io.mattw.youtube.commentsuite.db.SQLLoader.INSERT_IGNORE_COMMENTS;
import static io.mattw.youtube.commentsuite.db.SQLLoader.INSERT_OR_COMMENTS;
import static org.apache.commons.lang3.StringUtils.isBlank;

public class CommentsTable extends TableHelper<YouTubeComment> {

private static final Logger logger = LogManager.getLogger();

private static final String INSERT_IGNORE = INSERT_OR_COMMENTS.toString().replace(":method", "IGNORE");
private static final String INSERT_REPLACE = INSERT_OR_COMMENTS.toString().replace(":method", "REPLACE");

private CommentDatabase database;

public CommentsTable(final Connection connection, final CommentDatabase database) {
Expand All @@ -24,7 +31,8 @@ public CommentsTable(final Connection connection, final CommentDatabase database

@Override
public YouTubeComment to(ResultSet resultSet) throws SQLException {
return new YouTubeComment(resultSet.getString("comment_id"))
return new YouTubeComment()
.setId(resultSet.getString("comment_id"))
.setCommentText(resultSet.getString("comment_text"))
.setPublished(resultSet.getLong("comment_date"))
.setVideoId(resultSet.getString("video_id"))
Expand Down Expand Up @@ -83,24 +91,7 @@ public boolean exists(String id) throws SQLException {

@Override
public void insertAll(List<YouTubeComment> objects) throws SQLException {
try (PreparedStatement ps = preparedStatement(INSERT_IGNORE_COMMENTS.toString())) {
for (YouTubeComment ct : objects) {
ps.setString(1, ct.getId());
ps.setString(2, ct.getChannelId());
ps.setString(3, ct.getVideoId());
ps.setLong(4, ct.getPublished());
ps.setString(5, ct.getCommentText());
ps.setLong(6, ct.getLikes());
ps.setLong(7, ct.getReplyCount());
ps.setBoolean(8, ct.isReply());
ps.setString(9, ct.getParentId());
ps.addBatch();
}
ps.executeBatch();
}

// Delete from moderated if it is now approved
database.moderatedComments().deleteAll(objects);
insert(objects, INSERT_IGNORE);
}

@Override
Expand All @@ -118,7 +109,28 @@ public void deleteAll(List<YouTubeComment> objects) throws SQLException {

@Override
public void updateAll(List<YouTubeComment> objects) throws SQLException {
// not updating, ignoring if exists
insert(objects, INSERT_REPLACE);
}

private void insert(List<YouTubeComment> objects, String insertSql) throws SQLException {
try (PreparedStatement ps = preparedStatement(insertSql)) {
for (YouTubeComment ct : objects) {
ps.setString(1, ct.getId());
ps.setString(2, ct.getChannelId());
ps.setString(3, ct.getVideoId());
ps.setLong(4, ct.getPublished());
ps.setString(5, ct.getCommentText());
ps.setLong(6, ct.getLikes());
ps.setLong(7, ct.getReplyCount());
ps.setBoolean(8, ct.isReply());
ps.setString(9, ct.getParentId());
ps.addBatch();
}
ps.executeBatch();
}

// Delete from moderated if it is now approved
database.moderatedComments().deleteAll(objects);
}

public void associateTags(List<YouTubeComment> comments, List<String> tags)throws SQLException {
Expand Down
Loading

0 comments on commit a76e612

Please sign in to comment.