Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suppress video insert errors on conflict #657

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/main/java/me/kavin/piped/utils/VideoHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import me.kavin.piped.utils.obj.db.Video;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.StatelessSession;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.stream.StreamExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
Expand Down Expand Up @@ -49,14 +48,7 @@ public static void handleNewVideo(StreamInfo info, long time, me.kavin.piped.uti
Video video = new Video(info.getId(), info.getName(), info.getViewCount(), info.getDuration(),
Math.max(infoTime, time), info.getThumbnailUrl(), info.isShortFormContent(), channel);

var tr = s.beginTransaction();
try {
s.insert(video);
tr.commit();
} catch (Exception e) {
tr.rollback();
ExceptionHandler.handle(e);
}
insertVideo(video);
return;
}
}
Expand Down Expand Up @@ -87,14 +79,7 @@ public static void handleNewVideo(StreamExtractor extractor, long time, me.kavin
Video video = new Video(extractor.getId(), extractor.getName(), extractor.getViewCount(), extractor.getLength(),
Math.max(infoTime, time), extractor.getThumbnailUrl(), isShort, channel);

var tr = s.beginTransaction();
try {
s.insert(video);
tr.commit();
} catch (Exception e) {
tr.rollback();
ExceptionHandler.handle(e);
}
insertVideo(video);

}
}
Expand Down Expand Up @@ -169,4 +154,26 @@ public static boolean updateVideo(String id, long views, long duration, String t
return updated > 0;
}
}

public static void insertVideo(Video video) {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
var tr = s.beginTransaction();
try {
s.createNativeMutationQuery("INSERT INTO videos (uploader_id,duration,is_short,thumbnail,title,uploaded,views,id) values " +
"(:uploader_id,:duration,:is_short,:thumbnail,:title,:uploaded,:views,:id) ON CONFLICT DO NOTHING")
.setParameter("uploader_id", video.getChannel())
.setParameter("duration", video.getDuration())
.setParameter("is_short", video.isShort())
.setParameter("thumbnail", video.getThumbnail())
.setParameter("title", video.getTitle())
.setParameter("uploaded", video.getUploaded())
.setParameter("views", video.getViews())
.setParameter("id", video.getId()).executeUpdate();
tr.commit();
} catch (Exception e) {
tr.rollback();
ExceptionHandler.handle(e);
}
}
}
}